Cluster prediction for multivariate observations based on Gaussian finite mixture models estimated by Mclust.

# S3 method for Mclust
predict(object, newdata, ...)

Arguments

object

an object of class 'Mclust' resulting from a call to Mclust.

newdata

a data frame or matrix giving the data. If missing the clustering data obtained from the call to Mclust are classified.

...

further arguments passed to or from other methods.

Value

Returns a list of with the following components:

classification

a factor of predicted cluster labels for newdata.

z

a matrix whose [i,k]th entry is the probability that observation i in newdata belongs to the kth cluster.

Author

Luca Scrucca

See also

Examples

model <- Mclust(faithful)

# predict cluster for the observed data
pred <- predict(model) 
str(pred)
#> List of 2
#>  $ classification: int [1:272] 1 2 1 2 3 2 3 1 2 3 ...
#>  $ z             : num [1:272, 1:3] 9.72e-01 8.12e-13 9.97e-01 3.06e-07 1.17e-02 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:272] "1" "2" "3" "4" ...
#>   .. ..$ : chr [1:3] "1" "2" "3"
pred$z              # equal to model$z
#>                1            2            3
#> 1   9.718934e-01 1.352740e-08 2.810657e-02
#> 2   8.122644e-13 1.000000e+00 6.813886e-21
#> 3   9.967828e-01 2.108920e-05 3.196126e-03
#> 4   3.056090e-07 9.999997e-01 1.407759e-13
#> 5   1.165923e-02 1.183305e-19 9.883408e-01
#> 6   3.050001e-03 9.969497e-01 3.160044e-07
#> 7   2.962193e-03 3.235046e-22 9.970378e-01
#> 8   9.758494e-01 1.249199e-09 2.415062e-02
#> 9   4.942048e-12 1.000000e+00 1.658458e-19
#> 10  5.501578e-02 2.166813e-17 9.449842e-01
#> 11  1.571164e-12 1.000000e+00 1.757682e-20
#> 12  7.125339e-01 2.401096e-12 2.874661e-01
#> 13  1.522898e-01 1.947418e-14 8.477102e-01
#> 14  1.847134e-14 1.000000e+00 1.201399e-22
#> 15  2.602557e-03 2.076298e-21 9.973974e-01
#> 16  5.633073e-10 1.000000e+00 1.222865e-16
#> 17  7.200548e-12 1.000000e+00 3.172820e-20
#> 18  1.118067e-03 8.116665e-23 9.988819e-01
#> 19  6.725706e-15 1.000000e+00 1.038195e-23
#> 20  1.065036e-01 3.367444e-15 8.934964e-01
#> 21  2.463338e-13 1.000000e+00 2.233795e-21
#> 22  1.847134e-14 1.000000e+00 1.201399e-22
#> 23  9.920432e-01 4.123192e-07 7.956377e-03
#> 24  9.691025e-01 3.054996e-02 3.475489e-04
#> 25  8.788529e-03 7.084549e-18 9.912115e-01
#> 26  9.745951e-01 2.763897e-09 2.540491e-02
#> 27  3.407283e-11 1.000000e+00 1.195405e-18
#> 28  3.212497e-01 9.439276e-13 6.787503e-01
#> 29  7.919031e-01 1.107526e-10 2.080969e-01
#> 30  2.358343e-02 1.921480e-17 9.764166e-01
#> 31  6.186793e-02 7.827461e-15 9.381321e-01
#> 32  1.675999e-02 1.533009e-17 9.832400e-01
#> 33  9.944634e-01 2.568250e-04 5.279751e-03
#> 34  4.481798e-01 7.290846e-13 5.518202e-01
#> 35  7.991348e-01 7.705541e-10 2.008652e-01
#> 36  2.807776e-11 1.000000e+00 1.647089e-18
#> 37  2.851554e-13 1.000000e+00 5.014801e-21
#> 38  7.559766e-04 1.392489e-22 9.992440e-01
#> 39  1.147741e-11 1.000000e+00 1.127699e-19
#> 40  1.514760e-03 1.420726e-23 9.984852e-01
#> 41  4.864473e-02 1.399562e-16 9.513553e-01
#> 42  2.095305e-11 1.000000e+00 3.268107e-19
#> 43  8.471971e-03 6.485284e-20 9.915280e-01
#> 44  1.467127e-12 1.000000e+00 7.172049e-21
#> 45  8.565250e-03 1.027686e-17 9.914347e-01
#> 46  9.977963e-01 8.107782e-07 2.202843e-03
#> 47  7.542305e-01 3.880895e-08 2.457695e-01
#> 48  2.196538e-10 1.000000e+00 2.589804e-17
#> 49  4.540594e-03 2.058058e-20 9.954594e-01
#> 50  3.234673e-10 1.000000e+00 1.364151e-17
#> 51  8.853270e-04 2.304271e-21 9.991147e-01
#> 52  2.714269e-03 9.717403e-23 9.972857e-01
#> 53  1.571164e-12 1.000000e+00 1.757682e-20
#> 54  7.559766e-04 1.392489e-22 9.992440e-01
#> 55  2.127974e-13 1.000000e+00 9.950224e-22
#> 56  5.284488e-04 1.086389e-23 9.994716e-01
#> 57  9.101062e-01 2.941878e-08 8.989382e-02
#> 58  3.035027e-12 1.000000e+00 6.155272e-21
#> 59  7.074222e-03 8.763842e-19 9.929258e-01
#> 60  6.540510e-02 2.445501e-16 9.345949e-01
#> 61  3.410870e-08 1.000000e+00 1.098053e-14
#> 62  1.509745e-02 4.411429e-19 9.849025e-01
#> 63  2.749305e-14 1.000000e+00 1.742360e-22
#> 64  1.061560e-03 1.707274e-22 9.989384e-01
#> 65  1.240645e-11 1.000000e+00 1.033014e-19
#> 66  4.318969e-02 3.868255e-19 9.568103e-01
#> 67  1.932731e-01 4.780616e-14 8.067269e-01
#> 68  2.286484e-03 1.332539e-20 9.977135e-01
#> 69  1.342481e-08 1.000000e+00 8.692163e-16
#> 70  2.008720e-03 8.551723e-20 9.979913e-01
#> 71  4.610517e-01 3.385527e-13 5.389483e-01
#> 72  5.071456e-11 1.000000e+00 1.733666e-18
#> 73  1.328412e-02 2.835508e-18 9.867159e-01
#> 74  4.616284e-01 5.207901e-11 5.383716e-01
#> 75  7.592772e-10 1.000000e+00 2.553912e-17
#> 76  8.854996e-05 7.441358e-25 9.999115e-01
#> 77  6.763293e-10 1.000000e+00 3.223454e-17
#> 78  7.258917e-03 6.041756e-19 9.927411e-01
#> 79  7.303983e-01 1.169947e-10 2.696017e-01
#> 80  9.745951e-01 2.763897e-09 2.540491e-02
#> 81  2.296721e-01 3.696588e-13 7.703279e-01
#> 82  5.879368e-02 1.072609e-16 9.412063e-01
#> 83  2.588245e-01 5.886331e-12 7.411755e-01
#> 84  1.100918e-03 9.988991e-01 9.937520e-09
#> 85  3.348483e-01 4.467229e-12 6.651517e-01
#> 86  3.890515e-04 4.029390e-25 9.996109e-01
#> 87  6.016098e-01 2.524587e-11 3.983902e-01
#> 88  1.177245e-02 1.201813e-18 9.882276e-01
#> 89  1.147751e-10 1.000000e+00 2.764243e-17
#> 90  5.586289e-01 1.616689e-13 4.413711e-01
#> 91  2.624614e-08 1.000000e+00 6.173453e-15
#> 92  7.139508e-02 5.407336e-18 9.286049e-01
#> 93  6.317286e-13 1.000000e+00 1.054763e-20
#> 94  8.251763e-04 4.636603e-22 9.991748e-01
#> 95  5.633028e-11 1.000000e+00 4.988789e-19
#> 96  6.037828e-02 1.137000e-14 9.396217e-01
#> 97  3.558599e-03 3.689506e-21 9.964414e-01
#> 98  8.938701e-01 3.043581e-09 1.061299e-01
#> 99  9.402753e-13 1.000000e+00 1.529697e-20
#> 100 4.439774e-04 9.670839e-24 9.995560e-01
#> 101 1.665979e-05 9.999833e-01 4.392748e-11
#> 102 5.146737e-02 4.376120e-18 9.485326e-01
#> 103 4.475494e-11 1.000000e+00 5.854163e-18
#> 104 1.471627e-02 6.400264e-19 9.852837e-01
#> 105 4.181515e-01 3.253355e-13 5.818485e-01
#> 106 1.915831e-13 1.000000e+00 3.457826e-21
#> 107 2.670820e-03 1.431560e-21 9.973292e-01
#> 108 2.610027e-13 1.000000e+00 1.988317e-21
#> 109 7.616311e-04 9.184970e-24 9.992384e-01
#> 110 9.464002e-01 1.131278e-09 5.359983e-02
#> 111 1.587192e-03 1.576850e-20 9.984128e-01
#> 112 1.301956e-07 9.999999e-01 7.519433e-14
#> 113 5.324023e-04 7.165921e-25 9.994676e-01
#> 114 2.702041e-02 3.031389e-17 9.729796e-01
#> 115 8.036454e-13 1.000000e+00 2.474800e-21
#> 116 4.424756e-03 2.985099e-20 9.955752e-01
#> 117 5.101283e-09 1.000000e+00 4.316568e-15
#> 118 6.532596e-03 1.736927e-20 9.934674e-01
#> 119 8.335337e-12 1.000000e+00 7.122883e-20
#> 120 3.305090e-02 1.539350e-18 9.669491e-01
#> 121 6.770422e-06 9.999932e-01 7.257881e-11
#> 122 3.121312e-01 2.043738e-11 6.878688e-01
#> 123 1.016630e-01 7.121113e-15 8.983370e-01
#> 124 5.071456e-11 1.000000e+00 1.733666e-18
#> 125 7.057939e-03 5.691153e-21 9.929421e-01
#> 126 8.945755e-01 1.994239e-10 1.054245e-01
#> 127 2.349827e-13 1.000000e+00 6.909647e-21
#> 128 1.434458e-02 9.285648e-19 9.856554e-01
#> 129 1.371431e-08 1.000000e+00 6.589274e-15
#> 130 4.817023e-03 6.452448e-22 9.951830e-01
#> 131 8.647851e-14 1.000000e+00 1.644001e-21
#> 132 2.143190e-01 7.256895e-15 7.856810e-01
#> 133 8.656118e-04 9.991343e-01 4.236381e-08
#> 134 6.969308e-02 7.856501e-18 9.303069e-01
#> 135 6.522677e-14 1.000000e+00 8.981236e-22
#> 136 3.881733e-02 2.606207e-17 9.611827e-01
#> 137 1.294720e-12 1.000000e+00 2.421824e-20
#> 138 3.693752e-04 8.475181e-25 9.996306e-01
#> 139 5.754502e-11 1.000000e+00 3.781855e-18
#> 140 9.155242e-01 8.922485e-10 8.447583e-02
#> 141 1.271110e-01 2.548436e-15 8.728890e-01
#> 142 5.076795e-08 9.999999e-01 1.592479e-14
#> 143 1.079512e-02 3.612667e-19 9.892049e-01
#> 144 8.040481e-04 6.724494e-22 9.991960e-01
#> 145 5.074409e-02 1.006568e-15 9.492559e-01
#> 146 2.302645e-10 1.000000e+00 8.372484e-18
#> 147 4.311859e-03 4.329707e-20 9.956881e-01
#> 148 8.515086e-12 1.000000e+00 5.399649e-19
#> 149 1.115927e-04 1.702496e-28 9.998884e-01
#> 150 5.457239e-13 1.000000e+00 4.698339e-21
#> 151 1.222524e-04 1.362083e-24 9.998777e-01
#> 152 5.004916e-01 5.193022e-12 4.995084e-01
#> 153 1.045187e-05 9.999895e-01 1.235923e-11
#> 154 5.892098e-03 7.688890e-20 9.941079e-01
#> 155 9.740026e-01 6.316488e-07 2.599678e-02
#> 156 4.551836e-01 7.643306e-11 5.448164e-01
#> 157 1.398213e-02 1.347170e-18 9.860179e-01
#> 158 4.239136e-01 1.442305e-15 5.760864e-01
#> 159 5.457239e-13 1.000000e+00 4.698339e-21
#> 160 6.459649e-01 1.096637e-13 3.540351e-01
#> 161 6.732841e-11 1.000000e+00 2.337600e-17
#> 162 2.548501e-01 3.676249e-15 7.451499e-01
#> 163 2.173231e-10 1.000000e+00 9.406154e-18
#> 164 8.152864e-01 1.601752e-10 1.847136e-01
#> 165 9.833253e-01 1.778141e-05 1.665694e-02
#> 166 6.001466e-03 8.036682e-19 9.939985e-01
#> 167 2.439069e-06 9.999976e-01 2.277971e-12
#> 168 2.168957e-04 5.885076e-26 9.997831e-01
#> 169 5.236342e-12 1.000000e+00 1.476206e-19
#> 170 6.929452e-03 5.444971e-22 9.930705e-01
#> 171 1.153277e-12 1.000000e+00 3.056737e-20
#> 172 7.674203e-10 1.000000e+00 7.031707e-17
#> 173 6.158327e-03 5.540612e-19 9.938417e-01
#> 174 9.960389e-01 2.291304e-04 3.732014e-03
#> 175 2.057063e-01 1.543074e-14 7.942937e-01
#> 176 5.737352e-02 1.557926e-16 9.426265e-01
#> 177 1.138996e-02 2.643425e-17 9.886100e-01
#> 178 3.766471e-08 1.000000e+00 7.625111e-14
#> 179 5.522189e-01 2.378695e-13 4.477811e-01
#> 180 1.775964e-01 2.155979e-13 8.224036e-01
#> 181 6.354389e-12 1.000000e+00 1.071383e-19
#> 182 6.158327e-03 5.540612e-19 9.938417e-01
#> 183 1.167960e-01 7.524305e-16 8.832040e-01
#> 184 8.993726e-01 9.050050e-11 1.006274e-01
#> 185 2.597520e-11 1.000000e+00 1.798058e-18
#> 186 2.299299e-02 2.788361e-17 9.770070e-01
#> 187 3.681036e-01 4.490247e-14 6.318964e-01
#> 188 6.522677e-14 1.000000e+00 8.981236e-22
#> 189 2.988859e-02 6.832150e-18 9.701114e-01
#> 190 2.557641e-09 1.000000e+00 5.905644e-16
#> 191 1.034386e-03 2.476085e-22 9.989656e-01
#> 192 5.180779e-12 1.000000e+00 5.361570e-20
#> 193 9.085891e-04 1.588814e-21 9.990914e-01
#> 194 3.343315e-01 2.903181e-14 6.656685e-01
#> 195 5.740909e-01 1.175468e-11 4.259091e-01
#> 196 1.271110e-01 2.548436e-15 8.728890e-01
#> 197 9.902747e-01 4.224845e-09 9.725258e-03
#> 198 3.951356e-02 2.722420e-16 9.604864e-01
#> 199 1.989174e-09 1.000000e+00 9.141704e-16
#> 200 3.046901e-03 3.434744e-20 9.969531e-01
#> 201 3.554759e-09 1.000000e+00 3.494787e-16
#> 202 5.110452e-02 6.636923e-17 9.488955e-01
#> 203 3.111355e-01 8.630793e-16 6.888645e-01
#> 204 2.083071e-12 1.000000e+00 3.217410e-20
#> 205 5.453055e-03 2.346427e-19 9.945469e-01
#> 206 2.400481e-14 1.000000e+00 2.136891e-22
#> 207 3.918381e-02 2.646264e-16 9.608162e-01
#> 208 8.164068e-01 1.050126e-11 1.835932e-01
#> 209 1.588015e-12 1.000000e+00 4.839441e-20
#> 210 1.471627e-02 6.400264e-19 9.852837e-01
#> 211 8.089220e-05 9.999191e-01 7.057578e-11
#> 212 2.408037e-03 6.334700e-21 9.975920e-01
#> 213 4.244300e-13 1.000000e+00 7.272844e-21
#> 214 8.032694e-01 5.203791e-10 1.967306e-01
#> 215 9.912176e-01 2.087079e-04 8.573687e-03
#> 216 1.133928e-01 1.660728e-14 8.866072e-01
#> 217 8.841066e-08 9.999999e-01 1.427544e-13
#> 218 1.448983e-03 1.971190e-24 9.985510e-01
#> 219 6.590714e-11 1.000000e+00 3.083617e-18
#> 220 2.087458e-01 1.606877e-13 7.912542e-01
#> 221 6.317286e-13 1.000000e+00 1.054763e-20
#> 222 9.998566e-02 6.824912e-16 9.000143e-01
#> 223 2.989302e-13 1.000000e+00 1.621217e-21
#> 224 1.387969e-02 2.042580e-17 9.861203e-01
#> 225 5.069810e-01 3.534195e-12 4.930190e-01
#> 226 2.755283e-01 1.244176e-13 7.244717e-01
#> 227 3.326739e-01 4.412307e-13 6.673261e-01
#> 228 9.102191e-02 3.049321e-15 9.089781e-01
#> 229 6.328107e-01 5.584964e-10 3.671893e-01
#> 230 8.628811e-03 6.778307e-19 9.913712e-01
#> 231 2.882723e-01 9.209705e-12 7.117277e-01
#> 232 1.848555e-07 9.999998e-01 3.373248e-13
#> 233 2.041132e-01 1.522184e-15 7.958868e-01
#> 234 6.909142e-10 1.000000e+00 2.443606e-16
#> 235 2.695946e-02 1.968646e-19 9.730405e-01
#> 236 4.269228e-12 1.000000e+00 7.387443e-20
#> 237 2.207115e-12 1.000000e+00 2.863839e-20
#> 238 7.822194e-02 2.832627e-15 9.217781e-01
#> 239 6.201193e-01 7.891822e-12 3.798807e-01
#> 240 1.839679e-06 9.999982e-01 1.244465e-12
#> 241 2.044905e-01 2.342947e-13 7.955095e-01
#> 242 2.992473e-09 1.000000e+00 3.650333e-15
#> 243 3.693752e-04 8.475181e-25 9.996306e-01
#> 244 9.380836e-02 9.061825e-01 9.159001e-06
#> 245 7.568981e-03 2.827070e-20 9.924310e-01
#> 246 8.304147e-01 3.324163e-11 1.695853e-01
#> 247 7.674203e-10 1.000000e+00 7.031707e-17
#> 248 4.437355e-02 4.102308e-17 9.556264e-01
#> 249 1.112770e-07 9.999999e-01 1.216522e-14
#> 250 4.192277e-02 1.311454e-15 9.580772e-01
#> 251 2.413896e-09 1.000000e+00 6.634756e-16
#> 252 2.258101e-02 2.668524e-18 9.774190e-01
#> 253 9.752857e-01 2.854950e-07 2.471402e-02
#> 254 1.138996e-02 2.643425e-17 9.886100e-01
#> 255 2.648343e-01 1.724430e-15 7.351657e-01
#> 256 8.424041e-01 1.028674e-10 1.575959e-01
#> 257 6.388216e-01 3.787927e-10 3.611784e-01
#> 258 2.258101e-02 2.668524e-18 9.774190e-01
#> 259 9.809727e-11 1.000000e+00 4.472095e-18
#> 260 8.204836e-02 1.341164e-15 9.179516e-01
#> 261 1.275783e-03 1.947860e-21 9.987242e-01
#> 262 1.136386e-02 1.716631e-19 9.886361e-01
#> 263 1.083236e-11 1.000000e+00 1.266925e-19
#> 264 1.167960e-01 7.524305e-16 8.832040e-01
#> 265 3.968583e-13 1.000000e+00 2.185978e-20
#> 266 7.131699e-08 9.999999e-01 2.594669e-14
#> 267 1.368736e-03 9.680041e-21 9.986313e-01
#> 268 2.860117e-01 5.829758e-14 7.139883e-01
#> 269 3.688036e-11 1.000000e+00 8.066162e-18
#> 270 3.563232e-02 5.032974e-19 9.643677e-01
#> 271 4.737018e-14 1.000000e+00 5.672819e-22
#> 272 1.552378e-02 4.682115e-17 9.844762e-01
pred$classification # equal to 
#>   [1] 1 2 1 2 3 2 3 1 2 3 2 1 3 2 3 2 2 3 2 3 2 2 1 1 3 1 2 3 1 3 3 3 1 3 1 2 2
#>  [38] 3 2 3 3 2 3 2 3 1 1 2 3 2 3 3 2 3 2 3 1 2 3 3 2 3 2 3 2 3 3 3 2 3 3 2 3 3
#>  [75] 2 3 2 3 1 1 3 3 3 2 3 3 1 3 2 1 2 3 2 3 2 3 3 1 2 3 2 3 2 3 3 2 3 2 3 1 3
#> [112] 2 3 3 2 3 2 3 2 3 2 3 3 2 3 1 2 3 2 3 2 3 2 3 2 3 2 3 2 1 3 2 3 3 3 2 3 2
#> [149] 3 2 3 1 2 3 1 3 3 3 2 1 2 3 2 1 1 3 2 3 2 3 2 2 3 1 3 3 3 2 1 3 2 3 3 1 2
#> [186] 3 3 2 3 2 3 2 3 3 1 3 1 3 2 3 2 3 3 2 3 2 3 1 2 3 2 3 2 1 1 3 2 3 2 3 2 3
#> [223] 2 3 1 3 3 3 1 3 3 2 3 2 3 2 2 3 1 2 3 2 3 2 3 1 2 3 2 3 2 3 1 3 3 1 1 3 2
#> [260] 3 3 3 2 3 2 2 3 3 2 3 2 3
plot(faithful, col = pred$classification, pch = pred$classification)


# predict cluster over a grid
grid <- apply(faithful, 2, function(x) seq(min(x), max(x), length = 50))
grid <- expand.grid(eruptions = grid[,1], waiting = grid[,2])
pred <- predict(model, grid)
plot(grid, col = mclust.options("classPlotColors")[pred$classification], pch = 15, cex = 0.5)
points(faithful, pch = model$classification)