简体   繁体   English

如何将我构建的模型拟合到另一个数据集并获得残差?

[英]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.

相关问题 如何从装有fracdiff软件包的ARFIMA模型中获取残差? - How to obtain residuals from ARFIMA model fit with fracdiff package? 为什么我在R中的线性模型的残差值比我用来创建它的数据集少一个值? - Why would my residuals for a linear model in R have one less value than the data set I used to create it? 如何从R中的重复测量方差分析模型中获得残差 - How to get residuals from Repeated measures ANOVA model in R 如何在R中的数据帧缺少数据的情况下估计自回归模型AR(2)的残差 - How to estimate Residuals of Autoregressive Model AR(2)with missing data for dataframe in R 拟合多元线性回归线并从R中的另一个数据计算残差 - Fit Multiple Linear Regression line and calculate residuals from another data in R 如何在 R 中创建显示预测模型、数据和残差的图表 - How to create a graph showing the predictive model, data and residuals in R R编程:如何获得残差输出到变量中或充当数据框? - R programming: How can I get residuals output in a variable or to act like a data frame? 如何从混合模型(lmer)中提取学生化残差? - How can I extract studentized residuals from mixed model (lmer)? 如何恢复 glmer model 的残差 - How to recover the residuals of glmer model 如何使用固定效果获取面板数据中的各个系数和残差 - How to get individual coefficients and residuals in panel data using fixed effects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM