-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbreg_examples.R
153 lines (116 loc) · 3.56 KB
/
nbreg_examples.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
dispersion = function(model){
#Calculate the dispersion statistic
return(sum(residuals(model, type = "pearson")^2)/model$df.residual)
}
###1 Affairs
affairs = read.csv('affairs.csv')
str(affairs)
affairs$relig = as.factor(affairs$relig)
affairs$ratemarr = as.factor(affairs$ratemarr)
#Poisson Model Selection
m1 = glm(naffairs ~ male + age + educ + occup + affair + kids + avgmarr + hapavg + vryhap + notrel + slghtrel + smerel + vryrel + yrsmarr, data=affairs, family=poisson)
summary(m1)
m2 = glm(naffairs ~ kids + avgmarr + hapavg + vryhap + notrel + slghtrel + smerel + vryrel + yrsmarr, data=affairs, family=poisson)
summary(m2)
dispersion(m2)
#Legrange Multiplier Test for overdisp
mu = predict(m2, type='response')
obs = nrow(affairs)
mmu = mean(mu)
n.ybar = obs*mmu
musq = mu^2
mu2 = mean(musq)*obs
chi.val = (mu2 - n.ybar)^2 / (2*mu2)
chi.val
pchisq(chi.val,1,lower.tail=F)
#NB2
m1nb = glm.nb(naffairs ~ kids + avgmarr + hapavg + vryhap + notrel + slghtrel + smerel + vryrel + yrsmarr, data=affairs)
summary(m1nb)
#boundary LRT
LR = -2*logLik(m2) + m1nb$twologlik
pchisq(LR,1,lower.tail=F)/2
#reduced NB2 model
m2nb = glm.nb(naffairs ~ avgmarr + hapavg + vryhap + smerel + vryrel + yrsmarr, data=affairs)
summary(m2nb)
###2 Heart Procedures
heart = read.csv('azpro.csv')
#graphical visualization of los by procedure
par(mfrow=c(1,2))
hist(heart$los[heart$procedure == 0], main = 'PTCA',
ylim = c(0,1250),
xlim = c(0,50), xlab = 'LOS')
hist(heart$los[heart$procedure == 1], main = 'CABG',
ylim = c(0,1250),
xlim = c(0,50), xlab = 'LOS')
#poisson model
m1 = glm(los ~ procedure + sex + admit + age75, data=heart, family=poisson)
summary(m1)
dispersion(m1)
#score test for overdisp
mu = as.numeric(predict(m1,type='response'))
z = ((heart$los - mu)^2 - heart$los) / (mu * sqrt(2))
zscore = lm(z ~ 1)
summary(zscore)
#NB2
m1nb = glm.nb(los ~ procedure + sex + admit + age75, data=heart)
summary(m1nb)
#boundary LRT
LR = -2*logLik(m1) + m1nb$twologlik
pchisq(LR,1,lower.tail=F)/2
###3 Titanic
titanic = read.csv('titanic.csv')
#convert to grouped counts
class1=NULL;class2=NULL;class3=NULL
for(i in 1:dim(titanic)[1]){
if(titanic$age[i] == 1){
titanic$age[i] = 'Adult'
}else{
titanic$age[i] = 'Child'
}
if(titanic$sex[i] == 1){
titanic$sex[i] = 'Male'
}else{
titanic$sex[i] = 'Female'
}
if(titanic$class[i] == 1){
class1[i] = 1
}else if(titanic$class[i] != 1){
class1[i] = 0
}
if(titanic$class[i] == 2){
class2[i] = 1
}else if(titanic$class[i] != 2){
class2[i] = 0
}
if(titanic$class[i] == 3){
class3[i] = 1
}else if(titanic$class[i] != 3){
class3[i] = 0
}
}
titanic = cbind(titanic,class1,class2,class3)
survive=NULL
cases=NULL
for(i in c('Adult','Child')){
for(j in c('Female','Male')){
for(k in 1:3){
survive = c(survive,sum(titanic$survived[titanic$age == i & titanic$sex == j & titanic$class == k]))
cases = c(cases,length(titanic$survived[titanic$age == i & titanic$sex == j & titanic$class == k]))
}
}
}
#create df Titanic grouped data set
age = c(rep('adults',6),rep('child',6))
sex = c(rep('F',3),rep('M',3),rep('F',3),rep('M',3))
class = c(rep(seq(1,3,1),4))
titanic.count = data.frame(survive,cases,age,sex,class)
#poisson model
m1 = glm(survive ~ age + sex + factor(class) + offset(log(cases)), data=titanic.count, family=poisson)
summary(m1)
dispersion(m1)
#NB2
m1nb = glm.nb(survive ~ age + sex + factor(class) + offset(log(cases)), data=titanic.count)
summary(m1nb)
#boundary LRT
LR = -2*logLik(m1) + m1nb$twologlik
pchisq(LR,1,lower.tail=F)/2