2016-11-28 7 views
2
library(lmPerm) 
x <- lmp(formula = a ~ b * c + d + e, data = df, perm = "Prob") 

summary(x) # truncated output, I can see `NA` rows here! 

#Coefficients: (1 not defined because of singularities) 
#     Estimate Iter Pr(Prob) 
#b     5.874 51 1.000 
#c    -30.060 281 0.263 
#b:c     NA NA  NA 
#d1    -31.333 60 0.633 
#d2    33.297 165 0.382 
#d3    -19.096 51 1.000 
#e     1.976 NA  NA 

我想拔出Pr(Prob)结果一切,但系数表在秩不足时没有NA行;如何插入它们?

y <- summary(x)$coef[, "Pr(Prob)"] 

#(Intercept)   b   c   d1   d2 
# 0.09459459 1.00000000 0.26334520 0.63333333 0.38181818 
#   d3   e 
# 1.00000000   NA 

这不是我想要的。我也需要b:c排,在正确的位置。

我会从上面的喜欢将是输出的一个例子:

# (Intercept)   b   c b:c   d1   d2 
# 0.09459459 1.00000000 0.26334520  NA 0.63333333 0.38181818 
#   d3   e 
# 1.00000000   NA 

我也想拉出对应于每个可变的Iter柱。谢谢。

回答

3

lmp基于lmsummary.lmp也表现得像summary.lm,所以我会先用lm为了说明,那么说明我们能为lmp这样做。


lmsummary.lm

?summary.lm读取并注意以下返回值:

coefficients: a p x 4 matrix with columns for the estimated 
       coefficient, its standard error, t-statistic and 
       corresponding (two-sided) p-value. Aliased coefficients are 
       omitted. 

    aliased: named logical vector showing if the original coefficients are 
       aliased. 

当你有秩亏模型,NA系数被省略系数表,它们被称为aliased变量。请看下面的小,可重复的例子:

set.seed(0) 
zz <- xx <- rnorm(10) 
yy <- rnorm(10) 
fit <- lm(yy ~ xx + zz) 

coef(fit) ## we can see `NA` here 
#(Intercept)   xx   zz 
# 0.1295147 0.2706560   NA 

a <- summary(fit) ## it is also printed to screen 
#Coefficients: (1 not defined because of singularities) 
#   Estimate Std. Error t value Pr(>|t|) 
#(Intercept) 0.1295  0.3143 0.412 0.691 
#xx   0.2707  0.2669 1.014 0.340 
#zz    NA   NA  NA  NA 

b <- coef(a) ## but no `NA` returned in the matrix/table 
#    Estimate Std. Error t value Pr(>|t|) 
#(Intercept) 0.1295147 0.3142758 0.4121051 0.6910837 
#xx   0.2706560 0.2669118 1.0140279 0.3402525 

d <- a$aliased 
#(Intercept)   xx   zz 
#  FALSE  FALSE  TRUE 

如果你想垫NA行系数表/矩阵,我们可以做

## an augmented matrix of `NA` 
e <- matrix(nrow = length(d), ncol = ncol(b), 
      dimnames = list(names(d), dimnames(b)[[2]])) 
## fill rows for non-aliased variables 
e[!d] <- b 

#    Estimate Std. Error t value Pr(>|t|) 
#(Intercept) 0.1295147 0.3142758 0.4121051 0.6910837 
#xx   0.2706560 0.2669118 1.0140279 0.3402525 
#zz     NA   NA  NA  NA 

lmpsummary.lmp

什么都不需要改变。

library(lmPerm) 
fit <- lmp(yy ~ xx + zz, perm = "Prob") 
a <- summary(fit) ## `summary.lmp` 
b <- coef(a) 

#    Estimate Iter Pr(Prob) 
#(Intercept) -0.0264354 241 0.2946058 
#xx   0.2706560 241 0.2946058 

d <- a$aliased 
#(Intercept)   xx   zz 
#  FALSE  FALSE  TRUE 

e <- matrix(nrow = length(d), ncol = ncol(b), 
      dimnames = list(names(d), dimnames(b)[[2]])) 
e[!d] <- b 

#    Estimate Iter Pr(Prob) 
#(Intercept) -0.0264354 241 0.2946058 
#xx   0.2706560 241 0.2946058 
#zz     NA NA  NA 

如果,要提取IterPr(Prob),只是做

e[, 2] ## e[, "Iter"] 
#(Intercept)   xx   zz 
#  241   241   NA 

e[, 3] ## e[, "Pr(Prob)"] 
#(Intercept)   xx   zz 
# 0.2946058 0.2946058   NA 
+0

完美解决方案。非常感谢。 – Gotmadstacks

相关问题