lmp
基於lm
和summary.lmp
也表現得像summary.lm
,所以我會先用lm
爲了說明,那麼說明我們能爲lmp
這樣做。
lm
和summary.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
lmp
和summary.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
如果,要提取Iter
和Pr(Prob)
,只是做
e[, 2] ## e[, "Iter"]
#(Intercept) xx zz
# 241 241 NA
e[, 3] ## e[, "Pr(Prob)"]
#(Intercept) xx zz
# 0.2946058 0.2946058 NA
完美解決方案。非常感謝。 – Gotmadstacks