2012-12-31 83 views
1

我在R中工作,其包名爲lme4將lmer輸出轉換爲word/excel

執行模式:

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

讓我在控制檯輸出,在帖子的末尾表示。我想複製這個,看起來像是一張桌子,用Excel來表達,或者最終用詞來識別單獨的列和行。 Ctrl-C/Ctrl-V到excel會識別行,但不識別列。

使用write.csv(lmer.rasch)給出了錯誤:

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

這是內包有問題,或只是我使用寫入功能錯誤,或R實際上沒有這個分離到輸出列的一般問題?

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 

回答

9

這裏是單向的:

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") 

這應該給什麼,我覺得你的尋找,但你的模型是mer類不是matrixdata.frame的:

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

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

這這就是爲什麼你不能只是寫你想要的文件。


編輯


現在也:

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

這工作確實。非常感激! – Marloes

1

我遵循三個簡單的步驟複製並從R-Studio控制檯粘貼到Excel和維護/恢復柱結構:

  1. 複製文字m R-Studio控制檯。

  2. 將分隔字符串的內容粘貼到單個單元格中。

  3. 單擊我將數據粘貼到的單個單元格,然後從數據選項卡(或Alt + D + E)中選擇「text to columns」選項。這將啓動文本導入嚮導,我發現它基於文本寬度從R-Studio粘貼中找出列的方式非常好(單擊「下一個」很多)。

0

這個作品構成了我(使用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