#Program 7 # Ths program simulates samples of 10 paired observations from two # designs, thencalculates the Least Squares lines of best fit and # records the estimates of the slope of the line # # 'nsimulations' simulations are performed. #Then the program provides numerical and graphical summaries of # the slopes for the two designs. #set values of simulation parameters # beta0 <- 2 beta1 <- 0.7 sigma <- 0.4 nsimulations <- 1000 storebeta1ests1 <- storebeta1ests2 <- rep(0,nsimulations) xvector1 <- c(0:9)/9 xvector2 <- rep(c(0,1),each=5) xvector1 xvector2 set.seed(12345) exp1 <- expression(paste("Estimates of ",beta[1])) designtypes <- c("Equispaced","Extremities") # #perform the simulations # for (i in 1:nsimulations) { errorvector <- rnorm(10,0,sigma) yvector1 <- beta0 + beta1*xvector1 + errorvector output1 <- lm(yvector1 ~ xvector1) beta1est1 <- (output1$coef)[2] yvector2 <- beta0 + beta1*xvector2 + errorvector output2 <- lm(yvector2 ~ xvector2) beta1est2 <- (output2$coef)[2] # cat("estimate of beta1 is", beta1est, "\n") storebeta1ests1[i] <- beta1est1 storebeta1ests2[i] <- beta1est2 } # # Examine the estimates of beta1 # mean(storebeta1ests1) sd(storebeta1ests1) mean(storebeta1ests2) sd(storebeta1ests2) par(las=1) boxplot(cbind(storebeta1ests1,storebeta1ests2),names=designtypes,ylab=exp1)