R 将模型总结为整洁的对象

问题描述 投票:0回答:2

这涉及到如何将一个非整洁的对象强制进入整洁的宇宙。我正在使用一个相对较新的 r 包

GLMMadaptive
来适应混合模型。

这是代码

第1步:创建数据集

set.seed(1234)
n <- 200 # number of subjects
K <- 12 # number of measurements per subject
t_max <- 14 # maximum follow-up time

# construct a dataset with the design:
DF <- data.frame(id = rep(seq_len(n), each = K),
                 time = c(replicate(n, c(0, sort(runif(K - 1, 0, t_max))))),
                 sex = rep(gl(2, n/2, labels = c("male", "female")), each = K))

# design matrices for the fixed and random effects
X <- model.matrix(~ sex * time, data = DF)
Z <- model.matrix(~ time, data = DF)

betas <- c(-2.2, -0.25, 0.24, -0.05) # fixed effects coefficients
sigma <- 0.5 # errors' standard deviation
D11 <- 1.0 # variance of random intercepts
D22 <- 0.5 # variance of random slopes

# simulate random effects
b <- cbind(rnorm(n, sd = sqrt(D11)), rnorm(n, sd = sqrt(D22)))

# linear predictor
eta_y <- as.vector(X %*% betas + rowSums(Z * b[DF$id, ]))

# simulate normal longitudinal data
DF$y <- rnorm(n * K, mean = eta_y, sd = sigma)

# assume that values below -4 are not observed, and set equal to -4
DF$ind <- as.numeric(DF$y < -4)
DF$y <- pmax(DF$y, -4)

第2步:运行模型

foo <- mixed_model(cbind(y, ind) ~ sex * time, 
                   random = ~ time | id, 
                   data = DF, 
                   family = censored.normal())

第三步:模型结果

     summary(foo)
Call:
mixed_model(fixed = cbind(y, ind) ~ sex * time, random = ~time | 
    id, data = DF, family = censored.normal())



Fit statistics:
   log.Lik      AIC      BIC
 -2377.952 4771.904 4798.291

Random effects covariance matrix:
             StdDev    Corr
(Intercept)  0.9623        
time         0.6341  0.1459

Fixed effects:
               Estimate Std.Err  z-value p-value
(Intercept)     -2.2468  0.1008 -22.2968 < 1e-04
sexfemale       -0.0376  0.1445  -0.2599 0.79491
time             0.3231  0.0638   5.0628 < 1e-04
sexfemale:time  -0.0913  0.0933  -0.9795 0.32734

log(residual std. dev.):
  Estimate Std.Err
   -0.6932  0.0175

Optimization:
method: hybrid EM and quasi-Newton
converged: TRUE 

我无法通过

tidy
传输这些模型摘要,我需要帮助。预先感谢。

r tidyverse tidy broom
2个回答
2
投票
library(broom.mixed)
tidy(foo)

         term    estimate  std.error   statistic       p.value   conf.low   conf.high
1    (Intercept) -2.24677968 0.10076694 -22.2967947 3.969327e-110 -2.4442792 -2.04928012
2      sexfemale -0.03757411 0.14454807  -0.2599419  7.949086e-01 -0.3208831  0.24573491
3           time  0.32312932 0.06382416   5.0628059  4.131305e-07  0.1980363  0.44822238
4 sexfemale:time -0.09134472 0.09325713  -0.9794932  3.273364e-01 -0.2741253  0.09143589

该模型的

broom.mixed
中似乎还没有实现随机效应提取。


1
投票

您可以将固定效应的系数表强制转换为小标题,如下所示:

library(tidyverse)

summary(foo) %>% 
  getElement("coef_table") %>% 
  as.data.frame() %>%
  rownames_to_column() %>%
  as_tibble(.name_repair = "universal")
#> # A tibble: 4 x 5
#>   rowname        Estimate Std.Err z.value   p.value
#>   <chr>             <dbl>   <dbl>   <dbl>     <dbl>
#> 1 (Intercept)     -2.25    0.101  -22.3   3.97e-110
#> 2 sexfemale       -0.0376  0.145   -0.260 7.95e-  1
#> 3 time             0.323   0.0638   5.06  4.13e-  7
#> 4 sexfemale:time  -0.0913  0.0933  -0.979 3.27e-  1
© www.soinside.com 2019 - 2024. All rights reserved.