In this exercise you will:
You need to have installed R, RStudio, and the necessary packages for
the course, including the ELMER
package. See how to
get set up for this course
'data.frame': 8 obs. of 3 variables:
$ Dose : num 1.69 1.72 1.76 1.78 1.81 ...
$ NoBeetles: int 59 60 62 56 63 59 62 60
$ NoKilled : int 6 13 18 28 52 53 61 60
Fit the model \[\mu_Y = \beta_0 + \beta_1 Dose\] to the beetle mortality data using iterative reweighting based on Y having a binomial distribution. Compare the predictions with those from the arcsine transformation.
Beetles = Beetles |> mutate(Prop = NoKilled / NoBeetles)
ModelU =lm(Prop~Dose, data=Beetles)
FitsU=fitted(ModelU)
Beetles = Beetles |> mutate(TProp= asin(sqrt(Prop))) # the arcsine square root transformation for proportions
ModelT=lm(TProp~Dose, data=Beetles)
FitsT=(sin(fitted(ModelT)))^2 # backtransforms fitted values onto the original scale
ModelW=ModelU
for(i in 1:20){
FitProp = replace(fitted(ModelW), fitted(ModelW) <= 0, 0.0001)
FitProp = replace(FitProp, FitProp >= 1, 0.9999)
ModelW=lm(Prop~Dose, data=Beetles, weights=NoBeetles/(FitProp*(1-FitProp)))
}
FitsW=fitted(ModelW)
plot(Prop~Dose, data=Beetles)
abline(ModelU, col=2)
abline(ModelW, col=4)
lines(lowess(FitsT~Beetles$Dose), lty=2)
legend("bottomright", title="Data used in model was", lty=c(1,2,2), col=c(2,4,1), legend=c("raw", "weighted", "transformed"))