Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Predict函数返回拟合值,即使我输入了新数据_R_Validation - Fatal编程技术网

Predict函数返回拟合值,即使我输入了新数据

Predict函数返回拟合值,即使我输入了新数据,r,validation,R,Validation,我正在为自己制作模型验证测试功能 在这样做的过程中,我让 a=entire set of predictor variables in model-building set b=set of response variable in model-building set c=entire set of predictor variables in validation set d=set of response variable in validation set e=number o

我正在为自己制作模型验证测试功能

在这样做的过程中,我让

 a=entire set of predictor variables in model-building set
 b=set of response variable in model-building set
 c=entire set of predictor variables in validation set
 d=set of response variable in validation set
 e=number of column which I have an interest
这是基于《应用线性回归模型》一书,Kutner,所以我使用
库(ALSM)

在我的例子中,模型构建集是
SurgicalUnit
,验证集是
SurgicalUnitAdditional

这两个数据都由10列组成,其中从第1列到第8列是整个indep集合。变量,第9个是响应变量,第10个是日志(响应变量)

所以,
a=手术单位[,1:8];b=手术单位[,10];
c=手术加成[,1:8];d=手术加性[,10];e=c(1,2,3,8)

,因为我想适应记录的响应变量,我想回归变量x1、x2、x3和x8

(请注意,我之所以使用具有特定列数的“完整”自变量集而不是直接放置感兴趣的自变量集,是因为我需要立即在函数中获得Mallow的Cp。)

所以我的回归是,
asdf=lm(b~as.matrix(a[e]))
,问题是,我想预测用模型构建集构建的模型中的验证集。因此,我让
preds=data.frame(c[e])
和最后的
predict(asdf,newdata=preds)
predict(asdf)
相等,这意味着它是
asdf
的拟合值

为什么预测不起作用?我们会感激你的帮助

下面是我的功能

mod.valid=function(a,b,c,d,e){
asdf=lm(b~as.matrix(a[e]))              # model what you want
qwer=lm(b~as.matrix(a[1:max(e)]))       # full model in order to get Cp
mat=round(coef(summary(asdf))[,c(-3,-4)],4); mat2=matrix(0,5,2)
mat=rbind(mat,mat2); mat                  # matrix for coefficients and others(model-building)
n=nrow(anova(asdf)); m=nrow(anova(qwer))
nn=length(b)                                  # To get size of sample size
p=asdf$rank                                   # To get parameters p
cp=anova(asdf)$Sum[n] / (anova(qwer)$Mean[m]) - (nn-2*p); cp=round(cp,4)
mat[p+1,1]=p; mat[p+1,2]=cp             # adding p and Cp
rp=summary(asdf)$r.squared; rap=summary(asdf)$adj.r.squared; rp=round(rp,4); rap=round(rap,4)
mat[p+2,1]=rp; mat[p+2,2]=rap           # adding  Rp2 and Rap2
sse=anova(asdf)$Sum[n]; pre=MPV::PRESS(asdf); sse=round(sse,4); pre=round(pre,4)
mat[p+3,1]=sse; mat[p+3,2]=pre        # adding SSE and PRESS
**preds=data.frame(c[e]); predd=predict(asdf,newdata=preds)** **# I got problem here!**
mspr=sum((d-predd)^2) / length(d); mse=anova(asdf)$Mean[n]; mspr=round(mspr,4); mse=round(mse,4)
mat[p+4,1]=mse; mat[p+4,2]=mspr       # adding MSE and MSPR
aic=nn*log(anova(asdf)$Sum[n]) - nn*log(nn) + 2*p; aic=round(aic,4)
bic=nn*log(anova(asdf)$Sum[n]) - nn*log(nn) + log(nn)*p; bic=round(bic,4)
mat[p+5,1]=aic; mat[p+5,2]=bic        # adding AIC and BIC
rownames(mat)[p+1]="p&Cp"; rownames(mat)[p+2]="Rp.sq&Rap.sq"
rownames(mat)[p+3]="SSE&PRESS"; rownames(mat)[p+4]="MSE&MSPR"; rownames(mat)[p+5]="AIC&BIC"

asdf2=lm(d~as.matrix(c[e]))
qwer2=lm(d~as.matrix(c[1:max(e)]))
matt=round(coef(summary(asdf2))[,c(-3,-4)],4); matt2=matrix(0,5,2)
matt=rbind(matt,matt2); matt              # matrix for coefficients and others(validation)
n2=nrow(anova(asdf2)); m2=nrow(anova(qwer2))
nn2=length(d)                                    # To get size of sample size
p2=asdf$rank                                     # To get parameters p
cp2=anova(asdf2)$Sum[n2] / (anova(qwer2)$Mean[m2]) - (nn2-2*p2); cp2=round(cp2,4)
matt[p2+1,1]=p2; matt[p2+1,2]=cp2           # adding p and Cp
rp2=summary(asdf2)$r.squared; rap2=summary(asdf2)$adj.r.squared; rp2=round(rp2,4); rap2=round(rap2,4)
matt[p2+2,1]=rp2; matt[p2+2,2]=rap2     # adding  Rp2 and Rap2
sse2=anova(asdf2)$Sum[n]; pre2=MPV::PRESS(asdf2); sse2=round(sse2,4); pre2=round(pre2,4)
matt[p2+3,1]=sse2; matt[p2+3,2]=pre2      # adding SSE and PRESS
mse2=anova(asdf2)$Mean[n]; mse2=round(mse2,4)
matt[p2+4,1]=mse2; matt[p2+4,2]=NA        # adding MSE and MSPR, in this case MSPR=0
aic2=nn2*log(anova(asdf2)$Sum[n2]) - nn2*log(nn2) + 2*p2; aic2=round(aic2,4)
bic2=nn2*log(anova(asdf2)$Sum[n2]) - nn2*log(nn2) + log(nn2)*p2; bic2=round(bic2,4)
matt[p2+5,1]=aic2; matt[p2+5,2]=bic2      # adding AIC and BIC
mat=cbind(mat,matt); colnames(mat)=c("Estimate","Std.Error","Val.Estimate","Val.Std.Error")
print(mat)
}

此函数将为模型验证提供有用的统计信息

它返回一个包含系数p、Mallow的Cp、R.squared、R.adj.squared、SSE、PRESS、MSE、MSPR、AIC和BIC的矩阵


除了MSPR之外,对于一般给定的数据,一切都可以正常工作,因为
predict
函数不起作用!它只返回已安装的

你能试试这样的吗。您必须确保培训和测试数据具有相同的列名

x <- rnorm(100)
y <- x + rnorm(100)
df <- data.frame(x = x, y=y)
# model fitting
fit <- lm(y ~ x, data=df) 
predict(fit)

# creating new data
newx <- rnorm(50)
newdf <- data.frame(x = newx)
# making predictions
predict(fit, newdata = newdf)

x这可能与您使用矩阵,
as.matrix(a[e])
来拟合模型和数据帧,
preds=data.frame(c[e])
来测试有关吗?@DiscoSuperfly-woo这是一个合理的猜测,但是
lm
函数只接受矩阵形式,而
predict
函数只接受data.frame形式。所以我什么都做不了…不,
lm
接受数据帧。我运行了lm(b~data.frame(a[e]),它返回错误,我怎么办…?我的第一条评论可能是错误的。似乎只要你有相同的列名,无论你使用的是矩阵还是数据框都无关紧要。只要我在做多元回归,我就尝试了
df=data.frame(a=a,b=b)
其中
a=SurgicalUnit[,1:8];b=SurgicalUnit[,10]
lm(df[9]~df[1:8],data=df)
甚至不返回回归模型,说它不是它想要的类型..如果你不想安装
ALSM
软件包,你可以从[link]()获取此文件,即使你需要
SurgicalUnitAdditional
。这个
lm(df[9]~df[1:8],data=df)
不是你应该使用它的方式。用其列名代替
df[9]
df[1:8]
也是如此。例如
lm(a~,data=df)
其中
df
的第一列被命名为
a
。所有剩余列都用作预测值。df#仅使用列V1和V2作为V10的预测值。使用前40个样品配合度进行配合