T-Test HW

Author

Regina Cai

Loading Libraries

library(psych) # for the describe() command
library(car) # for the leveneTest() command
library(effsize) # for the cohen.d() command

Importing Data

d <- read.csv(file="Data/mydata.csv", header=T)

State Your Hypothesis - PART OF YOUR WRITEUP

There is a significant difference in mindfulness between individuals with high income and those with low income.

Check Your Assumptions

T-test Assumptions

  • Data values must be independent (independent t-test only) (confirmed by data report)
  • Data obtained via a random sample (confirmed by data report)
  • IV must have two levels (will check below)
  • Dependent variable must be normally distributed (will check below. if issues, note and proceed)
  • Variances of the two groups must be approximately equal, aka ‘homogeneity of variance’. Lacking this makes our results inaccurate (will check below - this really only applies to Student’s t-test, but we’ll check it anyway)

Checking IV levels

# preview the levels and counts for your IV
table(d$income, useNA = "always")

         1 low       2 middle         3 high rather not say           <NA> 
           878            873            534            855              0 
# # note that the table() output shows you exactly how the levels of your variable are written. when recoding, make sure you are spelling them exactly as they appear
# 
# # to drop levels from your variable
# # this subsets the data and says that any participant who is coded as 'LEVEL BAD' should be removed
# # if you don't need this for the homework, comment it out (add a # at the beginning of the line)
d <- subset(d, income != "2 middle")
d <- subset(d, income != "rather not say")

# # to combine levels
# # this says that where any participant is coded as 'LEVEL BAD' it should be replaced by 'LEVEL GOOD'
# # you can repeat this as needed, changing 'LEVEL BAD' if you have multiple levels that you want to combine into a single level
# # if you don't need this for the homework, comment it out (add a # at the beginning of the line)
# d$mindful[d$mindful == "anxiety disorder"] <- "mental health diagnosis"
# d$mindful[d$mindful == "bipolar"] <- "mental health diagnosis"
# d$mindful[d$mindful == "depression"] <- "mental health diagnosis"
# d$mindful[d$mindful == "eating disorders"] <- "mental health diagnosis"
# d$mindful[d$mindful == "obsessive compulsive disorder"] <- "mental health diagnosis"
# d$mindful[d$mindful == "other"] <- "mental health diagnosis"

# # preview your changes and make sure everything is correct
table(d$income, useNA = "always")

 1 low 3 high   <NA> 
   878    534      0 
# 
# # check your variable types
str(d)
'data.frame':   1412 obs. of  6 variables:
 $ mindful  : num  2.4 1.8 4.13 3 3.4 ...
 $ swb      : num  4.33 4.17 3.67 5.5 5 ...
 $ socmeduse: int  47 23 37 43 37 35 26 26 26 21 ...
 $ race_rc  : chr  "white" "white" "white" "white" ...
 $ idea     : num  3.75 3.88 3.5 3 3.38 ...
 $ income   : chr  "1 low" "1 low" "1 low" "1 low" ...
# 
# # make sure that your IV is recognized as a factor by R
d$income <- as.factor(d$income)

Testing Homogeneity of Variance with Levene’s Test

We can test whether the variances of our two groups are equal using Levene’s test. The null hypothesis is that the variance between the two groups is equal, which is the result we want. So when running Levene’s test we’re hoping for a non-significant result!

# # use the leveneTest() command from the car package to test homogeneity of variance
# # uses the same 'formula' setup that we'll use for our t-test: formula is y~x, where y is our DV and x is our IV
leveneTest (mindful~income, data = d)
Levene's Test for Homogeneity of Variance (center = median)
        Df F value Pr(>F)
group    1   0.422  0.516
      1410               

This is more of a formality in our case, because we are using Welch’s t-test, which does not have the same assumptions as Student’s t-test (the default type of t-test) about variance. R defaults to using Welch’s t-test so this doesn’t require any extra effort on our part!

Check Normality

# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct

# you can use the describe() command on an entire datafrom (d) or just on a single variable (d$pss)
# use it to check the skew and kurtosis of your DV
describe(d$mindful)
   vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 1412 3.69 0.85   3.67     3.7 0.89 1.2   6   4.8 -0.1    -0.22 0.02
# can use the describeBy() command to view the means and standard deviations by group
# it's very similar to the describe() command but splits the dataframe according to the 'group' variable
describeBy(d$mindful, group=d$income)

 Descriptive statistics by group 
group: 1 low
   vars   n mean   sd median trimmed  mad min  max range  skew kurtosis   se
X1    1 878  3.7 0.83   3.73    3.71 0.89 1.2 5.73  4.53 -0.13    -0.22 0.03
------------------------------------------------------------ 
group: 3 high
   vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 534 3.68 0.87   3.67    3.68 0.89 1.4   6   4.6 -0.04    -0.23 0.04
# also use a histogram to examine your continuous variable
hist(d$mindful)

# last, use a boxplot to examine your continuous and categorical variables together
# categorical/IV goes on the right, continuous/DV goes on the left
boxplot(d$mindful~d$income)

Issues with My Data - PART OF YOUR WRITEUP

“We dropped participants who are in the ‘2 middle’ and ‘rather not say’ in our independent variable, income. We also confirmed homogeneity of variance using Levene’s test (p = .516) and that our dependent variable is normally distributed (skew and kurtosis between -2 and +2).”

Run a T-test

# # very simple! we specify the dataframe alongside the variables instead of having a separate argument for the dataframe like we did for leveneTest()
t_output <- t.test(d$mindful~d$income)

View Test Output

t_output

    Welch Two Sample t-test

data:  d$mindful by d$income
t = 0.46783, df = 1090.7, p-value = 0.64
alternative hypothesis: true difference in means between group 1 low and group 3 high is not equal to 0
95 percent confidence interval:
 -0.07017112  0.11410879
sample estimates:
 mean in group 1 low mean in group 3 high 
            3.697874             3.675905 

Calculate Cohen’s d

# # once again, we use our formula to calculate cohen's d
d_output <- cohen.d(d$mindful~d$income)

View Effect Size

  • Trivial: < .2
  • Small: between .2 and .5
  • Medium: between .5 and .8
  • Large: > .8
d_output

Cohen's d

d estimate: 0.02592457 (negligible)
95 percent confidence interval:
      lower       upper 
-0.08173129  0.13358043 

Write Up Results

“We tested our hypothesis that there is significant difference in mindfulness between individuals with high income and those with low income using an independent samples t-test. Our data met all of the assumptions of a t-test, however, we did not find a significant difference, t(1090.7) = 0.47, p = .64, d = .03, 95% [-0.08, .13] (refer to Figure 1).”

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.