吴裕雄--天生自然 R语言开发学习:广义线性模型(续一)
#----------------------------------------------#
# 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)
吴裕雄--天生自然 R语言开发学习:广义线性模型(续一)的更多相关文章
- 吴裕雄--天生自然 R语言开发学习:R语言的安装与配置
下载R语言和开发工具RStudio安装包 先安装R
- 吴裕雄--天生自然 R语言开发学习:数据集和数据结构
数据集的概念 数据集通常是由数据构成的一个矩形数组,行表示观测,列表示变量.表2-1提供了一个假想的病例数据集. 不同的行业对于数据集的行和列叫法不同.统计学家称它们为观测(observation)和 ...
- 吴裕雄--天生自然 R语言开发学习:导入数据
2.3.6 导入 SPSS 数据 IBM SPSS数据集可以通过foreign包中的函数read.spss()导入到R中,也可以使用Hmisc 包中的spss.get()函数.函数spss.get() ...
- 吴裕雄--天生自然 R语言开发学习:使用键盘、带分隔符的文本文件输入数据
R可从键盘.文本文件.Microsoft Excel和Access.流行的统计软件.特殊格 式的文件.多种关系型数据库管理系统.专业数据库.网站和在线服务中导入数据. 使用键盘了.有两种常见的方式:用 ...
- 吴裕雄--天生自然 R语言开发学习:R语言的简单介绍和使用
假设我们正在研究生理发育问 题,并收集了10名婴儿在出生后一年内的月龄和体重数据(见表1-).我们感兴趣的是体重的分 布及体重和月龄的关系. 可以使用函数c()以向量的形式输入月龄和体重数据,此函 数 ...
- 吴裕雄--天生自然 R语言开发学习:基础知识
1.基础数据结构 1.1 向量 # 创建向量a a <- c(1,2,3) print(a) 1.2 矩阵 #创建矩阵 mymat <- matrix(c(1:10), nrow=2, n ...
- 吴裕雄--天生自然 R语言开发学习:图形初阶(续二)
# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...
- 吴裕雄--天生自然 R语言开发学习:图形初阶(续一)
# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...
- 吴裕雄--天生自然 R语言开发学习:图形初阶
# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...
- 吴裕雄--天生自然 R语言开发学习:基本图形(续二)
#---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...
随机推荐
- 查看jks文件中的签名
1. 打开CMD命令行进入本机安装的jdk或jre下的bin目录. 2. 下来看图 keytool -list -v -keystore C:\Users\Administrator\Desktop\ ...
- gcc和g++的区别解析
1.误区:gcc只能编译C源代码,g++只能编译C++源代码 解析:其实gcc和g++都可以编译c/c++源代码,只是细节不同,后缀名为.c的源文件,gcc将其当作C程序,而g++则当作c++程序来处 ...
- 面向对象 part2 属性的特性
6.1.1理解对象 创建自定义对象最简单的方式就是创建一个object实例.然后添加方法和实例 var person = new Object() person.name = "hi&quo ...
- python字典常用方法
字典(Dictionary) 字典是一个无序.可变和有索引的集合.在 Python 中,字典用花括号编写,拥有键和值. 实例 创建并打印字典: thisdict = { "brand&quo ...
- \_\_del\_\_
__del__ 一.__del__ __del__也称之为析构方法 __del__会在对象被删除之前自动触发 print('主')class People: def __init__(self, na ...
- Notification通知在OPPO手机上不弹出提示?
oppo默认应用 不允许通知. 解决步骤:设置 通知与状态栏 通知管理 NotificationTest 允许通知
- Proe 导出PDF Vb.net
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSt ...
- CodeForces 995B Suit and Tie(贪心,暴力)
https://codeforces.com/problemset/problem/995/B 题意: 就是通过每次移动相邻的两位数,来使数值相同的数挨在一起,求最少要移动多少次. 思路: 直接从前往 ...
- hdu2222 (AC自动机模板)
题:http://acm.hdu.edu.cn/showproblem.php?pid=2222 学习出:https://bestsort.cn/2019/04/28/402/ 主要是fail的建立. ...
- 虹软最新版 python 接口 完整版
虹软最新版 python 接口 完整版 当前开源的人脸检测模型,识别很多,很多小伙伴也踩过不少坑.相信不少使用过dlib和facenet人脸识别的小伙伴都有这样的疑惑,为什么论文里高达99.8以上的准 ...