2011-05-03 32 views
0

我想知道如何在摘要中刪除因子名稱。例如,我有單位性別(M,W)。當我打印出來的總結變量的名稱是:如何在R迴歸摘要中刪除因子名稱R

genderM 

但我想看到的只是

M 

是否有可能計算時,指令R擺脫factornames的線性模型的總結?

+2

是。您可以調整適當的彙總功能(例如lm的summary.lm)以僅打印級別而不是因子名稱。但是當你有一個可變的color.mother和一個color.child,都有白色和棕色的水平時,你可能想重新考慮你的想法。 R中的大多數設計選擇都有很好的理由... – 2011-05-03 22:09:29

回答

0

這也適用於(與Andrie的例子):

outp <- summary(fit) 
attr(outp$coefficients, "dimnames")[[1]][2] <- levels(group)[2] 
outp 

結果:

> outp 

Call: 
lm(formula = weight ~ group) 

Residuals: 
    Min  1Q Median  3Q  Max 
-1.0710 -0.4938 0.0685 0.2462 1.3690 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 5.0320  0.2202 22.850 9.55e-15 *** 
Trt   -0.3710  0.3114 -1.191 0.249  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.6964 on 18 degrees of freedom 
Multiple R-squared: 0.07308, Adjusted R-squared: 0.02158 
F-statistic: 1.419 on 1 and 18 DF, p-value: 0.249 
3

您可以留意@Joris Meys提供的非常合理的建議。 (事實上​​,我建議你做)。或者你可以使用一個缺憾一點變通帶着幾分regexcapture.output

# Set up data and fit model 
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) 
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) 
group <- gl(2,10,20, labels=c("Ctl","Trt")) 
weight <- c(ctl, trt) 
fit <- lm(weight ~ group) 

現在的操作:

  1. summary.lm結果使用capture.output
  2. 使用gsub代替你的因子水平的每一次出現

代碼:

# Capture output 
sfit <- capture.output(print(summary(fit))) 

gsub("groupTrt", "Trt  ", sfit) 

結果:

[1] ""                
[2] "Call:"               
[3] "lm(formula = weight ~ group)"         
[4] ""                
[5] "Residuals:"              
[6] " Min  1Q Median  3Q  Max "      
[7] "-1.0710 -0.4938 0.0685 0.2462 1.3690 "      
[8] ""                
[9] "Coefficients:"             
[10] "   Estimate Std. Error t value Pr(>|t|) "   
[11] "(Intercept) 5.0320  0.2202 22.850 9.55e-15 ***"   
[12] "Trt   -0.3710  0.3114 -1.191 0.249 "   
[13] "---"                
[14] "Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 " 
[15] ""                
[16] "Residual standard error: 0.6964 on 18 degrees of freedom"  
[17] "Multiple R-squared: 0.07308,\tAdjusted R-squared: 0.02158 "  
[18] "F-statistic: 1.419 on 1 and 18 DF, p-value: 0.249 "    
[19] ""      
+0

謝謝!這對我很有幫助! – user734124 2011-05-04 10:14:09