T-TEST

T-TEST We will begin by using the file “Invisibility.csv” Note: Use the Import From Text (readr) option to import. You’ll get funky column headings if you use From Text (base)

library(readr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ dplyr   1.0.10
## ✔ tibble  3.1.8      ✔ stringr 1.5.0 
## ✔ tidyr   1.2.1      ✔ forcats 0.5.2 
## ✔ purrr   1.0.1      
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
invisibility <- read_csv("Invisibility.csv")
## Rows: 24 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (2): Cloak, Mischief
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(invisibility)

1)

It’s good to explore your data before conducting any analyses, so please create a bar chart or boxplot examining the mean amount of mischief by cloaks.

# Get summary statistics for the the continuous column for each category
invisibility_summary <-invisibility %>%
  group_by(Cloak) %>%
  summarise(n = n(),
    mean_mischief = mean(Mischief),
    sd_mischief = sd(Mischief),
    min_mischief = min(Mischief),
    max_mischief = max(Mischief)
  )

invisibility_summary 
## # A tibble: 2 × 6
##   Cloak     n mean_mischief sd_mischief min_mischief max_mischief
##   <dbl> <int>         <dbl>       <dbl>        <dbl>        <dbl>
## 1     0    12          3.75        1.91            0            6
## 2     1    12          5           1.65            2            8
# Create a histogram of the Mischief variable
ggplot(invisibility, aes(x = Mischief, fill = factor(Cloak))) +
  geom_histogram(position = "dodge", bins = 10) +
  facet_wrap(~ factor(Cloak)) +
  labs(title = "Histogram of Mischief by Cloak", x = "Mischief", y = "Count") +
  theme_bw()

# Create a box plot of the Mischief variable
ggplot(invisibility, aes(x = factor(Cloak), y = Mischief, fill = factor(Cloak))) +
  geom_boxplot() +
  labs(title = "Box plot of Mischief by Cloak", x = "Cloak", y = "Mischief") +
  theme_bw()

# Create a customized density plot of the continuous variable
invisibility %>%
  ggplot(aes(x = Mischief, fill = factor(Cloak), group = factor(Cloak))) +
    geom_density(alpha = 0.5) +
    labs(title = "Density plot of Value by Category", x = "Mischief", y = "Density", fill = "Cloak") +
    theme_bw()

2)

Does it look like there is a different level of mischievousness if one is wearing a cloak or not?

cloak and nocloak to not look widely different.

There are two types of t-tests:

  1. an independent t-test which tests whether the mean of two groups is significantly different from each other. (The null hypothesis is that the means are equal).

  2. a dependent-means t-test which is done when we have matched-pairs, or subjects underwent both conditions of the experiment. In this case, we can take differences of the scores with the assumption that if both conditions have the same effect, the differences of those scores should be 0.

Assumptions of the Independent T-test

  1. The sampling distribution is normally distributed. We test this by seeing if the two independent samples are normally distributed.

  2. Data are measured at the interval level

  3. Scores in different treatments are independent

  4. Homogeneity of variance. The variance is the same in both groups.

Let’s discuss each of these assumptions in turn. 1. The sampling distribution is normal.

We want to test that it is normal within group.

nocloak <-invisibility %>%
  filter(Cloak=="0")
cloak <- invisibility %>%
  filter(Cloak=="1")
shapiro.test(nocloak$Mischief)
## 
##  Shapiro-Wilk normality test
## 
## data:  nocloak$Mischief
## W = 0.91276, p-value = 0.2314
shapiro.test(cloak$Mischief)
## 
##  Shapiro-Wilk normality test
## 
## data:  cloak$Mischief
## W = 0.97262, p-value = 0.9362

3)

Do either of these groups (Cloak or No cloak) violate the assumption of normality? Describe the method you used to determine the answer to this question. Did you use a test? If so, which? Did you create a graph/figure? If so, which one?

I created a histogram and other plots to observe the distribution. I appeared to be normal. Then I ran the Shapiro-Wilk test for each group

Based on the Shapiro-Wilk normality test results, the data for both Cloak levels appear to be approximately normally distributed.

For the Cloak level of 0 (nocloak), the test statistic is 0.91276 with a p-value of 0.2314. Since the p-value is greater than 0.05, we fail to reject the null hypothesis that the data are normally distributed.

For the Cloak level of 1 (cloak), the test statistic is 0.97262 with a p-value of 0.9362. Again, the p-value is greater than 0.05, so we fail to reject the null hypothesis that the data are normally distributed.

Therefore, we can assume that the data for both Cloak levels are normally distributed, which supports the assumption of the t-test.

4)

The data are expected to be interval. Are they? It is numeric, it is continuous. You can’t have a half of a mischievous act but it works fine for this test.

The scores in different treatments are assumed to be independent. We have not collected the data, but we will assume that this is the case.

5)

The fourth assumption is homogeneity of variance.

Please test this assumption. Do our data violate the homogeneity of variance assumption? Note: When you try to run this the way we did in class, you should get an error: “Levene’s test is not appropriate with quantitative explanatory variable” What this means is that it doesn’t know that Cloak is a categorical (or factor) variable. To fix this, we’ll include Cloak in this way:

library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
leveneTest(Mischief ~ as.factor(Cloak), data=invisibility)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  1  0.2698 0.6087
##       22

The null hypothesis is that the variances in the different groups are equal – meaning that a significant result from Levene’s test could indicate a problem. H0 = sigma1 = sigma2 Ha = sigma1 ≠ sigma2

The output of the Levene’s test for homogeneity of variance suggests that there is no significant difference in the variances of Mischief scores between the two levels of Cloak.

The test statistic is 0.2698, and the p-value is 0.6087, which is greater than the common alpha level of 0.05. This indicates that we fail to reject the null hypothesis that there is no difference in the variances of the Mischief scores between the two groups.

Therefore, we can assume that the variances of Mischief scores are approximately equal for both Cloak levels, which supports the assumption of the t-test.

Running the T-Test

6)

Please run the independent t-test. Is this test significant? What would we conclude?

t.test(Mischief ~ Cloak, data=invisibility, var.equal=TRUE) 
## 
##  Two Sample t-test
## 
## data:  Mischief by Cloak
## t = -1.7135, df = 22, p-value = 0.1007
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -2.7629284  0.2629284
## sample estimates:
## mean in group 0 mean in group 1 
##            3.75            5.00

The output of the two-sample t-test suggests that there is no significant difference in the mean Mischief scores between the Cloak levels of 0 and 1.

The test statistic (t-value) is -1.7135, and the p-value is 0.1007, which is greater than the common alpha level of 0.05. This indicates that we fail to reject the null hypothesis that there is no difference in the mean Mischief scores between the two groups.

The 95% confidence interval for the difference in means ranges from -2.7629 to 0.2629. Since this interval includes 0, it provides further evidence that there may be no significant difference in the mean Mischief scores between the two groups.

The sample means for the Mischief scores are 3.75 for Cloak level 0 and 5.00 for Cloak level 1. These values suggest that, on average, Mischief scores may be slightly higher for Cloak level 1 compared to Cloak level 0, but the difference is not statistically significant at the alpha level of 0.05.

Violated Assumptions It is possible that one of our three assumptions are violated. If homogeneity of variance is violated (the variances are not equal), then we need to run the Welch’s test. If normality is violated, then we need to run a non-parametric Wilcoxon rank-sum test. Finally, if the data are not independent, we need to run the dependent t-test (which we’ll cover next time).

Welch’s test

7)

The Welch’s test allows you to compare the means of two groups without requiring homogeneity of variance. Regardless of whether your homogeneity of variance assumption was met, run the Welch test and interpret the results.

t.test(Mischief ~ Cloak, data=invisibility) 
## 
##  Welch Two Sample t-test
## 
## data:  Mischief by Cloak
## t = -1.7135, df = 21.541, p-value = 0.101
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -2.764798  0.264798
## sample estimates:
## mean in group 0 mean in group 1 
##            3.75            5.00

If the variances are not equal, remove var.equal=TRUE in: t.test(*dependent_first* ~ *independent*, name_data, var.equal=TRUE) It automatically runs a Welch t-test. These results are not much different that the t-test.

The test statistic (t-value) is -1.7135, and the p-value is 0.101, which is greater than the common alpha level of 0.05. This indicates that we fail to reject the null hypothesis that there is no difference in the mean Mischief scores between the two groups.

The 95% confidence interval for the difference in means ranges from -2.7648 to 0.2648. Since this interval includes 0, it provides further evidence that there may be no significant difference in the mean Mischief scores between the two groups.

The sample means for the Mischief scores are 3.75 for Cloak level 0 and 5.00 for Cloak level 1. These values suggest that, on average, Mischief scores may be slightly higher for Cloak level 1 compared to Cloak level 0, but the difference is not statistically significant at the alpha level of 0.05.

Wilcoxon rank-sum test

8)

The Wilcoxon rank-sum test (also referred to as the Mann-Whitney test) allows you to analyze differences in means if the data are not normal. Regardless of whether you concluded that the data in invisibility was normal, run the Wilcoxon rank-sum test on that data and interpret the results.

wilcox.test(Mischief ~ Cloak, data=invisibility, paired=FALSE, exact=F)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Mischief by Cloak
## W = 47, p-value = 0.1492
## alternative hypothesis: true location shift is not equal to 0

The null hypothesis for the Wilcoxon rank sum test is that the two groups have the same distribution, or equivalently, that the location parameter of the two groups is the same. The mean of the ranks (medians) are equal to each other. The alternative hypothesis is that the two groups have different location parameters.

The test statistic W is 47 and the p-value is 0.1492. Mischief did not differ significantly between wearing a cloak and no cloak.

Communicating Results To communicate results, we want to remember our blocks from last week: A. Start with some descriptive statistics. B. The description tells you what the null hypothesis being tested is C. A “stat” block is included D. The results are interpreted

For the t-test, this will look something like:

On average, anastasia’s students performed better (M = 74.5, SD = 9.0) than cloakadette’s students (M = 69.1, SD = 5.77). A t-test was conducted to test whether the means of the two TAs was significantly different. This difference, 5.4, was significant t(31) = 2.115, p = 0.043. Based on this result, we can conclude that anastasia’s student performed significantly better on average than cloakadette’s students.

9)

Write a communication block for your findings from the Invisibility dataset.

*On average, Mischief was slightly higher for individuals with an invisibility cloak (M = 5, SD = 1.65) than individuals without a cloak (M = 3.75, SD = 1.91). We conducted a t-test to test whether the means of cloak and no cloak was significantly different. This difference, 1.25, was not significant t(22) = -1.7135, p = 0.1007. Based on this result, fail to reject the null that individuals with an invisibility cloak have different mischievous levels.

HW #7

Part 1:

You are interested in understanding age preferences for heterosexual couples. You have heard that on average women tend to be a bit younger than men in heterosexual partnerships and want to see if this represents preferences for men and women. To test this, you look at personal ads and measure the difference between the person’s reported age and the reported preference for age range of the partner they are seeking. For instance, if a person states that they are 29 but are seeking a partner 20-29, then this is calculated as 29 – 24.5(the midpoint of 20 and 29) = 4.5.

Download data from blackboard titled PartnerAge.csv.

PartnerAge <- read_csv("PartnerAge.csv")
## Rows: 86 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (2): sex, preferred_age
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

This is data shows the desired age of a partner from a set of personal ads for men and women (specifically, this only looks at the two groups: men seeking women (coded as 1) and women seeking men (coded as 0)). Negative values mean that individuals want an older partner.

head(PartnerAge)
## # A tibble: 6 × 2
##     sex preferred_age
##   <dbl>         <dbl>
## 1     0            -6
## 2     0            -6
## 3     0            -5
## 4     0            -4
## 5     0            -8
## 6     0            -4
sapply(PartnerAge, class)
##           sex preferred_age 
##     "numeric"     "numeric"
  1. Create a graph of the mean desired age for men and women. Personally, I think boxplots are a useful way to examine these distributions, but this type of chart is not required.
ggplot(PartnerAge, aes(x = factor(sex), y = preferred_age, fill = factor(sex))) +
  geom_boxplot() +
  labs(title = "Box plot of Preferred Age by Sex", x = "Sex", y = "Preferred Age") +
  theme_bw()

# Get summary statistics for the the continuous column for each category
PartnerAge_summary <-PartnerAge %>%
  group_by(sex) %>%
  summarise(n = n(),
    mean_preferred_age = mean(preferred_age),
    sd_preferred_age = sd(preferred_age),
    min_preferred_age = min(preferred_age),
    max_preferred_age = max(preferred_age)
  )

PartnerAge_summary
## # A tibble: 2 × 6
##     sex     n mean_preferred_age sd_preferred_age min_preferred_age max_prefer…¹
##   <dbl> <int>              <dbl>            <dbl>             <dbl>        <dbl>
## 1     0    43             -1.08              4.19               -14          7  
## 2     1    43              0.593             5.99               -18         13.5
## # … with abbreviated variable name ¹​max_preferred_age
-1.08 - 0.59
## [1] -1.67
  1. Determine whether the desired age is normally distributed, separately for males and females
#Separate males and females
women_seeking_men <-PartnerAge %>%
  filter(sex=="0")
men_seeking_women <- PartnerAge %>%
  filter(sex=="1")

#Test for normal distribution
shapiro.test(women_seeking_men$preferred_age)
## 
##  Shapiro-Wilk normality test
## 
## data:  women_seeking_men$preferred_age
## W = 0.97336, p-value = 0.4101
shapiro.test(men_seeking_women$preferred_age)
## 
##  Shapiro-Wilk normality test
## 
## data:  men_seeking_women$preferred_age
## W = 0.93216, p-value = 0.01379

Based on the Shapiro-Wilk normality test results, the data for both men_seeking_women and women_seeking_men appear to be approximately normally distributed.

For the women_seeking_men (0), the test statistic is 0.97336 with a p-value of 0.4101. Since the p-value is greater than 0.05, we can conclude that there is not enough evidence to reject the null hypothesis of normality. This suggests that the preferred_age values for women seeking men may be normally distributed..

For the men_seeking_women (1), the test statistic is 0.93216 with a p-value of 0.01379. However, since the p-value is less than 0.05, we reject the null hypothesis of normality. This suggests that the preferred_age values for men seeking women are not normally distributed.

  1. Comment on whether the data fulfills the other assumptions of the t-test.

We need to use nonparametric t-tests because the data is does not have a normal distribution.

  1. Conduct the t-test and comment on what you would conclude from this test.
wilcox.test(preferred_age ~ sex, data=PartnerAge, paired=FALSE, exact=F)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  preferred_age by sex
## W = 715.5, p-value = 0.07097
## alternative hypothesis: true location shift is not equal to 0
  1. Write a communication block.

On average, men sought women at an older age (M = 0.593, SD = 5.99) than women sought men (M = -1.08, SD = 4.19). I conducted a non-parametric t-test (Wilcoxon rank sum test) to determine whether the means of the sexes’ ages was significantly different. This difference, -1.67, was significant w = 715.5, p = 0.07097. Since the p-value is greater than the significance level of 0.05, I fail to reject the null hypothesis. This means that there is not enough evidence to conclude that there is a significant difference in the preferred age between the two sexes.

Part 2:

Make a flowchart that visually explains how to conduct a t-test. Be sure to include: 1. Types of variables 2. Descriptive Statistics 3. What to do if assumptions aren’t met 4. How to make decisions regarding assumptions and significance

Independent T-test flow chart