1 / 53

R workshop 5

MASH. R workshop 5. 2-way ANOVA ANCOVA Mixed Between-Within subjects ANOVA Notion of Power in statistics and Sample size: Computation and estimation of Sample size with R Effect Sizes with R. In this session you will learn:.

randya
Download Presentation

R workshop 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MASH R workshop 5

  2. 2-way ANOVA ANCOVA Mixed Between-Within subjects ANOVA Notion of Power in statistics and Sample size: Computation and estimation of Sample size with R Effect Sizes with R In this session you will learn:

  3. In the 1-way ANOVA, you were comparing a continuous measurement (the independent variable) between 3 groups. The dependent variable was “Group” and it is a categorical nominal variable. In the 2-way ANOVA, you add another categorical nominal variable (e.g. Gender). You then have 2 dependent variables. The 2-way ANOVA will analyse the difference of the measurement between the Genders, between the Groups AND the interaction effect between Group and Gender. Interaction: You are a Man, you will have the measurement increasing from group 1 to group 3. But if you are a woman, this measurement does not really change. 2-way anova

  4. First download and put the diet.csv file in the current directory where R Studio works. 2-way anova

  5. 2-way anova Research Question: We want to investigate if the weightlost is different according to the Diet and the Gender. We need to create the variable weight lost because this is the continuous variable that we want to compare between Diets and Genders:

  6. 2-way anova We need to rearrange the categorical variables “Diet” and “gender” so that we Can read their real values: 0 -> “Female” 1 -> “Male” It is always advised to transform your categorical variables so that we can see their meaning later on when you plot graphs. It is indeed nicer to have a graph indicating that such gender is “Male” rather than just a number 1… Dataset$variable because we did not attach the data set yet!

  7. 2-way anova Do not forget to attach the data so that it is simpler to call the variables and not using “data set + $ + variable”. Diet can be called directly now that you have attached the data set “dietR”.

  8. Assumptions: • 1 ) No significant outliers for each combination of the groups of the two independent variables. • 2 ) Dependent variable of the ANOVA should be approximately normally distributed for each combination of groups. • 3) Homogeneity of Variances for each combination of groups. • There are no non-parametric alternatives unfortunately! 2-way anova

  9. 1) Checking outliers across combinations of categories. 2-way anova

  10. 2) Dependent variable (continuous scale: weight lost) approximately normally distributed by combination of categories. 2-way anova In this combination (Female in Diet 3), the data looks skewed. But since in all the other combinations, there is a symmetrical distribution and the ANOVA is considered as a robust test, you can still go forthe 2-way ANOVA.

  11. 3) Homogeneity of variance: variance approximately the same per combination of groups. P-value > 0.05, so we keep Homogeneity of Variance (or do not reject it) and we can go on. 2-way anova

  12. 2-way anova It is a star * between the 2 factors.

  13. 2-way anova • Strange! • - There is no significant difference of weight lost between Genders. • There is a strong significant differencebetween the Diets. • There is a significant interaction effect. • So in the end, Gender is to be taken into account because there is an interactioneffect!

  14. 2-way anova Y-Axis X-Axis What the lines represent.

  15. Typical case of interaction: The lines are not parallel  Interaction! 2-way anova

  16. I could have chosen to show the interaction effect by gender and Diet. 2-way anova

  17. Post-hoc Test: 2-way anova

  18. A one-way ANCOVA is similar to a one-way ANOVA. However by comparing a measurement scale between 3 or more groups, we control for another continuous variable, called the covariate. This covariate is a variable that you think might affect the results we obtained in the one-way ANOVA (see Session 2). The p-values found in the ANCOVA will be the p-values adjusted for this covariate. There can be several covariates. We will use the same data set (dietR) as for the 2-way ANOVA. Since there will be only one independent categorical variable, we should call this analysis a “One-Way ANCOVA”. Research question: Is there a significant difference of weight lost between the types of Diet while controlling for the height of the participants? One-way ancova

  19. Assumptions: • 1 ) No significant outliers for each category (Diet). • 2 ) Dependent variable of the ANOVA should be approximately normally distributed for each category (Diet) • 3) Homogeneity of Variances: Variances statistically similar between different Diets. One-way ancova

  20. 1) First of all we will look for outliers in each Diet. One-way ancova

  21. 2) The measurement should be approximately normally distributed by Diet. One-way ancova OK!

  22. 3) Homogeneity of variance: Variance similar between Diets. One-way ancova P-value>0.05: Homogeneity of variance checked! (We keep the Null hypothesis: Homogeneity)

  23. “+” to add the covariate (s) “*” when it is an independent factor added to Diet. One-way ancova - When you control for the height, you have a strongly significant difference of lost weight between the Diets (p-value = 0.0015). - However Height is not significantly associated with lost weight (p-value = 0.722).

  24. If we compare with the one-way ANOVA without the covariate “Height”, we have not much difference. The results suggest that Height does not really affect the fact that there is a difference between the Diets. So whatever your height, the Diets will have the same effect! One-way ancova

  25. In order to conduct ANCOVA post-hoc tests (that is pairwise comparisons while controlling for covariate ‘Height’), type the same function TukeyHSD and specify the independent variable ‘Diet’: One-way ancova Diet 3 seems to be strongly statistically different from Diet 1 and Diet 2.

  26. Research question: Is there a significant difference of weight lost between the types of Diet and the Gender while controlling for the height of the participants? Pretty much similar to the 2-way ANCOVA but you will add an extra independent factor “Gender”. What code will you write? 2-way ancova

  27. “+” to add the covariate (s) “*” when it is an independent factor added to Diet. 2-way ancova

  28. Again the results of the 2-way ANOVA and the 2-way ANCOVA are pretty much similar. There is still an interaction effect between Gender and Diet but not between Genders alone. Height does not influence the effect of the Diet and Gender on the lost weight. 2-way ancova 2-WAY ANCOVA with Height as covariate. 2-WAY ANOVA (no covariate)

  29. Research question: Is there a significant difference of lost weight between the types of Diet and the Gender while controlling for the height and the age of the participants? The between-subjects factor is Diet. The within-subjects factoris Time (“pre” and “after 6 weeks”) Mixed between-within subjects anova

  30. Mixed between-within subjects anova Unfortunately, the format the data is presented is not ready for analysing a Repeated ANOVA! We need: Format 1: WIDE Data Format 2: LONG Data • There are two fundamental verbs of data tidying: • gather() takes multiple columns, and gathers them into key-value pairs: it makes “wide” data longer. • spread(). takes two columns (key & value) and spreads in to multiple columns, it makes “long” data wider.

  31. Mixed between-within subjects anova Install the library “tidyr”: Use the function “gather” contained in that library: Data frame to modify Name of the measure: Cholesterol level. The variable “Times” will take the following values: “Before”,”After4weeks” and “After8weeks”.

  32. We name the new data set “long” to remind us this is the long type of data. Mixed between-within subjects anova You might want to change this awful nameto “ID” or “subject” or “participant”. This function displays the names of each column, or each variable. The first element is the name of the first column That we want to change the name.

  33. The variable ID has a more convenient name now: Mixed between-within subjects anova

  34. We will use the same function “ezANOVA” used for the repeated measures ANOVA (Session 2): The procedure is as follows: Mixed between-within subjects anova We need to add the between-subjects factor “Margarine”. Otherwise it is the same way of writing as for the repeated measures ANOVA seen in Session 2.

  35. Mixed between-within subjects anova

  36. I suggest to look first at the test of Sphericity: There are 2 results: the first row (Effect “Times”) evaluates if the variances between Times are similar (statistically). The second row (Effect “Times”) evaluates if the variances between Times and Group of Margarine are similar (statistically). Based on this, the 2 results suggest that we reject the Sphericity and thus we will have to look at the adjusted p-values taking into accountthat there is no Sphericity, i.e. Sphericity Corrections. Mixed between-within subjects anova

  37. Based on this, the p-value is the second column from the right hand side: Mixed between-within subjects anova The p-value related to Effect Times suggests a very strong evidence against the Null Hypothesis that “There is no effect between Times”. (p-value < 0.001) The p-value related to the interaction Margarine:Times suggests that there is a significant Correlation/interaction between Margarine group and Time. (p-value = 0.0299<0.05)

  38. What about the Between-Subject factor Margarine? The first block of results shows you the Between significance: Mixed between-within subjects anova The p-value is 2.46e-01, but it means 0.246 (ask me if you do not understand!) The p-value is above 0.05, wo you will reject the difference between Margarine regardless the time.

  39. The power of a test statistics is to be taken as a score from 0 to 100%. It measures the reliability of your test. The power depends on the sample size and the bigger your sample size, the more power you will have, and thus the more reliable your test is. The power required in statistics is usually 80%. POWER BIG SAMPLE SIZE BIG In the functions provided in this course, you need to specify the p-value threshold (0.05 generally) and a parameter called the effect size. After that, you can either specify the sample size you have in order to get the power, or conversely, specify the power in order to know what minimum sample size you need. POWER calculation and sample size

  40. You will need to download the “pwr” package in R which provides the following functions to test the power of various tests: POWER calculation and sample size

  41. For correlation coefficients use pwr.r.test(n = , r = , sig.level = , power = ) where n is the sample size and r is the correlation. We use the correlation coefficient as the effect size measure. Cohen suggests that r values of 0.1, 0.3, and 0.5 represent small, medium, and large effect sizes respectively. POWER: correlation coefficient Minimum sample size is 55. Here we enter a sample size and we get The power. (36.91%)

  42. T-tests: For t-tests, use the following functions: pwr.t.test(n = , d = , sig.level = , power = , type = c("two.sample", "one.sample", "paired"))where n is the sample size, d is the effect size, and type indicates a two-sample t-test, one-sample t-test or paired t-test. If you have unequal sample sizes, use pwr.t2n.test(n1 = , n2= , d = , sig.level =, power = ) where n1 and n2 are the sample sizes. For t-tests, the effect size is assessed as Cohen suggests that d values of 0.2, 0.5, and 0.8 represent small, medium, and large effect sizes respectively. You can specify alternative="two.sided", "less", or "greater" to indicate a two-tailed, or one-tailed test. A two tailed test is the default. POWER: t-tests

  43. POWER: t-tests =

  44. POWER: t-tests - The two samples that I have seem not to have the same number of people. - I can nevertheless compute the power of the independent t test between these 2 groups. - Let me remind you that it is not necessary to have the same number of people in both groups in an Independent T-test. Now I want to know the power of my test in such conditions: I will use the secondformula proposed: pwr.t2n.test(n1 = , n2= , d = , sig.level =, power = ) Power of the test 51.20% only! Not very reliable…

  45. ANOVA, use pwr.anova.test(k = , n = , f = , sig.level = , power = )where k is the number of groups and n is the common sample size in each group. For a one-way ANOVA effect size is measured by f where Cohen suggests that f values of 0.1, 0.25, and 0.4 represent small, medium, and large effect sizes respectively. POWER: anova

  46. Effect Size ANOVA: POWER: anova You can download first the library “sjstats” and get the Cohen’s f valuefrom the ANOVA output “res.aov”.

  47. POWER: anova Based on the above, I have 3 groups of Diet and my Cohen’s f value is 0.31. For a power of 80%, I would need more than 34.43, that is 35 participants At least in each group of Diet.

  48. In a linear regression, it is generally agreed that you would need 10 or 15 participants at least per predictor variable. For instance, if you want to predict Birthweight (see Session 4) by the predictors: Gender, Gestation, Mother age and Father age. This multiple linear regression uses 4 predictors, and thus you would need at least 4 x 10 = 40 or 4 x 15 = 60 participants at least. Power: linear regression

  49. For chi-square tests use pwr.chisq.test(w =, N = , df = , sig.level =, power = ) where w is the effect size, N is the total sample size, and df is the degrees of freedom. The effect size w is defined as: Cohen suggests that w values of 0.1, 0.3, and 0.5 represent small, medium, and large effect sizes respectively. Power: chi-square test

  50. Power: chi-square test From the Titanic data set, The w value is in fact the square root of (the Chi-Square divided by the number of people involved in the study), which is given by “X” in the output of the Chi-Square: w = sqrt(X-squared/ Total number of people involved in the study) I would basically need at least 29 people in each subcategory (cell) of my table in order to have a power of 80%!

More Related