这个问题是关于如何从S4 lme模型摘要对象中删除部分字符串。
假设这是我的模型。
# ----preliminaries-------------------------------------------------------
library("lme4")
library("lattice")
library("minqa")
library("nlme")
##-------------------------------------------------------------------------
data("sleepstudy")
##-------------------------------------------------------------------------
sleepstudy$Days[sleepstudy$Days == "0"] <- "Zero"
sleepstudy$Days[sleepstudy$Days == "1"] <- "One"
sleepstudy$Days[sleepstudy$Days == "2"] <- "Two"
sleepstudy$Days[sleepstudy$Days == "3"] <- "Three"
sleepstudy$Days[sleepstudy$Days == "4"] <- "Four"
sleepstudy$Days[sleepstudy$Days == "5"] <- "Five"
sleepstudy$Days[sleepstudy$Days == "6"] <- "Six"
sleepstudy$Days[sleepstudy$Days == "7"] <- "Seven"
## Model
##-------------------------------------------------------------------------
fm1 <- lme(fixed = Reaction ~ Days ,
random = ~1 | Days,
data = sleepstudy)
summary(fm1)
Linear mixed-effects model fit by REML
Data: sleepstudy
AIC BIC logLik
1855.854 1893.483 -915.9268
Random effects:
Formula: ~1 | Days
(Intercept) Residual
StdDev: 30.55409 48.61125
Fixed effects: Reaction ~ Days
Value Std.Error DF t-value p-value
(Intercept) 336.6295 32.63178 170 10.316003 0
Days9 14.2217 46.14830 0 0.308174 NaN
DaysFive -28.1110 46.14830 0 -0.609146 NaN
DaysFour -47.9801 46.14830 0 -1.039693 NaN
DaysOne -72.1337 46.14830 0 -1.563086 NaN
DaysSeven -17.8789 46.14830 0 -0.387423 NaN
DaysSix -24.4512 46.14830 0 -0.529841 NaN
DaysThree -53.6375 46.14830 0 -1.162285 NaN
DaysTwo -71.2676 46.14830 0 -1.544317 NaN
DaysZero -79.9777 46.14830 0 -1.733058 NaN
我喜欢删除fixed effects model summary
开头的“
Day”一词
期望我的固定效应摘要术语看起来像这样。
Fixed effects: Reaction ~ Days
Value Std.Error DF t-value p-value
(Intercept) 336.6295 32.63178 170 10.316003 0
9 14.2217 46.14830 0 0.308174 NaN
Five -28.1110 46.14830 0 -0.609146 NaN
Four -47.9801 46.14830 0 -1.039693 NaN
One -72.1337 46.14830 0 -1.563086 NaN
Seven -17.8789 46.14830 0 -0.387423 NaN
Six -24.4512 46.14830 0 -0.529841 NaN
Three -53.6375 46.14830 0 -1.162285 NaN
Two -71.2676 46.14830 0 -1.544317 NaN
Zero -79.9777 46.14830 0 -1.733058 NaN
我已经尝试过了
summary(fm1) <- gsub("Days", "", summary(fm1))
我收到一条错误消息,该对象
fm1
属于 S4 类,gsub 将无法工作。因此,任何关于如何从模型摘要术语中排除“天”一词的建议都是值得赞赏的。谢谢。
您可以使用
broom.mixed::tidy()
将摘要转换为小标题并进行操作:
library(tidyverse)
broom.mixed::tidy(fm1) %>%
filter(effect == "fixed") %>% # Get fixed effects only
mutate(term = gsub("Days", "", term)) %>%
rename("t-value" = "statistic")
您可以创建自己的函数来模仿
nlme:::summary.lme
函数,如下所示:
在 R 控制台中键入以下内容,并将输出复制到名为“summary.lme.R”的新脚本文件中。
nlme:::summary.lme
通过在第 1 行后插入以下行来编辑此文件:
names(fixed) <- gsub("Days", "", names(fixed))
并在
summary.lme <-
前面添加
function(object, adjustSigmma = TRUE, ...)
然后保存文件,将其源到 R 控制台,以便在当前会话中可用,然后再次运行摘要命令。
> summary(fm1)
Linear mixed-effects model fit by REML
Data: sleepstudy
AIC BIC logLik
1855.854 1893.483 -915.9268
Random effects:
Formula: ~1 | Days
(Intercept) Residual
StdDev: 30.55409 48.61125
Fixed effects: Reaction ~ Days
Value Std.Error DF t-value p-value
(Intercept) 336.6295 32.63178 170 10.316003 0
9 14.2217 46.14830 0 0.308174 NaN
Five -28.1110 46.14830 0 -0.609146 NaN
Four -47.9801 46.14830 0 -1.039693 NaN
One -72.1337 46.14830 0 -1.563086 NaN
Seven -17.8789 46.14830 0 -0.387423 NaN
Six -24.4512 46.14830 0 -0.529841 NaN
Three -53.6375 46.14830 0 -1.162285 NaN
Two -71.2676 46.14830 0 -1.544317 NaN
Zero -79.9777 46.14830 0 -1.733058 NaN
您可以使这个函数更通用,但这回答了您的问题。