如何为类创建方法?

问题描述

一个作业中,我编写了下面的函数 (learnvq),它计算一些数据点与其他标记数据点的接近度,然后根据最近的点将这些标签分配给第一组。我将此函数输出作为“learnvq”类的列表返回。

我现在需要为这个类编写一个方法,该方法将使用一个新矩阵并根据函数输出预测标签。我已经编写了该函数,它似乎工作正常,但我不知道如何将其添加为“learnvq”类的方法

n <- 100
X <- cbind(x1 = runif(n,-1.5,1.5),x2 = runif(n,1.5)) # Generate random points
y <- as.integer(rowSums(X^2)<1) # Determine whether inside the circle#
idx <- sample(100,10) # Mess up 10 class labels ...
y[idx] <- 1-y[idx] # ... by flipping the label

############################################ ############################################

第 1 部分:定义类 learnvq

learnvq <- function(X,y,K=10,eta=.25,alpha=.5,H=25) {
is <- c(sample(which(y==0),K,replace=TRUE),sample(which(y==1),replace=TRUE))
X.prototype <- X[is,]
y.prototype <- y[is]

#Function closest to find the position of my matrix which contains the closest point to a vector
closest <- function(mat,vec) { # function to calculate the closest value
dist <-  0* mat[,1] # creates the empty vector same length as number of rows

for (i in 1:nrow(mat))  # loops through the matrix
{dist[i] <- sum((mat[i,] - vec)^2)} # subtracts the vector from the row and takes the absolute value. 
Assigns the value to dist[i]

min.coord <- which.min(dist) # takes the smallest value- equates to the closest distance
return(min.coord)          # returns the position of the smallest value

}
Covariate.matrix <- X[is,] # copies covariates matrix X for use in the loops below- dont want to 
update the original above- need that too.
###put the loops here ###
for (h in 1:H){
for (i in 1:n){ 
  # finding prototype x.star in k closest to xi
  k <- closest(X.prototype,X[i,])
  #updating the X.prototype and Y.prototype with the information from k:
  if (y[i] == y.prototype[k])
    {
    X.prototype[k,] <- X.prototype[k,] + eta*(X[i,]-X.prototype[k,])
    }
  
  else   {
    X.prototype[k,] - eta*(X[i,])
  }
  }
  }

  object <- list(Covariate.matrix=Covariate.matrix,y.prototypes= y.prototype,X.prototypes.star= 
  X.prototype,y.prototypes.star = y.prototype)
  class(object) <- "learnvq"
  object
  }

  check <- learnvq(X,y)

############################################ ############################################### ######

第 2 部分:预测方法

  predict.learnvq <- function(object,New.Mat) {

  #Function closest to find the position of my matrix which contains the closest point to a vector
  closest <- function(mat,vec) { # function to calculate the closest value
  dist <-  0* mat[,1] # creates the empty vector same length as number of rows

  for (i in 1:nrow(mat))  # loops through the matrix
  {dist[i] <- sum((mat[i,] - vec)^2)} # subtracts the vector from the row. Assigns the value to 
  dist[i]

  min.coord <- which.min(dist) # takes the smallest value- equates to the closest distance
  return(min.coord)          # returns the position of the smallest value

  }

  #extracts the rows from X.new and compares them to the X.prototypes and then calculates the 
  corresponding y label
  if (missing(New.Mat)) # if statement to allow for missing X.new argument
  {New.Mat = object$Covariate.matrix} # if New.Mat is omitted it uses the covariates of the training 
  data- 

  y.proto.test<- 0*nrow(New.Mat)
  for (j in 1:nrow(New.Mat))# loops through the matrix
  {y.proto.test[j] <- closest(check$X.prototypes.star,New.Mat[j,])} # returns the y.prototype 
  position that the new rows are closest to.
  estimated.class.labels <- check$y.prototypes.star[y.proto.test] # contains the row #s of the points 
  closest to the X.new points.
  return(estimated.class.labels)

  }

  X.new <- cbind(x1=runif(10,#creates a vector of unclassified points
           x2=runif(10,1.5))
  fitted.y <- predict.learnvq(check$X.prototypes.star,X.new)

解决方法

我不知道如何将其添加为“learnvq”类的方法?

你已经做到了。通过将您的方法命名为 predict.learnqv,您已经为 predict 类创建了一个 learnqv 方法。您现在可以调用它:

result = predict(learnqv_object,other_arguments…)

...这将自动调用 predict.learnqv

如果你把这段代码写在一个包中,你还需要注意正确导出 S3 方法名称,但对于“简单”的 R 代码,以上就足够了。请阅读 Advanced R 中的 read the chapter on S3,它包含更多相关信息。