How to fit a model I built to another data set and get residuals?

为君一笑 提交于 2019-12-20 01:35:29

问题


I fitted a mixed model to Data A as follows:

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. 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)

回答1:


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.

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.

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.




回答2:


You model:

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

2 predictions based on your model:

pred1=predict(model,newdata=A,type='response')
pred2=predict(model,newdata=B,type='response')

missed: A function that calculates the percent of false positives, with cut-off set to 0.5.
(predicted true but in reality those observations were not positive)

missed = function(values,prediction){sum(((prediction > 0.5)*1) != values)/length(values)}

missed(A,pred1)
missed(B,pred2)



来源:https://stackoverflow.com/questions/15213260/how-to-fit-a-model-i-built-to-another-data-set-and-get-residuals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!