如何将我构建的模型拟合到另一个数据集并获得残差? [英] How to fit a model I built to another data set and get residuals?

查看:42
本文介绍了如何将我构建的模型拟合到另一个数据集并获得残差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为数据 A 拟合了一个混合模型,如下所示:

I fitted a mixed model to Data A as follows:

model <- lme(Y~1+X1+X2+X3, random=~1|Class, method="ML", data=A)

接下来,我想看看模型如何拟合数据 B 并获得估计的残差.R 中是否有我可以使用的函数?

Next, I want to see how the model fits Data B and also get the estimated residuals. Is there a function in R that I can use to do so?

(我尝试了以下方法,但得到了所有新系数.)

(I tried the following method but got all new coefficients.)

model <- lme(Y~1+X1+X2+X3, random=~1|Class, method="ML", data=B)

推荐答案

第二次尝试使用 data=B 时得到新系数的原因是 lme 函数> 使用您提供的公式返回适合您的数据集的模型,并将该模型存储在您选择的变量 model 中.

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.

要获取有关模型的更多信息,您可以键入 summary(model_name).nlme 库包含一个名为 predict.lme 的方法,它允许您根据拟合模型进行预测.您可以键入 predict(my_model) 以使用原始数据集获取预测,或键入 predict(my_model, some_other_data) 如上所述以使用该模型生成预测,但使用不同的数据集.

To get more information about a model you can type summary(model_name). the nlme library includes a method called predict.lme which allows you to make predictions based on a fitted model. 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,some_other_data) - some_other_data$dependent_var,或者在你的情况下使用 predict(model,B) - B$Y.

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.

这篇关于如何将我构建的模型拟合到另一个数据集并获得残差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆