<jupyter_start><jupyter_text>Analysis of covariance (ANCOVA) ANCOVA is used to compare one variable (**the dependent variable**) in two or more populations while considering other continuous variables.These continuous varaibles, which are not part of the main experimental manipulation but have an influence on the dependent variable, are known as **covariates**.  Loading Data<jupyter_code>viagraData <- read.delim("data/ViagraCovariate.dat", stringsAsFactors = T, header = T)

viagraData$dose <- factor(viagraData$dose, levels = c(1:3), labels = c("Placebo","Low Dose", "High Dose"))
summary(viagraData)

viagraData<jupyter_output><empty_output><jupyter_text>Checking homogeneity of variance<jupyter_code>library(car)

leveneTest(viagraData$libido, viagraData$dose, center=median)<jupyter_output>Loading required package: carData<jupyter_text>Levene's test is **not significant**, which means that for these data the variances are very similar.  Checking the predictor variable and covariates are independent ANCOVA assumes that the predictor variable (groups) and covariates are independent. We can test this by running an ANOVA with the covariate as the outcome.<jupyter_code>summary( aov(partnerLibido ~ dose , data=viagraData) )<jupyter_output><empty_output><jupyter_text>The F-ratio is **not significant**, so we can carry out the ANCOVA.  Fitting an ANCOVA model  Type I and Type III sum of squares<jupyter_code>covariateFirst <- aov(libido ~ partnerLibido + dose, data=viagraData) 
summary(covariateFirst)

doseFirst <- aov(libido ~ dose + partnerLibido, data=viagraData) 
summary(doseFirst)<jupyter_output><empty_output><jupyter_text>Note that the order in which we enter predicators into a model makes a difference to the effects in the overall ANOVA.In model **libido ~ partnerLibido + dose**, **partnerLibido** is not significant and **dose** is significant.In model **libido ~ dose + partnerLibido**, **dose** is not significant and **partnerLibido** is significant. We can use Type III sums of squares (instead of Type I) by Anova() function to get consistent  results.<jupyter_code>Anova(covariateFirst, type="III")

Anova(doseFirst, type="III")<jupyter_output><empty_output><jupyter_text>Note that although the results are consistent, but they are **not correct** because Type III sum of squares require **orthogonal contrasts** (The default dummy coding is nonorthogonal) .  ANCOVA and Type I sum of squares If we want Type I sum of squares, we should enter the covariates first, then the independent variables in ANCOVA.So we should use **libido ~ partnerLibido + dose**.<jupyter_code>viagraModel <- aov(libido ~ partnerLibido + dose, data=viagraData) 

summary(doseFirst)
summary.lm(doseFirst)<jupyter_output><empty_output><jupyter_text>ANCOVA and Type III sum of squares We must use orthogonal contrast to get correct results of Type III sum of squares.<jupyter_code>contrasts(viagraData$dose)
contrasts(viagraData$dose) <- cbind(c(-2,1,1),c(0,-1,1))

contrasts(viagraData$dose)
viagraModel <- aov(libido ~ partnerLibido + dose, data=viagraData) 

Anova(viagraModel, type ="III")
summary.lm(viagraModel)<jupyter_output><empty_output><jupyter_text>Testing for homogeneity of regression slopes ANCOVA sassumes that the relationship bwtween the covariate and outcome variable (in this case **partnerLibido** and **dose**) should be similar at different levels of the predictor variable (in this case three **dose** groups).<jupyter_code>hoRS <- aov(libido ~ partnerLibido * dose, data = viagraData)

Anova(hoRS, type="III")<jupyter_output><empty_output><jupyter_text>The effect of interfaction of **partnerLibido** and **dose** (**partnerLibido:dose**) is significant, so the assumption can't hold.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0949468085106383, 'fraction_numerical': 0.002127659574468085, 'mean_word_length': 7.036324786324786, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 36, 'https://': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '145133', 'license': 'MIT', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1095, 'n_words': 385, 'path': 'References/Data.Analysis/Discovery.Statistics/R-11-1.ANCOVA.ipynb', 'repo_name': 'royqh1979/python_libs_usage', 'size': '3760'}