Question

My mixed model is as follows:

model <- lme(Cost~1+Units, random=~1+Units|Factory, method="ML", data=A)

I was told to apply the code below to plot residuals versus fitted values and it worked:

plot(fitted(model), resid(model))

However, it showed me an error message when I tried to plot residuals versus Units, the predictor variable.

> plot(Units(model), resid(model))
Error: could not find function "Units"
Error in plot(Units(model), resid(model)) : 
error in evaluating the argument 'x' in selecting a method for function 'plot'

What should I do to fix this problem? Thanks.

Was it helpful?

Solution

I'll extend the comment of @Didzis (which is of course true), so you'll really learn what is going on. Units is a variable in your data, not a particular name for somekind of variable like residuals or fitted values (although units as general does have that kind of meaning...), so there isn't any ready function which gives you those. You already have used Units in your first line of code when building the model: There you used argument data with value A, which told lme function that your variables used in modelling lie inside of the data frame called A.

You can access the variables in data frame by using dollar sign $ with the name of the variable you want to extract (you can check the names using function names(A)). Therefore you get your Units variable by using command A$Units, so you can plot your Units variable versus the residuals by using the command

plot(A$Units,resid(model))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top