R问题中的sqrt函数

时间:2017-09-08 05:45:09

标签: r

我正计算mse中我的最小二乘拟合模型的maeR

library(boot)
k = 10
set.seed(1)

mse.ls = rep(0,times =k)
mae.ls = rep(0,times =k)
mse.lad = rep(0,times =k)
mae.lad = rep(0,times =k)
predict.ls = rep(0,times = 20)
predict.lad = rep(0,times = 20)
folds = sample(1:k,nrow(Mydata), replace = TRUE)
for(j in 1:k){
ls.fit = lm(HtVol~ Ht+Wt+Ht*Wt, data=Mydata[folds!=j,])
pred.ls = predict(ls.fit,Mydata[folds==j,])
mse.ls[j] = sqrt(mean((Mydata$HtVol[folds==j]-pred.ls)^2)) 
mae.ls[j]=mean(abs(Mydata$HtVol[folds==j]-pred.ls))
predict.ls[folds==j]=pred.ls
}

当我执行时,

mse.ls[j] = sqrt(mean((Mydata$HtVol[folds==j]-pred.ls)^2)) 

输出只有向量的最后一个值sqrt

mse.ls
[1]  1671.18130  3098.68369  9211.37483  9263.10585  1176.78916 
22022.38220  8581.95307  8209.25681  5868.43977
[10]    76.07184

如果我单独使用sqrt,那么它适用于整个矢量

sqrt(mse.ls)
[1]  40.88008  55.66582  95.97591  96.24503  34.30436 148.39940  
92.63883  90.60495  76.60574  76.07184

有人可以告诉我这里我做错了什么吗?

0 个答案:

没有答案