<jupyter_start><jupyter_text>DS105 Intermediate Statistics : Lesson Seven Companion Notebook  Table of Contents * [Table of Contents](DS105L7_toc)    * [Page 1 - Introduction to ANCOVA](DS105L7_page_1)    * [Page 2 - ANCOVAs](DS105L7_page_2)    * [Page 3 - ANCOVA Setup in R](DS105L7_page_3)    * [Page 4 - ANCOVAs in R](DS105L7_page_4)    * [Page 5 - ANCOVA Activity](DS105L7_page_5)    * [Page 6 - ANCOVA Activity Solution](DS105L7_page_6)    * [Page 7 - Key Terms](DS105L7_page_7)    * [Page 8 - Lesson 7 Hands-On](DS105L7_page_8)    * [Page 9 - Lesson 7 Hands-On Solution](DS105L7_page_9)      Page 1 - Introduction to ANCOVAs[Back to Top](DS105L7_toc)<jupyter_code>from IPython.display import VimeoVideo
# Tutorial Video Name: ANCOVAs
VimeoVideo('388137895', width=720, height=480)<jupyter_output><empty_output><jupyter_text>The transcript for the above overview video **[is located here](https://repo.exeterlms.com/documents/V2/DataScience/Video-Transcripts/DSO105L07overview.zip)**. IntroductionNow that you have the basics of ANOVAs down, you will begin to explore some of the more advanced ANOVA variations.  The ANOVA family of analyses are endlessly diverse, which make them a great tool to have in your pocket! You can have as many independent variables as you like, as long as you have the sample size for it, and you can add it covariates that will help control for factors that may influence how your IV and your DV are related.As you may have discovered last lesson, R is really the most appropriate program to run ANOVA tests in.  Therefore, this lesson you will dig into within subjects ANOVAs, which help you look at changes over time, mixed-measures ANOVAs, which have both a within and between subjects element to them, and ANCOVAs, which stands for *Analysis of Covariance*, and will include one or more covariates.By the end of this lesson, you will be able to:* Test for assumptions and compute an ANCOVA in R using the linear modeling functionThis lesson will culminate in a hands-on that allows you to put your skills to use analyzing honey sales data over through the years.  Page 2 - ANCOVAs[Back to Top](DS105L7_toc)<jupyter_code>from IPython.display import VimeoVideo
# Tutorial Video Name: ANCOVAs
VimeoVideo('340999354', width=720, height=480)<jupyter_output><empty_output><jupyter_text>The transcript for the above overview video **[is located here](https://repo.exeterlms.com/documents/V2/DataScience/Video-Transcripts/DSO105L07pg2tutorial.zip)**.*ANCOVA* stands for *analysis of covariance*.  It is an analysis in the family of ANOVAs, and the big difference is that "C." An ANCOVA takes into account, or adjusts for, yet another factor in your model, aptly named a *covariate*.  Put another way, an ANCOVA controls for the changes that might come up naturally.  For instance, men are slightly better than women at spatial and analytic reasoning on average.  If you were trying to test out a new method of studying for a math class, and you tested it on both men and women, you would want to make sure to control for the natural effect that gender might have in mathematical reasoning. You could test the spatial and analytic reasoning skills at pre-test, teach everyone to study with your new method, and then test spatial and analytic reasoning skills at post-test.  If you collected information from everyone about their gender, you could then use it to control for the natural differences between men and women, using it as a covariate.Covariates are typically continuous, but it is possible to use categorical covariates if you dummy code them.            Additional Info!                If you would like to play around more with all the different variations of factorial ANOVAs, feel free to check out this website    ---  Page 3 - ANCOVA Setup in R[Back to Top](DS105L7_toc)<jupyter_code>from IPython.display import VimeoVideo
# Tutorial Video Name: ANCOVAs Part I
VimeoVideo('340999354', width=720, height=480)<jupyter_output><empty_output><jupyter_text>The transcript for the above overview video **[is located here](https://repo.exeterlms.com/documents/V2/DataScience/Video-Transcripts/DSO105L07pg3-4tutorial.zip)**. ANCOVA Set Up in RNow that you have learned a little bit about covariates, it's time to put them into use! But of course, it's important to get all your ducks in a row first.--- Load LibrariesThere are several libraries you will need to load in order to complete the ANCOVA process. ```rcompanion```, as you know by now, is for assessing normality with easy, best-fit histograms. The ```car``` library is both for assessing homogeneity of variance and for dealing with violations of said assumption. The ```effects``` library will assist you in the creation of means adjusted by your covariate, and the ```multcomp``` package is for conducting post hocs for ANCOVAs.```{r}library("rcompanion")library("car")library("effects")library("multcomp")```--- Load in DataThe data you will be conducting ANCOVAs upon is data about admissions into a graduate school program.  It is located **[here](https://repo.exeterlms.com/documents/V2/DataScience/Intermediate-Stats/graduate_admissions.zip)**. --- Question Set UpYou will answer the following question with your ANCOVA: ```textControlling for students' research participation in undergrad, does the rating of the students' undergraduate university impact their chance of admittance into graduate school? ```For this question, the covariate is students' research participation in undergrad (```Research```), since that is what you are controlling.  The IV is the categorical variable, rated 1-5, of students' undergraduate university (```University.Rating```), and the DV is the chance of admittance into graduate school (```Chance.of.Admit```).--- Data WranglingWith this particular dataset, there is very little data wrangling required. All that is necessary is to ensure that all categorical variables are factors, not integers, since the data type will throw off the code for getting adjusted means and post hocs later (the ANCOVA itself will take the data in any data type).--- Ensure the IV is a Factor You can see what format the data is in for your IV by using the ```str()``` function: ```{r}str(graduate_admissions$University.Rating)```Note that the output shows it is an integer! That can easily be converted: ```{r}graduate_admissions$University.Rating <- as.factor(graduate_admissions$University.Rating)```--- Ensure the CV is a FactorCovariates can either be categorical or continuous.  Either is perfectly acceptable.  However, if you do use a categorical CV, like ```Research``` is, it needs to be formatted as factor data, not as an integer.  You can test and fix this just like the IV: ```{r}str(graduate_admissions$Research)graduate_admissions$Research <- as.factor(graduate_admissions$Research)```You have now completed all data wrangling activities for this particular ANCOVA!--- Testing AssumptionsThe assumptions for an ANCOVA are similar to those for your basic ANOVAs. However, one assumption is added - the assumption of *homogeneity of regression slopes*, which tests for whether the predictor variable (DV) and the covariate are independent of each other. This is because you are controlling for the presence of the covariate, not determining whether it has an effect on your DV. If the covariate is related in any way to your DV, then most likely it should be an independent variable, not a covariate, because it is explaining some variance in your model.--- NormalityIs this one starting to get old hat yet? Take a look at the normality of your continuous variable, ```Chance.of.Admit```.```{r}plotNormalHistogram(graduate_admissions$Chance.of.Admit)```Here is the resulting figure: ![A bar graph depicts the plot of x against the frequency. The x-axis represents x and the y-axis represents frequency. The x-axis ranges from 0.3 to 1.0. The y-axis ranges from 0 to 60. Fourteen bars and a curve are plotted.](Media/ANCOVA5.png)It could probably be a little more normal, though it's not too bad.  Try a square transformation: ```{r}graduate_admissions$Chance.of.AdmitSQ <- graduate_admissions$Chance.of.Admit * graduate_admissions$Chance.of.AdmitplotNormalHistogram(graduate_admissions$Chance.of.AdmitSQ)```It looks like that was a pretty good choice, huh? ![A bar graph depicts the plot of x against the frequency. The x-axis represents x and the y-axis represents frequency. The x-axis ranges from 0.2 to 1.0. The y-axis ranges from 0 to 80. Fourteen bars and a curve are plotted. The curve is exactly placed on the bars.](Media/ANCOVA6.png)Your data now meets the assumption of normality.--- Homogeneity of VarianceTo test for homogeneity of variance, you will run Levene's Test.  Unfortunately, however, Levene's test will not take into account any categorical covariates (though they are perfectly valid).  So you'll include the model as best you can without it: ```{r}leveneTest(Chance.of.AdmitSQ~University.Rating, data=graduate_admissions)```Here is the result: ```textLevene's Test for Homogeneity of Variance (center = median)       Df F value  Pr(>F)  group   4  2.4283 0.04734 *      395                  ---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1```Unfortunately, this test is significant, which means that you have violated the assumption of homogeneity of variance.  You will learn how to correct for this violation on the next page.--- Homogeneity of Regression SlopesIn order to test for homogeneity of regression slopes, you can run a one-way ANOVA, with your covariate as the IV and the DV you were planning to use for your ANCOVA. If the *F* test is non-significant, then you are good to go!Here is the basic ANOVA information: ```{r}Homogeneity_RegrSlp = lm(Chance.of.AdmitSQ~Research, data=graduate_admissions)anova(Homogeneity_RegrSlp)```As you can see, the results of this ANOVA are saved into an object called ```Homogeneity_RegrSlp```.  Then you can use the ```lm()``` function to create your linear model.  The dependent variable will remain the same as the one you plan to use in your ANCOVA - ```Chance.of.AdmitSQ```. And the covariate you're planning to use in the ANCOVA will be the IV - ```Research```.  Specify the dataset with the argument ```data=``` and away you run! To get an ANOVA summary table out of this analysis, just call the ```anova``` function on your saved object. Here are the results: ```textAnalysis of Variance TableResponse: Chance.of.AdmitSQ           Df  Sum Sq Mean Sq F value    Pr(>F)    Research    1  5.2035  5.2035   189.8 < 2.2e-16 ***Residuals 398 10.9113  0.0274                      ---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1```Unfortunately, since the *p* value is significant, your data does not meet the assumption of homogeneity of regression slopes.  That means that whether someone does research or not actually does have an impact on their chance of admittance to graduate school, and that you should NOT use ```Research``` as a covariate, but rather include it as a second independent variable in the model.  However, for the purposes of learning, you will just continue on.--- Sample SizeThe last assumption for ANCOVAs is sample size.  There has to be at least 20 cases for every IV or CV.  Since you will have one IV and one CV, you will need at least 40 rows of data. In this case, you have 400 cases, so this assumption is more than adequately met!---  Page 4 - ANCOVAs in R[Back to Top](DS105L7_toc)<jupyter_code>from IPython.display import VimeoVideo
# Tutorial Video Name: ANCOVAs Part II
VimeoVideo('340997929', width=720, height=480)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07499140006879945, 'fraction_numerical': 0.02287581699346405, 'mean_word_length': 4.658880778588808, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '107810', 'license': 'MIT', 'n_tokens_mistral': 3459, 'n_tokens_neox': 3051, 'n_words': 1579, 'path': 'Data Science and Machine Learning/Machine-Learning-In-Python-THOROUGH/RECAP_DS/04_INTERMEDIATE_STATISTICS/L07.ipynb', 'repo_name': 'okara83/Becoming-a-Data-Scientist', 'size': '11628'}