2014-04-25 36 views
0

摺疊交叉驗證方法比較使用正向選擇擬合的殘差19個模型。我被困在最後一步。 代碼是這樣的:在lapply中使用seq_along

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) 
} 

謝謝!

+0

不知道這是否能解決您的問題,但:您真的需要在這裏使用'seq_along'嗎?只要'1:10'就夠了。 – Shambho

+0

'seq_along(1:10)'具有1:10的值,所以seq_along是多餘的。考慮使用包'cvTools'而不是自己動手。 –

+0

你在這裏有什麼問題?目前尚不清楚。 –

回答

0

這可能是一個範圍問題。沒有嘗試過,但請嘗試以下操作:

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) 
}) 

這裏唯一的變化是assignment operator

此外,不知道這是否會解決您的問題,但:你真的需要在這裏使用seq_along?只需1:10應該就夠了。