0
$\begingroup$

I'm using a linear model to predict a dependent variable (the score to a test) starting by the age of a person. As shown in the figure below, the variability decreases when the age increases (data are simulated!).

enter image description here

Since this characteristic trend of the variance, I'm using the Generalized Least Squares method implemented in R in the function gls() of the package nlme, by using the varExp variance function:

library("nlme")
vf <- varExp(form=~age)
fit <- gls(score~age, data=dataset, weights=vf)

The variance function provides a good distribution of residuals.

enter image description here

Now, I need to estimate, for some value of age, the corresponding value of the residual variance. However, the function predict() extracts only the fitted values, not the variances. There is a method to estimated the residual variance of each age, starting by the model?

$\endgroup$
1
  • $\begingroup$ see the ?varWeights function, although I don't think it will predict for new values of age ... $\endgroup$
    – Ben Bolker
    Commented May 24, 2016 at 14:14

1 Answer 1

2
$\begingroup$

Looking at the help page for varExp, you can see that the function is defined as s2(v) = exp(2* t * v). Where t is the variance function coefficient and v is the covariate (in your case age).

fit$modelStruct$varStruct should give you the parameter estimate for your variance coefficient. You can plot the variance against age by using plot(y= exp( 2* fit$modelStruct$varStruct * dataset$age),x= dataset$age)

Of course you can enter any value for age into exp( 2* fit$modelStruct$varStruct * age) to get a variance estimate for that particular age. Hope this answers your question.

$\endgroup$
1
  • $\begingroup$ Dear Niek does your solution apply to this question as well? $\endgroup$ Commented Nov 1, 2020 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.