【R语⾔学习笔记】关于提取各类模型值的意外发现
之前在做各类回归⽅程和检验的时候,针对模型⾥⾯的值的提取总是有⼀种碰运⽓的成本,⽐如在做t检验的时候想提取⾥⾯的⾃由度,随便举个例⼦,基于mtcars这个数据集
a<-t.test(mtcars$vs,mtcars$cyl)
风雨凄凄结果为
Welch Two Sample t-test
data: mtcars$vs and mtcars$cyl
t = -17.528, df = 35.907, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to0
95 percent confidence interval:
-6.415358 -5.084642
sample estimates:
mean of x mean of y
0.4375 6.1875
⾥⾯其实是有df=35.907这个字段的,但是不能每次看到后在⼿⼯提取,之前的做法是针对这类名称,直接⽤a$df去看,但是其实这个字段储存在parameter⾥,⽐如
a$parameter
df
35.90693
那么问题来了,我怎么知道哪个参数储存在哪⾥呢?
下⾯意外的⽤到了str函数。
⽐如针对刚才的t检验结果a,⽤str
str(a)
List of9
$ statistic : Named num -17.5
..- attr(*, "names")= chr "t"小野猫舞蹈视频
$ parameter : Named num 35.9
..- attr(*, "names")= chr "df"
$ p.value : num 3.5e-19
$ conf.int : atomic [1:2] -6.42 -5.08
..- attr(*, "conf.level")= num 0.95
$ estimate : Named num [1:2] 0.4386.188
..- attr(*, "names")= chr [1:2] "mean of x""mean of y"
$ null.value : Named num 0
..- attr(*, "names")= chr "difference in means"
$ alternative: chr "two.sided"put词组
$ method : chr "Welch Two Sample t-test"
$ data.name : chr "mtcars$vs and mtcars$cyl"
- attr(*, "class")= chr "htest"
看到有各类的参数,储存在$后的字段⾥,⽐如我要提取p值,直接输⼊
a$p.value
[1] 3.500725e-19
就能看到p值为3.500725e-19。
同理,我做⼀个⽅差分析,⽐如就这个mtcars数据集了
fit.a<-aov(mpg~am,data=mtcars)
summary(fit.a)
Df Sum Sq Mean Sq F value Pr(>F)
am 1 405.2 405.2 16.86 0.000285 ***
Residuals 30 720.9 24.0
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1看这个⽅差分析都有什么参数:
str(fit.a)
List of 12
$ coefficients : Named num [1:2] 17.157.24
..- attr(*, "names")= chr [1:2] "(Intercept)""am"
$ residuals : Named num [1:32] -3.39 -3.39 -1.594.
..- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ effects : Named num [1:32] -113.65 -20.13 -0.644.
..- attr(*, "names")= chr [1:32] "(Intercept)""am"""""...
$ rank : int 2
$ fitted.values: Named num [1:32] 24.424.424.417.
..- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ assign : int [1:2] 01
$ qr :List of 5
..$ qr : num [1:32, 1:2] -5.6570.1770.1770.
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
.. .. ..$ : chr [1:2] "(Intercept)""am"
.
. ..- attr(*, "assign")= int [1:2] 01
..$ qraux: num [1:2] 1.181.18
..$ pivot: int [1:2] 12
..$ tol : num 1e-07
..$ rank : int 2
..- attr(*, "class")= chr "qr"
$ df.residual : int 30
$ xlevels : Named list()
$ call : language aov(formula = mpg ~ am, data = mtcars)
$ terms :Class 'terms', 'formula' language mpg ~ am
.. ..- attr(*, "variables")= language list(mpg, am)
.
. ..- attr(*, "factors")= int [1:2, 1] 01
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "mpg""am"
.. .. .. ..$ : chr "am"
competitions.. ..- attr(*, "term.labels")= chr "am"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "respon")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(mpg, am)
.. ..- attr(*, "dataClass")= Named chr [1:2] "numeric""numeric"
.
. .. ..- attr(*, "names")= chr [1:2] "mpg""am"
$ model :'data.frame': 32 obs. of 2 variables:
..$ mpg: num [1:32] 212122.821.418.718.114.324.422.
..$ am : num [1:32]
..- attr(*, "terms")=Class 'terms', 'formula' language mpg ~ am
.. .. ..- attr(*, "variables")= language list(mpg, am)梦见自己离婚
.. .. ..- attr(*, "factors")= int [1:2, 1] 01
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "mpg""am"
.. .. .. .. ..$ : chr "am"
.. .. ..- attr(*, "term.labels")= chr "am"
.
. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "respon")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(mpg, am)
.. .. ..- attr(*, "dataClass")= Named chr [1:2] "numeric""numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "mpg""am"
- attr(*, "class")= chr [1:2] "aov""lm"
可以看到有12个参数,⽐如我想看下相关系数:
社保基数如何计算
fit.a$coefficients
(Intercept) am
17.1473687.244939
⽽且前⾯的截距就是数字,还可以计算
fit.a$coefficients[1]*5
85.73684
同理,弄⼀个logistic回归的⼴义线性模型
fit.b<-glm(am~mpg+gear,data=mtcars,family=quasibinomial())
summary(fit.b)
结果为:
Call:
glm(formula = am ~ mpg + gear, family = quasibinomial(), data = mtcars)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.68311 -0.00003 -0.00002 0.04042 1.17990
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -88.2992 7928.1434 -0.011 0.9912
mpg 0.3366 0.1403 2.399 0.0231 *
gear 20.3062 1982.0355 0.010 0.9919
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for quasibinomial family taken to be 0.3263161)
Null deviance: 43.230 on 31 degrees of freedom
Residual deviance: 11.659 on 29 degrees of freedom
AIC: NA
Number of Fisher Scoring iterations: 19
看到mpg有点显著性,那么我想要提取这个相关系数,看到⼴义模型的参数更为复杂
str(fit.b)
List of 30
$ coefficients : Named num [1:3] -88.2990.33720.306
..- attr(*, "names")= chr [1:3] "(Intercept)""mpg""gear"
$ residuals : Named num [1:32] 2.012.011.55 -1 -1...
..- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ fitted.values : Named num [1:32] 4.99e-014.99e-016.46e-011.
.
.- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ effects : Named num [1:32] -0.448411.37014 -0.005850.
..- attr(*, "names")= chr [1:32] "(Intercept)""mpg""gear"""...
$ R : num [1:3, 1:3] -1.4100 -30.
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:3] "(Intercept)""mpg""gear"
.. ..$ : chr [1:3] "(Intercept)""mpg""gear"
$ rank : int 3
$ qr :List of 5
..$ qr : num [1:32, 1:3] -1.413.56e-013.40e-014.
.. ..- attr(*, "dimnames")=List of 2
.
. .. ..$ : chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
.. .. ..$ : chr [1:3] "(Intercept)""mpg""gear"
..$ rank : int 3
..$ qraux: num [1:3] 1.361.091世界上最长的城墙
..$ qraux: num [1:3] 1.361.091
..$ pivot: int [1:3] 123
..$ tol : num 1e-11
..- attr(*, "class")= chr "qr"
$ family :List of 11
..$ family : chr "quasibinomial"
..$ link : chr "logit"
.
.$ linkfun :function (mu)
..$ linkinv :function (eta)
..$ variance :function (mu)
..$ sids:function (y, mu, wt)
..$ aic :function (y, n, mu, wt, dev)
..$ mu.eta :function (eta)
..$ initialize: expression({ if (NCOL(y) == 1) { if (is.factor(y)) y <- y != levels(y)[1L] n <- rep.int(1, nobs) if (any(y < 0 | y > ..$ validmu :function (mu)
..$ valideta :function (eta)
..- attr(*, "class")= chr "family"
$ linear.predictors: Named num [1:32] -0.00586 -0.005860.60003 -20.1774 -
..- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ deviance : num 11.7
$ aic : num NA
$ null.deviance : num 43.2
德能勤绩廉述职报告$ iter : int 19
$ weights : Named num [1:32] 2.50e-012.50e-012.29e-014.
..- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ prior.weights : Named num [1:32]
..- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ df.residual : int 29
$ df.null : int 31
$ y : Named num [1:32]
.
.- attr(*, "names")= chr [1:32] "Mazda RX4""Mazda RX4 Wag""Datsun 710""Hornet 4 Drive"...
$ converged : logi TRUE
$ boundary : logi FALSE
$ model :'data.frame': 32 obs. of 3 variables:
..$ am : num [1:32]
..$ mpg : num [1:32] 212122.821.418.718.114.324.422.
..$ gear: num [1:32]
..- attr(*, "terms")=Class 'terms', 'formula' language am ~ mpg + gear
.. .. ..- attr(*, "variables")= language list(am, mpg, gear)
.. .. ..- attr(*, "factors")= int [1:3, 1:2] 010001
.. .. .. ..- attr(*, "dimnames")=List of 2
.
. .. .. .. ..$ : chr [1:3] "am""mpg""gear"
.. .. .. .. ..$ : chr [1:2] "mpg""gear"
.. .. ..- attr(*, "term.labels")= chr [1:2] "mpg""gear"
.. .. ..- attr(*, "order")= int [1:2] 11
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "respon")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(am, mpg, gear)
.. .. ..- attr(*, "dataClass")= Named chr [1:3] "numeric""numeric""numeric"
.. .. .. ..- attr(*, "names")= chr [1:3] "am""mpg""gear"
$ call : language glm(formula = am ~ mpg + gear, family = quasibinomial(), data = mtcars)
$ formula :Class 'formula' language am ~ mpg + gear
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
$ terms :Class 'terms', 'formula' language am ~ mpg + gear
.. ..- attr(*, "variables")= language list(am, mpg, gear)
.. ..- attr(*, "factors")= int [1:3, 1:2] 010001
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:3] "am""mpg""gear"
.. .. .. ..$ : chr [1:2] "mpg""gear"
.. ..- attr(*, "term.labels")= chr [1:2] "mpg""gear"
.. ..- attr(*, "order")= int [1:2] 11
.. ..- attr(*, "intercept")= int 1
.
. ..- attr(*, "respon")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(am, mpg, gear)