Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R variogram plot in lme vs lm?

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
like image 212
Jaood Avatar asked Mar 19 '26 04:03

Jaood


1 Answers

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

like image 104
Marco Sandri Avatar answered Mar 21 '26 20:03

Marco Sandri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!