Question

I am working in R, with a package called lme4.

Performing a model:

lmer.rasch <- lmer(Response ~ item -1 + (1|STIDSTD),family=binomial, data=exampledata)

gets me output in the console as shown at the end of the post. I wanted to copy this, what appears to be a table, to excel, or ultimately word, recognising the separate columns and rows. Ctrl-C/Ctrl-V to excel does recognise the rows, yet not the columns.

Using write.csv(lmer.rasch) gives the error:

Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class 'structure("mer", package = "lme4")' into a data.frame

Is this a problem within the package, or just a general problem with me using the write function wrongly, or R not actually separating this output into columns?

    Fixed effects:
                        Estimate Std. Error z value Pr(>|z|)    
    variableamoeba       -2.7529     0.3000  -9.175  < 2e-16 ***
    variablebacterium    -2.3937     0.2244 -10.668  < 2e-16 ***
    variableleech         0.5578     0.1693   3.294 0.000987 ***
    variablecentipede     1.7012     0.1909   8.911  < 2e-16 ***
    variablelizard       -4.1836     0.4090 -10.229  < 2e-16 ***
    variabletapeworm     -1.3697     0.1841  -7.439 1.01e-13 ***
    variablehead lice     1.1803     0.1777   6.643 3.07e-11 ***
    variablemaggot        0.8819     0.1740   5.068 4.03e-07 ***
    variableant           2.5971     0.2332  11.137  < 2e-16 ***
    variablemoth          2.5389     0.2305  11.016  < 2e-16 ***
    variablemosquito      4.1270     0.3984  10.359  < 2e-16 ***
    variableearthworm    -0.3113     0.1675  -1.858 0.063106 .  
    variablecaterpillar   0.7278     0.1706   4.265 2.00e-05 ***
    variablescorpion     -3.1011     0.2748 -11.286  < 2e-16 ***
    variablesnail        -1.4499     0.1861  -7.791 6.66e-15 ***
    variablespider        0.4913     0.1681   2.923 0.003469 ** 
    variablegrasshopper   1.9167     0.1986   9.650  < 2e-16 ***
    variabledust mite     0.5767     0.1701   3.391 0.000696 ***
    variabletarantula    -0.7640     0.1734  -4.406 1.05e-05 ***
    variabletermite       1.8333     0.2007   9.136  < 2e-16 ***
    variablebat          -5.2427     0.6486  -8.083 6.33e-16 ***
    variablewasp          3.0696     0.2687  11.423  < 2e-16 ***
    variablesilkworm      1.1310     0.1792   6.313 2.74e-10 ***
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
Was it helpful?

Solution

here is one way:

 x <- rnorm(100)
 y <- 1:100
 g <- c(rep("a",50),rep("b",50))

library(lme4)
mod1 <- glmer(y ~ x  + (1|g) )
summary(mod1)

library(memisc)
getSummary.mer(mod1)$coef
write.csv(getSummary.mer(mod1)$coef,"answer.csv")

this should give what I think your looking for, but your model is of class mer not a matrix or data.frame:

 class(mod1)
[1] "mer"
attr(,"package")
[1] "lme4"

class(getSummary.mer(mod1)$coef)
[1] "matrix"

which is why you cannot just write to a file as you wanted.


EDIT


also now:

summary(mod1)$coefficients  
write.csv( summary(mod1)$coefficients )

OTHER TIPS

I follow a three easy steps to copy and paste from the R-studio console to excel and maintain/recover the column structure:

  1. Copy text from R-Studio console.

  2. Paste the contents into a cell in Excel.

  3. Click on the (single) cell that I pasted the data into and select the "text to columns" option from the data tab (or Alt+D+E). This launches the Text Import Wizard which I have found works pretty well in figuring out columns from R-Studio pastes based on the text width (click "next" a lot).

This works form me (using lme4):

    coeffs <- coef(summary(your.model)) # get estimates, etc...
    p <- pnorm(abs(coeffs[, "t value"]), lower.tail = FALSE) * 2 # add the much disputed p-values

    coeffsp <- cbind(coeffs, "p value" = round(p,3)) # combine it into one object
    write.csv(coeffsp, "coeffsp.csv") # export
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top