# # File name: lec05.R # # # data example: advertising # advert = data.frame(c(3.2, 1.4, 2.6, 6.9, 3.6, 1.7, 5.0), c(4.9, 2.4, 4.4, 9.6, 4.8, 2.1, 7.3)) names(advert) = c("ads","sale") advert # # construct Working-Hotelling 90% simultaneous CIs for the expected sale # when advertising expenditure is 3, 5, and 7. # # load function conf.band in the script "lec05-func.R" source("lec05-func.R") WH.conf.band(3, advert$ads, advert$sale, 0.9) # [1] 3.961907 4.863895 WH.conf.band(5, advert$ads, advert$sale, 0.9) # [1] 6.554108 7.694864 WH.conf.band(7, advert$ads, advert$sale, 0.9) # [1] 8.876144 10.795998 # # plot a Working-Hotelling 99% confidence band along with the fitted line # # create a grid of predictor variable values within the range of the observed data range(advert$ads) # [1] 1.4 6.9 all.ads = matrix(seq(0, 8, length=50),ncol=1) # apply the function conf.band function to each value in all.ads #Your function would be cooler than mine if it operated on vectors #(thereby removing the need for apply()), eg. if it was called #as WH.conf.band(seq(0,8,length=50),...etc...) out.ci = apply(all.ads, 1, WH.conf.band, advert$ads, advert$sale, 0.99) plot(advert$ads, advert$sale, pch=20, xlab="advertising expenditure", ylab="sale") abline(lm(sale~ads, data=advert)) lines(all.ads, out.ci[1,], lty=2) lines(all.ads, out.ci[2,], lty=2) # # construct 90% simultaneous PIs when advertising expenditures are 3, 5, and 7 using # (a) Scheffe procedure # (b) Bonferroni procedure # # Scheffe' multiplier sqrt(3*qf(0.9, 3, 5)) # Bonferroni multiplier qt(1-0.1/(2*3), 5)