[英]How to fit a model I built to another data set and get residuals?
I fitted a mixed model to Data A as follows: 我在数据A中安装了一个混合模型,如下所示:
model <- lme(Y~1+X1+X2+X3, random=~1|Class, method="ML", data=A)
Next, I want to see how the model fits Data B and also get the estimated residuals. 接下来,我想看看模型如何适合数据B并获得估计的残差。 Is there a function in R that I can use to do so?
R中是否有可以使用的功能?
(I tried the following method but got all new coefficients.) (我尝试了以下方法,但获得了所有新系数。)
model <- lme(Y~1+X1+X2+X3, random=~1|Class, method="ML", data=B)
The reason you are getting new coefficients in your second attempt with data=B
is that the function lme
returns a model fitted to your data set using the formula you provide, and stores that model in the variable model
as you have selected. 您在第二次尝试使用
data=B
获得新系数的原因是函数lme
使用您提供的公式返回适合您的数据集的模型,并将该模型存储在您选择的变量model
中。
To get more information about a model you can type summary(model_name)
. 要获取有关模型的更多信息,可以键入
summary(model_name)
。 the nlme
library includes a method called predict.lme
which allows you to make predictions based on a fitted model. nlme
库包含一个名为predict.lme
的方法,它允许您根据拟合的模型进行预测。 You can type predict(my_model)
to get the predictions using the original data set, or type predict(my_model, some_other_data)
as mentioned above to generate predictions using that model but with a different data set. 您可以键入
predict(my_model)
以使用原始数据集获取预测,或者键入如上所述的predict(my_model, some_other_data)
以使用该模型生成预测但使用不同的数据集。
In your case to get the residuals you just need to subtract the predicted values from observed values. 在您获取残差的情况下,您只需要从观测值中减去预测值。 So use
predict(my_model,some_other_data) - some_other_data$dependent_var
, or in your case predict(model,B) - B$Y
. 所以使用
predict(my_model,some_other_data) - some_other_data$dependent_var
,或者在你的情况下predict(model,B) - B$Y
You model: 你模型:
model <- lme(Y~1+X1+X2+X3, random=~1|Class, method="ML", data=A)
model < - lme(Y~1 + X1 + X2 + X3,random = ~1 | Class,method =“ML”,data = A)
2 predictions based on your model: 2个基于您的模型的预测:
pred1=predict(model,newdata=A,type='response')
PRED1 =预测(模式,newdata = A,类型= '响应')
pred2=predict(model,newdata=B,type='response')PRED2 =预测(模式,newdata = B,类型= '响应')
missed: A function that calculates the percent of false positives, with cut-off set to 0.5. miss:计算误报百分比的函数,截止值设置为0.5。
(predicted true but in reality those observations were not positive) (预测是真实的,但实际上这些观察结果不是正面的)
missed = function(values,prediction){sum(((prediction > 0.5)*1) != values)/length(values)}
miss = function(values,prediction){sum(((prediction> 0.5)* 1)!= values)/ length(values)}
missed(A,pred1)
错过(A,PRED1)
missed(B,pred2)错过(B,PRED2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.