2
$\begingroup$

I have data of countries, years and some variables as shown below:

df <- structure(list(country = c(1, 1, 1, 2, 2, 2, 3, 3, 3), continent = structure(c(3L, 
3L, 3L, 4L, 4L, 4L, 2L, 2L, 2L), .Label = c("Africa", "Americas", 
"Asia", "Europe", "Oceania"), class = "factor"), year = c(0, 
1, 2, 0, 1, 2, 0, 1, 2), lifeExp = c(80.69, 82, 82.603, 77.218, 
78.471, 79.425, 76.81, 77.31, 78.242), pop = c(125956499L, 127065841L, 
127467972L, 58808266L, 59912431L, 60776238L, 272911760L, 287675526L, 
301139947L), gdpPercap = c(28816.58499, 28604.5919, 31656.06806, 
26074.53136, 29478.99919, 33203.26128, 35767.43303, 39097.09955, 
42951.65309)), class = "data.frame", row.names = c(NA, -9L))

  country continent year lifeExp       pop gdpPercap
1       1      Asia    0  80.690 125956499  28816.58
2       1      Asia    1  82.000 127065841  28604.59
3       1      Asia    2  82.603 127467972  31656.07
4       2    Europe    0  77.218  58808266  26074.53
5       2    Europe    1  78.471  59912431  29479.00
6       2    Europe    2  79.425  60776238  33203.26
7       3  Americas    0  76.810 272911760  35767.43
8       3  Americas    1  77.310 287675526  39097.10
9       3  Americas    2  78.242 301139947  42951.65

I am interested in looking at how countries life expectancy change with time or year, while adding another predictor -- a time-varying covariate, which is gdpPercap. I am using a step-by-step model building process by adding complexity to each successive model using the r package nlme:

randomIntercept <- lme(lifeExp ~ 1, random=~1|country, data=df, method='ML') # random intercept model
yearRI <- lme(lifeExp ~ year, random=~1|country, data=df, method='ML') # add year as fixed effect
ARModel <- update(yearRI, correlation=corAR1()) # include first-order autocorrelation
gdp_tvc <- update(ARModel, .~. + gdpPercap) # adding gdp as time-varying covariate

I am wondering if this is the correct way to add a time-varying covariate? So my last model in full form is:

gdp_tvc <- lme(lifeExp ~ year + gdpPercap, random=~1|country, data=df, correlation=corAR1(), method='ML')
$\endgroup$

1 Answer 1

4
$\begingroup$

Yes, there is nothing wrong with your approach and you can simply add a time-varying covariate as a fixed effect. The only thing to be aware of is that this covariate could account for a large proportion of the autoregressive component, which could make the model unstable or singular.

You might also want to allow for nested random effects, if you have multiple countries within a continent, as it's possible that observations will be correlated within continents.

$\endgroup$
5
  • $\begingroup$ Hi Robert, thank you for the reply. Can you elaborate more on the portion "this covariate could account for a large proportion of the autoregressive component, which could make the model unstable or singular". I don't quite understand what it means $\endgroup$
    – TYL
    Commented Aug 21, 2020 at 12:26
  • $\begingroup$ Sure. Suppose you have a variable that goes up and down during the day. An autoregressive process might be used to model this. However it might simply be, mainly, a function of the time of day. So you could use an AR structure OR you could include the time of day as a fixed effect but it you tried to do both you could run into trouble. You can, of course, and should, try both, but the model with both may not converge (or might just estimate a very small AR coefficient). $\endgroup$ Commented Aug 21, 2020 at 12:40
  • $\begingroup$ I see. I understand now. Also, I find that when I include the AR1 into my model (the ARModel in my code above), the model predicts the same intercept (time point 0) for all countries, but when I exclude it, each country has its own random intercept. Do you know what might be the reason? $\endgroup$
    – TYL
    Commented Aug 21, 2020 at 13:38
  • $\begingroup$ The ARModel actually improves the model fit (from BIC, AIC and log likelihood test), and the subsequent gdp_tvc model improves it further. Just that the intercept is all the same for all countries when the ARModel is included $\endgroup$
    – TYL
    Commented Aug 21, 2020 at 13:55
  • 1
    $\begingroup$ Please could you ask a new question about interpreting the output when you include AR(1), as it's seperate from the question you have asked above. $\endgroup$ Commented Aug 21, 2020 at 15:50

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.