kknn                  package:kknn                  R Documentation

_W_e_i_g_h_t_e_d _k-_N_e_a_r_e_s_t _N_e_i_g_h_b_o_r _C_l_a_s_s_i_f_i_e_r

_D_e_s_c_r_i_p_t_i_o_n:

     Performs k-nearest neighbor classification of a test set using a
     training set.  For each row of the test set, the k nearest
     training set vectors (according to Minkowski distance) are found, 
     and the classification is done via the maximum of summed kernel
     densities. In addition even ordinal and continuous variables can
     be predicted.

_U_s_a_g_e:

     kknn(formula = formula(train), train, test, na.action = na.omit(), k = 7, distance = 2, kernel = "triangular", contrasts = c('unordered' = "contr.dummy", ordered = "contr.ordinal"))

_A_r_g_u_m_e_n_t_s:

 formula: A formula object.

   train: Matrix or data frame of training set cases.

    test: Matrix or data frame of test set cases.

na.action: A function which indicates what should happen when the data
          contain 'NA's.

       k: Number of neighbors considered.

distance: Parameter of Minkowski distance.

  kernel: Kernel to use. Possible choices are "rectangular" (which is
          standard unweighted knn) , "triangular", "epanechnikov" (or
          beta(2,2)),  "biweight" (or beta(3,3)), "triweight" (or
          beta(4,4)), "cos", "inv" and "gaussian".

contrasts: A vector containing the 'unordered' and 'ordered' contrasts
          to use.

_D_e_t_a_i_l_s:

     This nearest neighbor method expands knn in several directions.
     First it can be used not only for classification, but also for
     regression and ordinal classification. Second it uses kernel
     functions to weight the neighbors according to their distances. In
     fact, not only kernel functions but every monotonic decreasing
     function  f(x) for all x>0 will work fine.

_V_a_l_u_e:

     'kknn' returns a list-object of class 'kknn' including the
     components  

fitted.values: Vector of predictions.

      CL: Matrix of classes of the k nearest neighbors.

       W: Matrix of weights of the k nearest neighbors.

       D: Matrix of distances of the k nearest neighbors.

    prob: Matrix of predicted class probabilities.

response: Type of response variable, one of _continuous_, _nominal_ or
          _ordinal_.

distance: Parameter of Minkowski distance.

    call: The matched call.

   terms: The 'terms' object used.

_A_u_t_h_o_r(_s):

     Klaus P. Schliep K.P.Schliep@massey.ac.nz 
      Klaus Hechenbichler

_R_e_f_e_r_e_n_c_e_s:

     Hechenbichler K. and Schliep K.P. (2004)  _Weighted
     k-Nearest-Neighbor Techniques and Ordinal Classification_,
     Discussion Paper 399, SFB 386, Ludwig-Maximilians University
     Munich (<URL:
     http://www.stat.uni-muenchen.de/sfb386/papers/dsp/paper399.ps>)

_S_e_e _A_l_s_o:

     'train.kknn', 'simulation', 'knn' and 'knn1'

_E_x_a_m_p_l_e_s:

     library(kknn)

     data(iris)
     m <- dim(iris)[1]
     val <- sample(1:m, size = round(m/3), replace = FALSE, prob = rep(1/m, m)) 
     iris.learn <- iris[-val,]
     iris.valid <- iris[val,]
     iris.kknn <- kknn(Species~., iris.learn, iris.valid, distance = 1, kernel = "triangular")
     summary(iris.kknn)
     fit <- fitted(iris.kknn)
     table(iris.valid$Species, fit)
     pcol <- as.character(as.numeric(iris.valid$Species))
     pairs(iris.valid[1:4], pch = pcol, col = c("green3", "red")[(iris.valid$Species != fit)+1])

     data(ionosphere)
     ionosphere.learn <- ionosphere[1:200,]
     ionosphere.valid <- ionosphere[-c(1:200),]
     fit.kknn <- kknn(class ~ ., ionosphere.learn, ionosphere.valid)
     table(ionosphere.valid$class, fit.kknn$fit)
     (fit.train1 <- train.kknn(class ~ ., ionosphere.learn, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 1))
     table(predict(fit.train1, ionosphere.valid), ionosphere.valid$class)
     (fit.train2 <- train.kknn(class ~ ., ionosphere.learn, kmax = 15, kernel = c("triangular", "rectangular", "epanechnikov"), distance = 2))
     table(predict(fit.train2, ionosphere.valid), ionosphere.valid$class)

