Uploaded by phqrr1

MATH4330B

advertisement
Example:
#input data
y = c(24, 35, 21, 30)
n = c(24+1355, 35+603, 21+192, 30+224)
x = c(0, 2, 4, 5)
#writing own program in R for this problem with identity link
lik = function(theta){
alpha = theta[1]
beta = theta[2]
sum(y*log(alpha+beta*x) + (n-y)*log(1-alpha-beta*x))
}
nlik = function(theta) -lik(theta)
theta0 = c(0.1, 0.1)
nlm(nlik, theta0)
$minimum
[1] 417.496
$estimate
[1] 0.01724637 0.01977741
<- alphahat and betahat
$gradient
[1] 5.570655e-06 -3.979039e-07
<- values of the score function and it should be 0
$code
[1] 1
<- relative gradient is close to zero, current iterate is probably solution
$iterations
[1] 12
Warning messages:
1: In log(alpha + beta * x) : NaNs produced
2: In nlm(nlik, theta0) : NA/Inf replaced by maximum positive value
3: In log(alpha + beta * x) : NaNs produced
4: In nlm(nlik, theta0) : NA/Inf replaced by maximum positive value
5: In log(alpha + beta * x) : NaNs produced
6: In nlm(nlik, theta0) : NA/Inf replaced by maximum positive value
7: In log(alpha + beta * x) : NaNs produced
8: In nlm(nlik, theta0) : NA/Inf replaced by maximum positive value
#built-in glm command specifying distribution and link
result = glm(formula = y/n ~ x, family = binomial(link="identity"))
summary(result)
Call:
glm(formula = y/n ~ x, family = binomial(link = "identity"))
Deviance Residuals:
1
2
3
4
0.002992 -0.010124 0.004100 0.002008
Coefficients:
Estimate
(Intercept) 0.01702
x
0.02009
Std. Error z value Pr(>|z|)
0.12409
0.137
0.891
0.05546
0.362
0.717
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 0.10426994 on 3 degrees of freedom
Residual deviance: 0.00013229 on 2 degrees of freedom
AIC: 4.6069
Number of Fisher Scoring iterations: 4
#what we did in last lecture, using LS method
lm(y/n~x)
Call:
lm(formula = y/n ~ x)
Coefficients:
(Intercept)
x
0.01631 0.02034
<- alphahat and betahat
Some information about R
glm(formula, family=familytype(link=linkfunction), data=)
Family
Default Link Function
binomial
(link = "logit")
gaussian
(link = "identity")
Gamma
(link = "inverse")
inverse.gaussian (link = "1/mu^2")
poisson
(link = "log")
quasi
(link = "identity", variance = "constant")
quasibinomial
(link = "logit")
quasipoisson
(link = "log")
Note: Book gives SAS program that’s why I did not give info here.
Download