nm: Nearest Mean Classification

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/nm.R

Description

Function for nearest mean classification.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
nm(x, ...)

## Default S3 method:
nm(x, grouping, gamma = 0, ...)
## S3 method for class 'data.frame'
nm(x, ...)
## S3 method for class 'matrix'
nm(x, grouping, ..., subset, na.action = na.fail)
## S3 method for class 'formula'
nm(formula, data = NULL, ..., subset, na.action = na.fail)

Arguments

x

matrix or data frame containing the explanatory variables (required, if formula is not given)

grouping

factor specifying the class for each observation (required, if formula is not given)

formula

formula of the form groups ~ x1 + x2 + .... That is, the response is the grouping factor and the right hand side specifies the (non-factor) discriminators

data

Data frame from which variables specified in formula are preferentially to be taken

gamma

gamma parameter for rbf weight of the distance to mean. If gamma=0 the posterior is 1 for the nearest class (mean) and 0 else.

subset

An index vector specifying the cases to be used in the training sample. (Note: If given, this argument must be named!)

na.action

specify the action to be taken if NAs are found. The default action is for the procedure to fail. An alternative is na.omit, which leads to rejection of cases with missing values on any required variable. (Note: If given, this argument must be named.)

...

further arguments passed to the underlying sknn function

Details

nm is calling sknn with the class means as observations. If gamma>0 a gaussian like density is used to weight the distance to the class means weight=exp(-gamma*distance). This is similar to an rbf kernel. If the distances are large it may be useful to scale the data first.

Value

A list containing the function call and the class means (learn)).

Author(s)

Karsten Luebke, [email protected]

See Also

sknn, rda, knn

Examples

1
2
3
4
5
data(B3)
x <- nm(PHASEN ~ ., data = B3)
x$learn
x <- nm(PHASEN ~ ., data = B3, gamma = 0.1)
predict(x)$post

Example output

Loading required package: MASS
   BSP91JW   CP91JW    DEFRATE      EWAJW EXIMRATE     GM1JW      IAU91JW
1 4.036102 3.545254 -1.3394915  1.0645763 3.880847  8.613390  6.893559322
2 6.312083 6.427500 -0.8479167  2.6808333 3.104167 11.015833 11.360416667
3 2.769149 3.656596 -0.8380851  1.0778723 3.013617  6.841489 -0.003404255
4 1.093333 2.620000 -1.6548148 -0.9940741 3.124815  9.607778 -1.939259259
     IB91JW   LSTKJW   PBSPJW    PCPJW    ZINSK   ZINSLR
1  3.397119 2.111017 2.789153 2.214746 4.715085 3.976271
2  6.830417 4.195833 3.927917 3.187917 6.836250 3.571250
3  1.673191 6.291064 4.496170 4.062128 7.682553 3.230000
4 -1.491852 4.249630 4.206296 3.938148 5.672222 3.739259
                  1            2            3            4
1955,4 3.472988e-12 1.000000e+00 6.256530e-28 9.209813e-38
1956,1 1.967463e-11 1.000000e+00 8.959782e-23 1.924185e-33
1956,2 1.882068e-06 9.999981e-01 2.223063e-14 5.553180e-22
1956,3 1.174916e-01 5.236247e-03 8.771519e-01 1.202531e-04
1956,4 9.067165e-05 2.234848e-08 9.968615e-01 3.047773e-03
1957,1 3.870388e-01 1.020512e-03 6.119223e-01 1.837694e-05
1957,2 5.496775e-09 3.318330e-17 6.801114e-02 9.319889e-01
1957,3 1.166941e-03 3.486237e-08 5.046809e-01 4.941521e-01
1957,4 9.151862e-01 4.514699e-04 4.943612e-02 3.492618e-02
1958,1 8.511008e-06 2.470814e-11 2.222837e-03 9.977687e-01
1958,2 9.016348e-01 8.529318e-03 7.993207e-02 9.903783e-03
1958,3 8.568335e-01 1.431629e-01 3.607673e-06 3.512772e-08
1958,4 3.067390e-01 6.932609e-01 4.717733e-08 1.233783e-10
1959,1 1.382153e-02 9.861785e-01 4.985562e-10 1.393021e-14
1959,2 9.385329e-05 9.999061e-01 1.018568e-17 1.280491e-21
1959,3 2.745819e-03 9.972542e-01 2.241443e-14 5.375662e-17
1959,4 6.405728e-05 9.999359e-01 5.953432e-17 6.669036e-21
1960,1 6.988521e-09 1.000000e+00 5.725794e-26 5.818246e-33
1960,2 3.440691e-05 9.999656e-01 1.207759e-15 7.516451e-21
1960,3 6.612344e-05 9.999339e-01 1.263519e-14 3.231589e-20
1960,4 3.087470e-03 9.969125e-01 1.340914e-10 1.287466e-15
1961,1 1.875582e-06 9.999981e-01 1.809291e-14 1.141122e-22
1961,2 1.229742e-02 9.877024e-01 1.340580e-07 8.658827e-13
1961,3 2.297179e-01 7.696405e-01 6.410647e-04 5.668398e-07
1961,4 4.960982e-01 4.997128e-01 4.135101e-03 5.394124e-05
1962,1 9.852647e-01 2.189668e-03 4.685051e-03 7.860576e-03
1962,2 2.187218e-01 7.808754e-01 3.996255e-04 3.160063e-06
1962,3 7.325656e-01 2.672382e-01 1.957836e-04 3.591392e-07
1962,4 9.032811e-01 9.265692e-02 4.054543e-03 7.389732e-06
1963,1 7.339309e-16 3.430401e-30 2.172746e-07 9.999998e-01
1963,2 8.269534e-03 2.127405e-08 9.615644e-01 3.016605e-02
1963,3 8.335183e-01 3.978251e-04 1.657725e-01 3.113607e-04
1963,4 8.898305e-01 6.742892e-04 1.094195e-01 7.574328e-05
1964,1 5.552572e-10 1.000000e+00 6.276229e-20 1.265898e-32
1964,2 2.377382e-01 7.622618e-01 2.008911e-08 2.120929e-12
1964,3 9.557727e-01 4.414078e-02 8.640369e-05 1.395924e-07
1964,4 4.228472e-02 9.577040e-01 1.125014e-05 1.613403e-10
1965,1 2.358090e-01 7.641897e-01 1.293290e-06 4.573253e-10
1965,2 5.046386e-01 4.947181e-01 6.421651e-04 1.112905e-06
1965,3 2.721444e-01 6.712938e-01 5.655436e-02 7.492892e-06
1965,4 9.384091e-01 1.556845e-02 4.579564e-02 2.267695e-04
1966,1 7.087844e-01 1.269574e-01 1.642543e-01 3.823452e-06
1966,2 2.887408e-02 1.025617e-06 9.654188e-01 5.706075e-03
1966,3 1.715603e-06 1.012954e-14 7.673364e-01 2.326619e-01
1966,4 7.816257e-11 1.022979e-22 2.608917e-01 7.391083e-01
1967,1 1.067741e-19 6.628875e-38 3.480040e-05 9.999652e-01
1967,2 6.058609e-18 3.811354e-37 1.005614e-06 9.999990e-01
1967,3 1.691011e-13 1.559688e-30 4.752637e-06 9.999952e-01
1967,4 9.759991e-01 5.045654e-09 1.085870e-04 2.389234e-02
1968,1 4.232896e-04 7.851046e-14 1.512715e-02 9.844496e-01
1968,2 9.997123e-01 6.389233e-05 1.841760e-04 3.965700e-05
1968,3 6.543684e-02 9.345632e-01 1.951972e-11 3.454319e-15
1968,4 8.532763e-01 1.467106e-01 1.303746e-05 5.223202e-09
1969,1 3.943878e-07 9.999996e-01 8.795726e-23 4.390598e-29
1969,2 5.915987e-07 9.999994e-01 3.635206e-19 3.274949e-26
1969,3 1.558122e-08 1.000000e+00 2.943650e-21 8.742149e-29
1969,4 2.844816e-04 9.997155e-01 2.451193e-09 5.647720e-15
1970,1 9.234552e-06 9.999908e-01 1.927538e-10 6.734492e-17
1970,2 2.888751e-08 1.000000e+00 9.785636e-13 6.232884e-22
1970,3 9.718046e-06 9.999872e-01 3.063395e-06 1.936368e-14
1970,4 3.862314e-06 9.999959e-01 2.061494e-07 6.264200e-16
1971,1 3.441319e-10 1.000000e+00 5.282803e-15 4.909304e-26
1971,2 1.839722e-02 9.534786e-01 2.811598e-02 8.243135e-06
1971,3 1.445700e-06 1.404073e-08 9.562189e-01 4.377967e-02
1971,4 7.345582e-06 2.780965e-08 9.138428e-01 8.614982e-02
1972,1 3.050049e-03 7.821211e-04 9.934640e-01 2.703812e-03
1972,2 2.563834e-06 2.715661e-11 3.783050e-01 6.216924e-01
1972,3 1.335780e-04 1.737048e-08 2.428053e-01 7.570611e-01
1972,4 6.583025e-04 1.002554e-05 9.443097e-01 5.502195e-02
1973,1 8.323619e-03 3.686080e-03 9.865152e-01 1.475052e-03
1973,2 1.907606e-06 1.020643e-08 9.987786e-01 1.219488e-03
1973,3 5.650933e-10 2.184012e-15 9.999027e-01 9.726599e-05
1973,4 1.784516e-11 2.694241e-18 9.983770e-01 1.622967e-03
1974,1 3.495811e-18 1.961723e-32 1.054575e-01 8.945425e-01
1974,2 3.309108e-20 1.751800e-34 2.290788e-03 9.977092e-01
1974,3 4.320003e-17 5.203365e-28 7.138464e-04 9.992862e-01
1974,4 8.035438e-19 3.234839e-30 1.216134e-04 9.998784e-01
1975,1 4.756468e-19 7.025849e-32 4.146113e-07 9.999996e-01
1975,2 1.123433e-11 4.518901e-21 3.278204e-06 9.999967e-01
1975,3 1.785758e-10 3.404891e-19 1.114261e-05 9.999889e-01
1975,4 9.637751e-01 1.222955e-03 6.129196e-05 3.494065e-02
1976,1 9.971666e-01 2.746922e-03 6.381602e-07 8.581077e-05
1976,2 7.890956e-01 2.109031e-01 1.180233e-06 4.340608e-08
1976,3 3.344110e-01 6.902980e-05 5.985957e-01 6.692425e-02
1976,4 9.902541e-01 2.793671e-04 9.428759e-03 3.777358e-05
1977,1 9.495612e-01 4.799824e-02 2.440279e-03 2.665894e-07
1977,2 2.153304e-01 2.721643e-06 7.096038e-01 7.506303e-02
1977,3 8.433622e-01 1.566371e-01 6.924847e-07 6.671118e-09
1977,4 9.939679e-01 4.724456e-04 2.867925e-03 2.691708e-03
1978,1 1.875724e-01 2.445933e-06 5.781048e-03 8.066441e-01
1978,2 9.162849e-01 8.369739e-02 1.620503e-05 1.469576e-06
1978,3 5.745327e-01 4.254631e-01 4.062001e-06 1.166544e-07
1978,4 9.562770e-01 4.361003e-02 9.857144e-05 1.440811e-05
1979,1 9.918409e-01 8.147995e-03 8.420808e-06 2.682733e-06
1979,2 2.829828e-01 7.170153e-01 1.886117e-06 4.410491e-10
1979,3 8.217633e-01 1.759412e-01 2.295405e-03 9.574092e-08
1979,4 5.989776e-01 3.245700e-01 7.645220e-02 2.031714e-07
1980,1 1.924036e-03 9.980587e-01 1.728536e-05 8.075518e-15
1980,2 9.198976e-07 1.372799e-12 9.992905e-01 7.085780e-04
1980,3 1.653621e-06 7.104327e-12 9.989598e-01 1.038576e-03
1980,4 6.086415e-09 1.879658e-17 3.493405e-01 6.506595e-01
1981,1 1.513218e-12 2.489632e-24 1.969965e-03 9.980300e-01
1981,2 2.339505e-09 1.259689e-18 8.866675e-01 1.133325e-01
1981,3 2.577487e-10 1.283297e-20 8.105798e-01 1.894202e-01
1981,4 1.167114e-12 3.949235e-26 1.467601e-01 8.532399e-01
1982,1 2.021965e-15 6.276169e-30 5.379590e-03 9.946204e-01
1982,2 6.572380e-13 3.671270e-26 2.928630e-03 9.970714e-01
1982,3 1.560676e-14 5.208210e-28 1.489215e-03 9.985108e-01
1982,4 3.225832e-08 1.638642e-18 4.319003e-02 9.568099e-01
1983,1 1.771391e-04 1.630827e-12 4.921423e-03 9.949014e-01
1983,2 9.613690e-01 3.413997e-06 2.246396e-03 3.638115e-02
1983,3 9.989620e-01 1.373144e-04 3.202853e-04 5.803885e-04
1983,4 9.966626e-01 3.337206e-03 1.972565e-07 2.034946e-08
1984,1 3.963533e-01 6.083644e-07 5.930797e-01 1.056641e-02
1984,2 6.537712e-10 2.104297e-22 4.196614e-02 9.580339e-01
1984,3 8.987604e-01 1.462443e-08 8.702338e-02 1.421616e-02
1984,4 6.585481e-02 2.894535e-10 6.979735e-01 2.361717e-01
1985,1 1.303221e-03 7.709256e-15 2.759518e-04 9.984208e-01
1985,2 9.996519e-01 3.479989e-04 8.893914e-08 5.285830e-10
1985,3 8.867201e-01 2.139783e-07 9.784082e-02 1.543886e-02
1985,4 9.971931e-01 3.603590e-07 1.209688e-03 1.596895e-03
1986,1 9.598700e-01 1.609785e-06 3.122576e-02 8.902619e-03
1986,2 9.968522e-01 3.122231e-03 2.504093e-05 5.603311e-07
1986,3 8.649695e-01 5.460061e-06 9.956050e-02 3.546451e-02
1986,4 9.474326e-01 6.196164e-06 4.903993e-02 3.521318e-03
1987,1 9.192757e-01 1.959045e-07 6.295037e-03 7.442907e-02
1987,2 5.973861e-01 4.688865e-07 1.430719e-01 2.595416e-01
1987,3 9.995478e-01 2.231940e-05 2.188156e-04 2.110916e-04
1987,4 9.949703e-01 3.335791e-06 2.172346e-03 2.854048e-03
1988,1 2.047822e-01 7.952178e-01 1.331397e-08 3.012846e-13
1988,2 9.998637e-01 6.817994e-06 4.259450e-05 8.689389e-05
1988,3 9.996827e-01 1.158645e-05 6.264816e-05 2.430339e-04
1988,4 9.999308e-01 1.192765e-05 1.573440e-05 4.152823e-05
1989,1 7.549508e-01 2.450472e-01 2.024052e-06 4.811993e-10
1989,2 9.920881e-01 7.910957e-03 8.953224e-07 5.292392e-10
1989,3 9.989622e-01 6.122715e-05 9.729765e-04 3.591625e-06
1989,4 9.952112e-01 4.756394e-03 3.235102e-05 8.214640e-09
1990,1 8.025813e-03 9.919742e-01 4.148329e-12 7.506986e-19
1990,2 6.388090e-02 9.361191e-01 5.332443e-08 3.087810e-12
1990,3 9.375471e-05 9.999062e-01 4.536061e-14 3.320149e-15
1990,4 7.947931e-05 9.999205e-01 1.064924e-13 5.326177e-14
1991,1 5.785052e-04 9.994215e-01 6.689156e-14 5.606857e-11
1991,2 4.791956e-06 9.999952e-01 5.018906e-15 3.632442e-17
1991,3 1.411074e-01 8.588737e-01 1.896947e-05 2.038736e-09
1991,4 6.020221e-02 4.260563e-05 9.375429e-01 2.212304e-03
1992,1 1.449101e-03 2.216462e-07 9.984883e-01 6.241682e-05
1992,2 3.628242e-06 1.039444e-13 8.635592e-01 1.364372e-01
1992,3 2.234100e-07 7.217030e-15 7.891512e-01 2.108485e-01
1992,4 5.187204e-08 2.343732e-15 2.563485e-01 7.436514e-01
1993,1 1.310536e-19 1.524020e-34 1.552034e-05 9.999845e-01
1993,2 5.719466e-20 2.103846e-35 3.496522e-05 9.999650e-01
1993,3 1.932700e-17 2.245841e-32 1.292095e-05 9.999871e-01
1993,4 3.212406e-17 3.508225e-35 1.582053e-04 9.998418e-01
1994,1 3.417393e-09 1.486352e-19 1.254912e-03 9.987451e-01
1994,2 1.348941e-04 8.197988e-14 3.024959e-03 9.968401e-01
1994,3 3.291346e-02 9.995157e-11 7.579785e-03 9.595068e-01
1994,4 9.973659e-01 1.738824e-05 8.005291e-05 2.536681e-03

klaR documentation built on May 31, 2017, 1:53 a.m.

Related to nm in klaR...