繁体   English   中英

在lapply中使用seq_along

[英]Use seq_along in lapply

折叠交叉验证方法,以比较使用正向选择拟合的残差19个模型。 我被困在最后一步。 代码是这样的:

library(ISLR)
summary(Hitters)  # Use the dataset of Hitters
Hitters = na.omit(Hitters)

library(leaps)

set.seed(11)
folds = sample(rep(1:10,length=nrow(Hitters)))  # used for cross validation later
table(folds)
cv.errors = matrix(NA,10,19)   
# store the errors from 10 validations, each contains an error for a model

# write a prediction function
predict.regsubsets = function(object,newdata,id,...){
  form = as.formula(object$call[[2]])   # extract the formula
  mat = model.matrix(form,newdata)      # extract the exploratory data
  coefi = coef(object,id=id)            # coefficients for the ith model
  return(mat[,names(coefi)]%*%coefi)    # manually get the predicted value
}
# write a function to extract the Mean of squared root of residuals
error = function(object,newdata,origin,num,...){
  pred = lapply(seq_along(1:num),function(x){predict.regsubsets(object,newdata,id=x)})
  sapply(pred,function(x){mean((x-origin)^2)})
}

# this gives error: $ operator is invalid for atomic vectors 
lapply(seq_along(1:10),function(X){
  best.fit = regsubsets(Salary~.,data=Hitters[folds!=X,],nvmax=19,method="forward")
  cv.errors[X,]=error(best.fit,newdata=Hitters[folds==X,],origin=Hitters$Salary[folds==X],num=19)
})

# this works well, except for being slow...
for(X in 1:10){
  best.fit = regsubsets(Salary~.,data=Hitters[folds!=X,],nvmax=19,method="forward")
  cv.errors[X,]=error(best.fit,newdata=Hitters[folds==X,],origin=Hitters$Salary[folds==X],num=19)
}

谢谢!

这可能是一个范围界定问题。 没有尝试过,但是请尝试以下操作:

lapply(1:10,function(X){
  best.fit = regsubsets(Salary~.,data=Hitters[folds!=X,],nvmax=19,method="forward")
  cv.errors[X,] <- error(best.fit,newdata=Hitters[folds==X,],origin=Hitters$Salary[folds==X],num=19)
})

这里唯一的变化是赋值运算符

另外,不确定是否可以解决您的问题,但是:您真的需要在这里使用seq_along吗? 只要1:10就足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM