rextractlsmeans

Extracting elements of lsmobj from lsmeans package in R


I wonder how to get the element of lsmobj from lsmeans package in R.

require(lsmeans)
fiber.lm <- lm(strength ~ diameter + machine, data = fiber)
fiber.lsm <- lsmeans (fiber.lm, "machine")
fiber.lsm
machine   lsmean        SE df lower.CL upper.CL
 A       40.38241 0.7236252 11 38.78972 41.97510
 B       41.41922 0.7444169 11 39.78077 43.05767
 C       38.79836 0.7878785 11 37.06426 40.53247

Confidence level used: 0.95 


str(fiber.lsm)
'lsmobj' object with variables:
    machine = A, B, C

I want extract lsmeans and SE columns of fiber.lsm.


Solution

  • fiber.lsm is an S4 object that cannot be subsetted. But you can use the summary of the object. It returns a data frame of the model results.

    s <- summary(fiber.lsm)
    
    class(s)
    # [1] "summary.ref.grid" "data.frame"   
    
    s[c("lsmean", "SE")]
    #   lsmean        SE
    # 40.38241 0.7236252
    # 41.41922 0.7444169
    # 38.79836 0.7878785
    
    c(s[c("lsmean", "SE")])
    # $lsmean
    # [1] 40.38241 41.41922 38.79836
    #
    # $SE
    # [1] 0.7236252 0.7444169 0.7878785