2

I am using a linear mixed effects model to determine the relationship between 2 variables over time, model -

data(mtcars)

# linear mixed effects model
mlme <- lme(mpg ~ wt, random = ~ 1|cyl, data = mtcars)

I then access the confidence intervals using -

conf <- intervals(mlme, level = 0.95, which = 'fixed')
conf

Returning the results -

Approximate 95% confidence intervals

 Fixed effects:
               lower      est.    upper
(Intercept) 7.431921 18.416639 29.40136
DairyTotal  2.001397  6.716849 11.43230

I want to access the values of the lower and upper limits so I can use them as variables but can't find a method to do so. If you want to access the coefficients for example you can use coef(summary(mlme)) and convert it to a dataframe.

I tried converting the result to a dataframe using data.frame(coef) but get the error:

> data.frame(conf)
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ‘"intervals.lme"’ to a data.frame

Can anyone recommend a method to access variables that are returned in this format?

John Conor
  • 722
  • 6
  • 20
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Apr 04 '23 at 20:23
  • That fair, I was hoping it was just something procedural as R returns a lot of variables are returned in this format... I will make it reproducible. – John Conor Apr 04 '23 at 20:25

2 Answers2

2

Here is an example with the mtcars dataset how we could access the lower and upper ci as variables:

library(nlme)

data(mtcars)

# linear mixed effects model
mlme <- lme(mpg ~ wt, random = ~ 1|cyl, data = mtcars)

# Confidence intervals
conf <- intervals(mlme, level = 0.95, which = 'fixed')

# Lower and upper confidence intervals as variables
lower <- conf$fixed["(Intercept)", "lower"]
upper <- conf$fixed["(Intercept)", "upper"]

[1] 25.7204
[1] 37.32919
TarJae
  • 72,363
  • 6
  • 19
  • 66
2

Another possibility would be to use broom.mixed::tidy(). Using @TarJae's example:

broom.mixed::tidy(mlme, conf.int = TRUE, effects = "fixed") |> 
     dplyr::select(term, estimate, conf.low, conf.high)
# A tibble: 2 × 4
  term        estimate conf.low conf.high
  <chr>          <dbl>    <dbl>     <dbl>
1 (Intercept)    31.5     25.7      37.3 
2 wt             -3.52    -4.99     -2.04
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453