-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoursework.R
More file actions
172 lines (137 loc) · 4.29 KB
/
Coursework.R
File metadata and controls
172 lines (137 loc) · 4.29 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#1
bp$female <- as.factor(bp$female)
contrasts(bp$female)
summary(bp)
plot(bp[,-3])
bp <- bp[which(bp$dose != 69.4),]
summary(bp)
plot(bp[,-3])
plot(bp[,-3],col=bp$female)
legend(7,4.3,c("male","female"),col=c("red","black"),pch=1)
model0 <- lm(response~.,data = bp)
model1 <- lm(response~dose,data = bp)
model2 <- lm(response~.^2,data = bp)
par(mfrow=c(2,2))
plot(model0)
plot(model1)
plot(model2)
#2
model <- glm(y~.,data=read,family="poisson")
plot(model)
X <- cbind(1,read$x,read$y)
y <- read$y
beta <- c(3,0,0) # initial guess
r <- 3
seqvec <- Vectorize(seq.defalt,vectorize.args = c("from","to"))
#devience function
D <- function(n){ # n represents inputed mu's and p are the probabilities and y is the count
a <- (n-y)*log(p) # term 1
b <- apply(seqvec(from=n+1,to=n+r-1,by=1),2,prod)/apply(seqvec(from=y+1,to=y+r-1,by=1),2,prod) # term 2
2*r*sum(a+b) # sum and calculate devience
}
nblnglm<-function(X,y,beta,r=3,terminate=1e-8){
eta <- X%*%beta
mu <- exp(eta)
p <- mu/(r+mu)
oldD <- D(as.vector(exp(as.numeric(X%*%beta))))
control <- Inf
while (control > terminate){
eta <- X%*%beta
mu <- exp(eta)
p <- mu/(r+mu)
detadmu <- 1/mu
z <- eta + (y-mu)*detadmu
w <- (1-p)*mu
lmod <- lm(z~X+0,weights=w)
beta <- as.vector(lmod$coeff)
newD <- D(as.vector(exp(as.numeric(X%*%beta))))
control <- abs(newD-oldD)/(abs(newD)+0.1)
oldD <- newD
}
rtrn <- list(lmod,w,beta,newD,p)
names(rtrn) <- c("lmod","w","beta","newD","p")
return(rtrn)
}
rtrn <- nblnglm
lmod <- rtrn&lmod
w <- rtrn$w
beta <- rtrn$beta
newD <- rtrn$newD
p <- rtrn$p
#standard error
J <- t(X)%*%diag(as.vector(w))%*%X
inv.J <- solve(J)
beta.sd <- sqrt(as.vector(diag(inv.J)))
#deviance residuals
n <- as.vector(exp(X%*%beta))
a <- (n-y)*log(p)
b <- apply(seqvec(from=n+1,to=n+r-1,by=1),2,prod)/apply(seqvec(from=y+1,to=y+r-1,by=1),2,prod)
d <- sign(y-mu)*sqrt(2*(a+b))
summary(d)
#pvalues
z <- beta/beta.sd
pvalues <- 2*(1-pnorm(abs(z),lower.tail = TRUE))
#AIC
AIC <- -2*sum(log(choose(y+r-1,y)*((1-p)^r)*(p^y))) + 2*length(beta)
plot(lmod)
X <- cbind(1,read$x,read$y,read$x*read$y)
beta <- c(3,0,0,0) # initial guess
eta <- X%*%beta
mu <- exp(eta)
p <- mu/(r+mu)
oldD <- D(as.vector(exp(as.numeric(X%*%beta))))
control <- Inf
while (control > 1e-8){
eta <- X%*%beta
mu <- exp(eta)
p <- mu/(r+mu)
detadmu <- 1/mu
z <- eta + (y-mu)*detadmu
w <- (1-p)*mu
lmod <- lm(z~X+0,weights=w)
beta <- as.vector(lmod$coeff)
newD <- D(as.vector(exp(as.numeric(X%*%beta))))
control <- abs(newD-oldD)/(abs(newD)+0.1)
print(newD)
oldD <- newD
}
#standard error
J <- t(X)%*%diag(as.vector(w))%*%X
inv.J <- solve(J)
beta.sd <- sqrt(as.vector(diag(inv.J)))
#deviance residuals
n <- as.vector(exp(X%*%beta))
a <- (n-y)*log(p)
b <- apply(seqvec(from=n+1,to=n+r-1,by=1),2,prod)/apply(seqvec(from=y+1,to=y+r-1,by=1),2,prod)
d <- sign(y-mu)*sqrt(2*(a+b))
summary(d)
#pvalues
z <- beta/beta.sd
pvalues <- 2*(1-pnorm(abs(z),lower.tail = TRUE))
#AIC
AIC <- -2*sum(log(choose(y+r-1,y)*((1-p)^r)*(p^y))) + 2*length(beta)
plot(lmod)
X <- cbind(1,read$x) # create design matrix
y <- read$y # response vector
beta <- c(3,0) # initial guess
r <- 3 # set r
terminate <- 10e-8
eta <- X%*%beta # calculate initial eta
mu <- exp(eta) # apply inverse of log to eta to get mu
p <- mu/(r+mu) # calculate p from mu using its definition
oldD <- D(exp(as.numeric(X%*%beta))) # calculate initial deviance of initial guess
control <- Inf # set control
while (control > terminate){ # loop until deviance criterion is less than terminate
eta <- X%*%beta # calculate eta
mu <- exp(eta) # apply inverse log to get mu
p <- mu/(r+mu) # calculate p from mu
detadmu <- 1/mu # calculate differential of eta wrt mu
z <- eta + (y-mu)*detadmu # calculate z
w <- (1-p)*mu # calculate weights
lmod <- lm(z~X+0,weights=w) # fit model with weights to design matrix
beta <- as.vector(lmod$coeff) # extract new betas
newD <- D(exp(as.numeric(X%*%beta))) # calculate new deviance
control <- abs(newD-oldD)/(abs(newD)+0.1) # calculate criterion value
oldD <- newD # assign as oldD for next loop
}
sprintf("Final deviance: %s",round(newD,4)) # print deviance