rreplacegsubmixed-modelsnlme

r replace part of string from lme S4 object


This question is about how to remove a part of the string from S4 lme model summary object.

Suppose this is my model.

# ----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

I like to remove the word "Day" at the beginning of the fixed effects model summary

Expecting my fixed effects summary terms to look like this.

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

I have tried

 summary(fm1) <- gsub("Days", "", summary(fm1))

I received an error saying, this object fm1 is of class S4 and gsub will not work. So any suggestion for how to exclude the word "Days" from the model summary terms is appreciated. Thanks.


Solution

  • Most summary() functions return a result that you can modify before printing. So instead of running summary(fm1), save the result of that call using

    summ <- summary(fm1)
    

    and edit the names before printing. You can look at ?summary.lme to find the names of the components to edit, or just look at names(summ) and guess. This works for me:

    rownames(summ$tTable) <- sub("Days", "", rownames(summ$tTable))
    rownames(summ$corFixed) <- sub("Days", "", rownames(summ$corFixed))
    colnames(summ$corFixed) <- sub("Days", "", colnames(summ$corFixed))
    summ