--- title: "Exploration 3.1" output: word_document --- ```{r} #install.packages("lattice") ``` ```{r setup, include=FALSE} knitr::opts_knit$set(global.par = TRUE) knitr::opts_chunk$set(fig.width=4, fig.height=2.67) load(url("http://www.isi-stats.com/isi2/ISI.RData")) ``` **Load the piggrowth dataset, e.g.,** ```{r} piggrowth= read.delim("http://www.isi-stats.com/isi2/data/piggrowth.txt", stringsAsFactors=T) head(piggrowth) ``` **5) Create a histogram of the mean pig growths for these 12 pigs** ```{r} with(piggrowth, hist(Growth)) with(piggrowth, isisummary(Growth)) ``` **Report the overall mean pig growth, along with the standard deviation.** ```{r} ##Subset by treatment library(lattice) with(piggrowth, histogram(~Growth |Treatment)) isisummary(piggrowth$Growth, piggrowth$Treatment) ``` **6) How many pigs were assigned to each treatment? Do the four treatments appear to explain variation in pig growth? What proportion of variation in pig growth do the treatments explain?** **7) Record the four treatment means and standard deviations. Which treatment appears to be the best? The worst?** ```{r} table(piggrowth$Antibiotic, piggrowth$VitaminB12) ``` **8) Determine how many pigs in each Antibiotic group were given B12 and how many weren’t. Explain why the two explanatory variables Antibiotic and VitaminB12 are not confounded in this study design.** ```{r} summary(aov(piggrowth$Growth ~ piggrowth$Treatment)) ``` **9) Does the treatment variable explain a statistically significant amount of variation in the pig growths? What is the residual standard error?** ```{r} rsquared = summary(with(piggrowth, lm(Growth~Antibiotic)))$r.squared summary(aov(piggrowth$Growth ~ piggrowth$Antibiotic)) ``` **10) What proportion of variation in pig growth is explained by whether or not the pig received the antibiotic? What is the SSvalue for this variable?** Proportion of variation explained: `r rsquared` SSvalue for Antibiotic: `r summary(aov(piggrowth$Growth ~ piggrowth$Antibiotic))[[1]]$'Sum Sq'[1]` **11) Construct a prediction equation for mean pig growth boxed on the antibiotic treatment given.** ```{r} with(piggrowth, lm(Growth~Antibiotic)) ``` ```{r} ##Subset by Vitamin12 rsquared2 = summary(with(piggrowth, lm(Growth~VitaminB12)))$r.squared summary(aov(piggrowth$Growth ~ piggrowth$VitaminB12)) ``` **12) What proportion of variation in pig growth is explained by whether or not the pig received the antibiotic? What is the SSvalue for this variable?** Proportion of variation explained: `r rsquared2` SSvalue for Vitamin B12: `r summary(aov(piggrowth$Growth ~ piggrowth$VitaminB12))[[1]]$'Sum Sq'[1]` **13. How does the least squares mean compare for these two prediction equations? Why?** **14) When you combine Antibiotic and VitaminB12 into a two-variable model, should you expect a change in the treatment effects? In SSantibiotic or SSvitaminB12 (from questions #12 and #13)? Explain. (Hint: Recall what we learned about this in Chapter 2 in relation to the idea of covariation.)** **15)Two-variable model** ```{r} summary(aov(piggrowth$Growth ~ piggrowth$VitaminB12 + piggrowth$Antibiotic)) ``` **Did the sums of squares change for VitaminB12 or for Antibiotic? Did the $F$-statistics change for VitaminB12 or for Antibiotic? Explain.** **16) What proportion of the total variation in pig growth is explained by the two-variable model? That is, what proportion of the total variation in pig growth is explained by these variables together (the “model” $R^2$)? How does this compare to the sum of the $R^2$ values from the two one-variable models?** **17) What does the p-value corresponding to the overall two-variable model tell us? Describe in the context of the study.** **Statistical model** ```{r} model2 = lm(piggrowth$Growth ~ piggrowth$VitaminB12 + piggrowth$Antibiotic) model2 ``` **18) Use the output to create the combined prediction equation. (Hint: Focus on the values in the “Coef” column; they should look familiar!)** **What is the residual standard error?** `r summary(lm(piggrowth$Growth ~ piggrowth$VitaminB12 + piggrowth$Antibiotic))$sigma` **19) How does the total variation explained by this two-variable model compare to what you found with the one-variable model using the “treatment” variable? How does the residual standard error compare?** **20) The statistical model output gives you a “standard error” for the vitamin B12 effect, after adjusting for the antibiotic. Use this output to approximate a 95% confidence interval for the long-run treatment effect of adding vitamin B12 to the pig diet and interpret this interval in context.** **21) Use your model to estimate the predicted growth for a pig given both the antibiotic and vitamin B12. Include units in your answer.** **22) How does your model prediction from #21 compare to the actual treatment mean? Of course, not every pig will grow the same amount as the mean of the group.** **23) Calculate the residual for the first pig in the dataset. Interpret this residual in the context of this study.** **Show residuals ```{r} hist(resid(model2)) plot(resid(model2)~fitted.values(model2)) ``` **24) One of the validity conditions for the $F$-tests is that the distribution of residuals looks symmetric. Does this condition appear to be met? Be clear about how you are deciding.** **25) Another validity condition is that the responses in each treatment have a similar standard deviation. Scroll down to examine the residuals vs. predicted values graph. Does this condition appear to be met? (Also consider your output in Question #6.)** **26) The third validity condition is that the samples must be independent across the treatments being compared. Is this validity condition met? Why or why not?** **27) Which variable (vitamin B12 or antibiotic) has a larger effect on average pig growth? Which explains a greater proportion of variation in average pig growth? Why is it okay to compare these values?** **28) Would these be causal “effects” or is a cause and effect conclusion not possible here? Explain.** **29) Summarize the main advantages of the study design (balanced factorial design) and the two-variable analysis using both the antibiotic and vitamin B12 simultaneously**