Why I got Vcov error in calculate confidence interval for regression coefficient?

Hello, Dear all:
I am doing this regression lm by each patient ID. Then try to calculate confidence interval for coefficient. I got this error message in the end.
fitted_models = df31 %>% group_by(PatId) %>% do(model = lm(var1 ~ yr_findex, data = .))
t<- fitted_models%>%group_by(PatId) %>% do(confint(., "yr_findex", level=0.80))

Then I always this error as
Error in UseMethod("vcov") :
no applicable method for 'vcov' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"

Anyone knows why? I am a SAS newly converted R user...so still asking very simple questions here. Thanks a lot!

The problem you had with calling confint is that your . object was a dataframe rathen than an lm object.

as I dont have your data I used iris as example data.

Here is one approach

library(tidyverse)
library(purrr)

fitted_models <- iris %>%
  group_by(Species) %>%
  do(model = lm(Petal.Length ~ Petal.Width, data = .)) %>%
  ungroup()

t <- purrr::map_dfr(
  fitted_models$model,
  ~ as_tibble(confint(., "Petal.Width", level = 0.80))
)

combined <- bind_cols(fitted_models, t)
1 Like

thanks a lot! Let me try!

Why I need to use "%>% ungroup()" in the end?
I tried without this line, it also works... just want to know what that means?

It removes the group attribute, so it effect how dplyr verbs operate on the tibble.

1 Like

Right now I have another issue. Since I need to extract the coefficient from model by ID.
I did this.
fitted_models = df31 %>% group_by(PatId) %>% do(model = lm(eGFR_value ~ yr_findex, data = .))
fit_est<-fitted_models %>% tidy(model)%>% subset(term %in% "yr_findex")

In the end, I merge the CI for coefficient by ID to fit_est. If I used ungroup(). Then I can't have fit_est created correctly...
How to run one model. Then extract the slope and calculate CI as the results?

thanks a lot!

Please use iris as a representative data if you want me to run your code.

thanks, I figure it out. Only run the models first and ungroup for CI calculation in the second step.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.