1 Loading Libraries

# install any packages you have not previously used, then comment them back out.

#install.packages("car")
#install.packages("effsize")

library(psych) # for the describe() command
## Warning: package 'psych' was built under R version 4.5.3
library(car) # for the leveneTest() command
## Warning: package 'car' was built under R version 4.5.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(effsize) # for the cohen.d() command
## Warning: package 'effsize' was built under R version 4.5.3
## 
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
## 
##     cohen.d

2 Importing Data

d <- read.csv(file="projectdata.csv", header=T)

# For the HW, you will import the project dataset you cleaned previously
# This will be the dataset you'll use for HWs throughout the rest of the semester

3 State Your Hypothesis

There will be a significant difference in Narcissism by people’s level of Sibling Status, between having at least one sibling and being an only child.

4 Check Your Variables

# you **only** need to check the variables you're using in the current analysis

## Checking the Categorical variable (IV)

str(d)
## 'data.frame':    3126 obs. of  7 variables:
##  $ ResponseID  : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ sibling     : chr  "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
##  $ edu         : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ npi         : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
##  $ swb         : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ efficacy    : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
# if the categorical variable you're using is showing as a "chr" (character), you must change it to be a ** factor ** -- using the next line of code (as.factor)

 d$sibling<- as.factor(d$sibling)

str(d)
## 'data.frame':    3126 obs. of  7 variables:
##  $ ResponseID  : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ sibling     : Factor w/ 2 levels "at least one sibling",..: 1 1 1 1 1 1 1 2 1 1 ...
##  $ edu         : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ npi         : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
##  $ swb         : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ efficacy    : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
table(d$sibling, useNA = "always")
## 
## at least one sibling           only child                 <NA> 
##                 2826                  300                    0
## Checking the Continuous variable (DV)

# you can use the describe() command on an entire dataframe (d) or just on a single variable within your dataframe -- which we will do here

describe(d$npi)
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 3126 0.28 0.31   0.15    0.24 0.23   0   1     1 0.94    -0.69 0.01
# also use a histogram to visualize your continuous variable

hist(d$npi)

# 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$npi, group=d$sibling)
## 
##  Descriptive statistics by group 
## group: at least one sibling
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 2826 0.28 0.31   0.15    0.24 0.23   0   1     1 0.95    -0.66 0.01
## ------------------------------------------------------------ 
## group: only child
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 300  0.3 0.33   0.15    0.26 0.23   0   1     1  0.8    -1.02 0.02
# lastly, use a boxplot to examine your chosen continuous and categorical variables together

boxplot(d$npi~d$sibling)

5 Check Your Assumptions

5.1 T-test Assumptions

  • IV must have two levels
  • Data values must be independent (independent t-test only)
  • Data obtained via a random sample
  • Dependent variable must be normally distributed
  • Variances of the two groups are approx. equal
# If the IV has more than 2 levels, you must DROP any additional levels in order to meet the first assumption of a t-test.

## NOTE: This is a FOUR STEP process!

 d<- subset(d,sibling != "nb") # use subset() to remove all participants from the additional level

table(d$sibling, useNA = "always") # verify that now there are ZERO participants in the additional level
## 
## at least one sibling           only child                 <NA> 
##                 2826                  300                    0
 d$sibling<- droplevels(d$sibling) # use droplevels() to drop the empty factor

table(d$sibling, useNA = "always") # verify that now the entire factor level is removed 
## 
## at least one sibling           only child                 <NA> 
##                 2826                  300                    0
## Repeat ALL THE STEPS ABOVE if your IV has more levels that need to be DROPPED. Copy the 4 lines of code, and replace the level name in the subset() command.

5.2 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
# it 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(npi~sibling, data =d)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value Pr(>F)  
## group    1  2.7411 0.0979 .
##       3124                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Levene’s test revealed that my data does not have significantly different variances between the two comparison groups, at least one sibling and only child, on their levels of narcissism.

5.3 Issues with My Data

No notable issues with my data, as my independent variable only had two levels, and the homogeneity of variance was met with the Levene’s test.

6 Run a T-test

# Very simple! we use the same formula of y~x, where y is our DV and x is our IV

t_output <- t.test(d$npi~d$sibling)  # t_output will now show in your Global Environment

7 View Test Output

t_output
## 
##  Welch Two Sample t-test
## 
## data:  d$npi by d$sibling
## t = -1.2637, df = 356.92, p-value = 0.2072
## alternative hypothesis: true difference in means between group at least one sibling and group only child is not equal to 0
## 95 percent confidence interval:
##  -0.06372024  0.01386668
## sample estimates:
## mean in group at least one sibling           mean in group only child 
##                          0.2771245                          0.3020513

8 Calculate Cohen’s d - Effect Size

# once again, we use the same formula, y~x, to calculate cohen's d

# We **only** calculate effect size if the test is SIG!

d_output <- cohen.d(d$npi~d$sibling)  # d_output will now show in your Global Environment

9 View Effect Size

d_output
## 
## Cohen's d
## 
## d estimate: -0.08092213 (negligible)
## 95 percent confidence interval:
##       lower       upper 
## -0.19999857  0.03815431
## Remember to always take the ABSOLAUTE VALUE of the effect size value (i.e., it will never be negative)

10 Write Up Results

To test my hypothesis that only child participants in my sample would report significantly higher levels of Narcissism than participants with at least one sibling, I used an independent samples t-test. I tested the homogeneity of variance with Levene’s test, which verified homogeneity of variance (p = 098). I used Welch’s t-test to check for any potential issues around variance. My data met all assumptions of an independent samples t-test.

Unlike I predicted, I found that only child participants (M = 0.3, SD = 0.33) did not report significantly higher levels of narcissism than participants with siblings (M = .28, SD = 0.31); t(356.92) = -1.2637, p = .2072 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of 0.081 (negligible effect; Cohen, 1988).

References

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