#----------------------------------------------#
# R in Action (2nd ed): Chapter 13 #
# Generalized linear models #
# requires packages AER, robust, gcc #
# install.packages(c("AER", "robust", "gcc")) #
#----------------------------------------------# ## Logistic Regression # get summary statistics
data(Affairs, package="AER")
summary(Affairs)
table(Affairs$affairs) # create binary outcome variable
Affairs$ynaffair[Affairs$affairs > 0] <- 1
Affairs$ynaffair[Affairs$affairs == 0] <- 0
Affairs$ynaffair <- factor(Affairs$ynaffair,
levels=c(0,1),
labels=c("No","Yes"))
table(Affairs$ynaffair) # fit full model
fit.full <- glm(ynaffair ~ gender + age + yearsmarried + children +
religiousness + education + occupation +rating,
data=Affairs,family=binomial())
summary(fit.full) # fit reduced model
fit.reduced <- glm(ynaffair ~ age + yearsmarried + religiousness +
rating, data=Affairs, family=binomial())
summary(fit.reduced) # compare models
anova(fit.reduced, fit.full, test="Chisq") # interpret coefficients
coef(fit.reduced)
exp(coef(fit.reduced)) # calculate probability of extramariatal affair by marital ratings
testdata <- data.frame(rating = c(1, 2, 3, 4, 5),
age = mean(Affairs$age),
yearsmarried = mean(Affairs$yearsmarried),
religiousness = mean(Affairs$religiousness))
testdata$prob <- predict(fit.reduced, newdata=testdata, type="response")
testdata # calculate probabilites of extramariatal affair by age
testdata <- data.frame(rating = mean(Affairs$rating),
age = seq(17, 57, 10),
yearsmarried = mean(Affairs$yearsmarried),
religiousness = mean(Affairs$religiousness))
testdata$prob <- predict(fit.reduced, newdata=testdata, type="response")
testdata # evaluate overdispersion
fit <- glm(ynaffair ~ age + yearsmarried + religiousness +
rating, family = binomial(), data = Affairs)
fit.od <- glm(ynaffair ~ age + yearsmarried + religiousness +
rating, family = quasibinomial(), data = Affairs)
pchisq(summary(fit.od)$dispersion * fit$df.residual,
fit$df.residual, lower = F) ## Poisson Regression # look at dataset
data(breslow.dat, package="robust")
names(breslow.dat)
summary(breslow.dat[c(6, 7, 8, 10)]) # plot distribution of post-treatment seizure counts
opar <- par(no.readonly=TRUE)
par(mfrow=c(1, 2))
attach(breslow.dat)
hist(sumY, breaks=20, xlab="Seizure Count",
main="Distribution of Seizures")
boxplot(sumY ~ Trt, xlab="Treatment", main="Group Comparisons")
par(opar) # fit regression
fit <- glm(sumY ~ Base + Age + Trt, data=breslow.dat, family=poisson())
summary(fit) # interpret model parameters
coef(fit)
exp(coef(fit)) # evaluate overdispersion
deviance(fit)/df.residual(fit)
library(qcc)
qcc.overdispersion.test(breslow.dat$sumY, type="poisson") # fit model with quasipoisson
fit.od <- glm(sumY ~ Base + Age + Trt, data=breslow.dat,
family=quasipoisson())
summary(fit.od)

最新文章

  1. 【.NET深呼吸】Zip文件操作(2):动态生成Zip文档
  2. JQuery选择器转义说明
  3. 最短路径之迪杰斯特拉(Dijkstra)算法
  4. ArcGIS Engine开发的ArcGIS 版本管理的功能
  5. DotNetBar v12.1.0.0 Fully Cracked
  6. 一个伪ajax图片上传代码的例子
  7. 在C#中使用GDAL创建Shape文件
  8. Ruby探针的基本实现原理
  9. 如何使用USB安装XenServer 6.x
  10. 【英语】Bingo口语笔记(26) - Take系列
  11. jquery $.trim()方法使用介绍
  12. 新年Uber司机端换新装啦!
  13. cocos2dx进阶学习之屏幕适配
  14. FastDFS 简介
  15. Oracle数据库备份及还原
  16. [物理学与PDEs]第2章习题11 Lagrange 形式的一维理想流体力学方程组在强间断线上的间断连接条件
  17. 网络编程-Mysql-1、数据库的启动关闭,创建数据库,表等基本操作
  18. 2017-2018-2 20155314《网络对抗技术》Exp9 Web安全基础
  19. D- 泛型练习 ,继承,方法
  20. [leetcode]Subsets @ Python

热门文章

  1. 01 语言基础+高级:1-3 常用API第一部分_day08【String类、static、Arrays类、Math类】
  2. PAT Advanced A1104 Sum of Number Segments (20) [数学问题]
  3. OracleXE 11g user莫名过期
  4. 将keras的h5模型转换为tensorflow的pb模型
  5. Dynamics CRM - 为 sub-grid 添加 OnChange 事件
  6. Microsoft.Office.Inter.Excel.dll在調用時可能會出現如下錯誤
  7. LeetCode——853.车队
  8. 内网部署Docker版本Gitlab
  9. 高精度处理斐波那契序列(C语言)
  10. Vue其他指令-组件-全局-局部-组件的交互父传子