# MATH4330B

```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
&lt;- alphahat and betahat
[1] 5.570655e-06 -3.979039e-07
&lt;- values of the score function and it should be 0
\$code
[1] 1
&lt;- 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=&quot;identity&quot;))
summary(result)
Call:
glm(formula = y/n ~ x, family = binomial(link = &quot;identity&quot;))
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(&gt;|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
&lt;- alphahat and betahat
Family
binomial
gaussian