如何读取R 的sumary()结果
思路
step 1: sum = summary(model)
step 2: sum有好多属性,直接根据属性名称引用(\()即可, 如:
+ > sum\)call 返回 model 使用的模型语句
+ > sum\(coefficients; 返回一个列表,可以继续引用,如下
+ > sum\)coefficients[1 : 12]; 返回一个列表的一个切片,还可以继续切片
+ > sum$coefficients[1 : 12][2]; 返回一个列表的一个切片的第二个元素
下面是一些 测试代码,未整理,可以大致学习一下
用'demo()'来看一些示范程序,用'help()'来阅读在线帮助文件,或
用'help.start()'通过HTML浏览器来看帮助文件。
用'q()'退出R.
[Workspace loaded from ~/.RData]
> y=c(53,434,111,38,108,48)
> x1=c(1,2,3,1,2,3)
> x2=c(1,2,1,2,1,2)
> log.glm <-glm(y~x1+x2,family = possion(link=log))
Error in possion(link = log) : 没有"possion"这个函数
> log.glm <-glm(y~x1+x2,family = possion(link=log),data=(y,x1,x2))
错误: 意外的',' in "log.glm <-glm(y~x1+x2,family = possion(link=log),data=(y,"
> dataframe <-data.frame(y,x1,x2)
> head(dataframe)
y x1 x2
1 53 1 1
2 434 2 2
3 111 3 1
4 38 1 2
5 108 2 1
6 48 3 2
> log.glm <-glm(y~x1+x2,family = possion(link=log),data=data.frame(y,x1,x2))
Error in possion(link = log) : 没有"possion"这个函数
> log.glm <-glm(y~x1+x2,family = poisson(link=log),data=data.frame(y,x1,x2))
> summary(log.glm)
Call:
glm(formula = y ~ x1 + x2, family = poisson(link = log), data = data.frame(y,
x1, x2))
Deviance Residuals:
1 2 3 4 5 6
-3.1382 16.6806 0.8189 -11.0398 1.8210 -12.6942
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.59532 0.15792 22.767 < 2e-16 ***
x1 0.12915 0.04370 2.955 0.00312 **
x2 0.64803 0.07483 8.660 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 662.84 on 5 degrees of freedom
Residual deviance: 575.10 on 3 degrees of freedom
AIC: 619.08
Number of Fisher Scoring iterations: 5
> log.glm.x1
错误: 找不到对象'log.glm.x1'
>
>
>
>
>
>
> summary(log.glm)
Call:
glm(formula = y ~ x1 + x2, family = poisson(link = log), data = data.frame(y,
x1, x2))
Deviance Residuals:
1 2 3 4 5 6
-3.1382 16.6806 0.8189 -11.0398 1.8210 -12.6942
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.59532 0.15792 22.767 < 2e-16 ***
x1 0.12915 0.04370 2.955 0.00312 **
x2 0.64803 0.07483 8.660 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 662.84 on 5 degrees of freedom
Residual deviance: 575.10 on 3 degrees of freedom
AIC: 619.08
Number of Fisher Scoring iterations: 5
> log.glm.x1
错误: 找不到对象'log.glm.x1'
> help("glm")
> anova(log.glm)
Analysis of Deviance Table
Model: poisson, link: log
Response: y
Terms added sequentially (first to last)
Df Deviance Resid. Df Resid. Dev
NULL 5 662.84
x1 1 8.770 4 654.07
x2 1 78.978 3 575.10
> ano= anova(log.glm)
> ano[1]
Df
NULL
x1 1
x2 1
> ano[2]
Deviance
NULL
x1 8.770
x2 78.978
> ano[3]
Resid. Df
NULL 5
x1 4
x2 3
> sum= summary(log.glm)
> sum
Call:
glm(formula = y ~ x1 + x2, family = poisson(link = log), data = data.frame(y,
x1, x2))
Deviance Residuals:
1 2 3 4 5 6
-3.1382 16.6806 0.8189 -11.0398 1.8210 -12.6942
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.59532 0.15792 22.767 < 2e-16 ***
x1 0.12915 0.04370 2.955 0.00312 **
x2 0.64803 0.07483 8.660 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 662.84 on 5 degrees of freedom
Residual deviance: 575.10 on 3 degrees of freedom
AIC: 619.08
Number of Fisher Scoring iterations: 5
> sum[1]
$call
glm(formula = y ~ x1 + x2, family = poisson(link = log), data = data.frame(y,
x1, x2))
> sum[1,1]
Error in sum[1, 1] : 量度数目不对
> sum[2]
$terms
y ~ x1 + x2
attr(,"variables")
list(y, x1, x2)
attr(,"factors")
x1 x2
y 0 0
x1 1 0
x2 0 1
attr(,"term.labels")
[1] "x1" "x2"
attr(,"order")
[1] 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
attr(,"predvars")
list(y, x1, x2)
attr(,"dataClasses")
y x1 x2
"numeric" "numeric" "numeric"
> sum[3]
$family
Family: poisson
Link function: log
> sum[4]
$deviance
[1] 575.0954
> sum[4,1]
Error in sum[4, 1] : 量度数目不对
> sum
Call:
glm(formula = y ~ x1 + x2, family = poisson(link = log), data = data.frame(y,
x1, x2))
Deviance Residuals:
1 2 3 4 5 6
-3.1382 16.6806 0.8189 -11.0398 1.8210 -12.6942
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.59532 0.15792 22.767 < 2e-16 ***
x1 0.12915 0.04370 2.955 0.00312 **
x2 0.64803 0.07483 8.660 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 662.84 on 5 degrees of freedom
Residual deviance: 575.10 on 3 degrees of freedom
AIC: 619.08
Number of Fisher Scoring iterations: 5
> sum[5]
$aic
[1] 619.0808
> sum[6]
$contrasts
NULL
> sum[8]
$null.deviance
[1] 662.8432
> sum[9]
$df.null
[1] 5
> sum[10]
$iter
[1] 5
> sum$aic
[1] 619.0808
> sum$null.deviance
[1] 662.8432
> sum$residual.deviance
NULL
> sum$residual.devianc
NULL
> sum[11]
$deviance.resid
1 2 3 4 5 6
-3.1382350 16.6805594 0.8189003 -11.0397892 1.8209720 -12.6941833
> summary.aov()
Error in summary.aov() : 缺少参数"object",也没有缺省值
> sum
Call:
glm(formula = y ~ x1 + x2, family = poisson(link = log), data = data.frame(y,
x1, x2))
Deviance Residuals:
1 2 3 4 5 6
-3.1382 16.6806 0.8189 -11.0398 1.8210 -12.6942
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.59532 0.15792 22.767 < 2e-16 ***
x1 0.12915 0.04370 2.955 0.00312 **
x2 0.64803 0.07483 8.660 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 662.84 on 5 degrees of freedom
Residual deviance: 575.10 on 3 degrees of freedom
AIC: 619.08
Number of Fisher Scoring iterations: 5
> sum$coefficients
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.5953201 0.15791713 22.767132 9.709542e-115
x1 0.1291456 0.04370053 2.955240 3.124256e-03
x2 0.6480267 0.07482977 8.660013 4.717107e-18
> sum$coefficients[4]
[1] 0.1579171
> sum$coefficients[5]
[1] 0.04370053
> sum$coefficients[6]
[1] 0.07482977
> sum$coefficients[1]
[1] 3.59532
> sum$coefficients[1..2]
错误: unexpected numeric constant in "sum$coefficients[1..2"
> sum$coefficients[1:2]
[1] 3.5953201 0.1291456
> sum$coefficients[1:5]
[1] 3.59532005 0.12914558 0.64802675 0.15791713 0.04370053
> sum$coefficients[11:12]
[1] 3.124256e-03 4.717107e-18
> sum$coefficients[11:12][1]
[1] 0.003124256
> sum$coefficients[11:12][2]
[1] 4.717107e-18