R variogram plot in lme vs lm?

769 views Asked by At

If I use the lme function in the package nlme and write

m <- lme(y ~ Time, random = ~1|Subject)

and then write

Variogram(m, form = ~Time|Subject)

it produces the variogram no problem.

However, if I use lm without the random effect,

m <- lm(y ~ Time)

and write

Variogram(m, form = ~Time)

it produces

Error in Variogram.default(m, form = ~Time) : 
  argument "distance" is missing, with no default

What's going on? Why does it need a distance when I fit a lm, when it didn't need it before with lme?

How then does one plot a Variogram without needing to specify "Distance"? I have the same problem using other modelling methods: glm, gam, gamm, etc.

EDIT:

You can verify all of this yourself using e.g. the BodyWeight data in nlme.

> m <- lm(weight ~ Time, data = BodyWeight)
> Variogram(m, form =~Time)
Error in Variogram.default(m, form = ~Time) : 
  argument "distance" is missing, with no default
1

There are 1 answers

0
Marco Sandri On

In nlme there is a Variogram.lme method function for an lme fit, but there is not an equivalent method for lm models.

You can use Variogram.default as follows:

library(nlme)
mod1 <- lm(weight ~ Time, data = BodyWeight)
n <- nrow(BodyWeight)
variog <- Variogram(resid(mod1), distance=dist(1:n))
head(variog)

############
      variog dist
1 17.4062805    1
2 23.1229516    2
3 29.6500135    3
4 15.6848617    4
5  3.1222878    5
6  0.9818238    6

We can also plot the variogram:

plot(variog)

enter image description here