1 Loading Libraries

#install.packages("apaTables")
#install.packages("kableExtra")

library(psych) # for the describe() command and the corr.test() command
library(apaTables) # to create our correlation table
library(kableExtra) # to create our correlation table

2 Importing Data

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

# For HW, import the your project data you cleaned previously; this will be the dataset you'll use throughout the rest of the semester

3 State Your Hypothesis

We predict that maturity and self efficacy will be correlated with each other.

4 Check Your Variables

# you only need to check the variables you're using in the current analysis
# it's always a good idea to look them to be sure that everything is correct
str(d)
## 'data.frame':    3116 obs. of  7 variables:
##  $ ResponseID  : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ income      : chr  "1 low" "1 low" "rather not say" "rather not say" ...
##  $ edu         : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ swb         : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ efficacy    : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ stress      : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...
# Since we're focusing only on our continuous variables, we're going to subset them into their own dataframe. This will make some stuff we're doing later on easier.

d2 <- subset(d, select=c(moa_maturity, efficacy))

# You can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)

describe(d2)
##              vars    n mean   sd median trimmed  mad min max range  skew
## moa_maturity    1 3116 3.59 0.43   3.67    3.65 0.49 1.0   4   3.0 -1.20
## efficacy        2 3116 3.13 0.45   3.10    3.13 0.44 1.1   4   2.9 -0.24
##              kurtosis   se
## moa_maturity     1.89 0.01
## efficacy         0.44 0.01
# NOTE: Our fake variable has high kurtosis, which we'll ignore for the lab. You don't need to discuss univariate normality in the results write-ups for the labs/homework, but you will need to discuss it in your final project manuscript.

# also use histograms to examine your continuous variables

# because we are looking at 4 variables, we will have 4 histograms. You may not have this many for your HW. Make as many as you need to reflect your hypothesis.

hist(d2$moa_maturity)

hist(d2$efficacy)

# last, use scatterplots to examine your continuous variables together, for each pairing

# because we are looking at 4 variables, we will have 6 pairings/plots. You may not have this many for your HW. Make as many as you need to reflect your hypothesis.

plot(d2$moa_maturity, d2$efficacy)

5 Check Your Assumptions

5.1 Pearson’s Correlation Coefficient Assumptions

  • Should have two measurments for each participant.
  • Variables should be continuous and normally distributed.
  • Outliers should be identified and removed.
  • Relationship between the variables should be linear.

5.1.1 Checking for Outliers

Note: You are NOT REQUIRED to screen out outliers or take any action based on what you see here. This is something you will always check and then discuss in your write-up.

# We are going to standardize (z-score) all of our 4 variables, and check them for outliers.

d2$moa_maturity <- scale(d2$moa_maturity, center=T, scale=T)
hist(d2$moa_maturity)

sum(d2$moa_maturity < -3 | d2$moa_maturity > 3)
## [1] 22
d2$efficacy <- scale(d2$efficacy, center=T, scale=T)
hist(d2$efficacy)

sum(d2$efficacy < -3 | d2$worry > 3)
## [1] 0

5.2 Issues with My Data

One of my variables, self efficacy met all of the assumptions of Pearson’s correlation coefficient. One variable, a measure of maturity had 22 outliers. Outliers can distort the relationship between two variables and sway the correlation in their direction. Both of the variables have appropriate skew and kurtosis.

[Make sure to revise the above paragraph for your HW.]

6 Run a Single Correlation

corr_output <- corr.test(d2$moa_maturity, d2$efficacy)

7 View Single Correlation

corr_output
## Call:corr.test(x = d2$moa_maturity, y = d2$efficacy)
## Correlation matrix 
##      [,1]
## [1,] 0.17
## Sample Size 
## [1] 3116
## These are the unadjusted probability values.
##   The probability values  adjusted for multiple tests are in the p.adj object. 
##      [,1]
## [1,]    0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option

8 Create a Correlation Matrix

Strong: Between |0.50| and |1| Moderate: Between |0.30| and |0.49| Weak: Between |0.10| and |0.29| Trivial: Less than |0.09|

Remember, Pearson’s r is also an effect size!

corr_output_m <- corr.test(d2)

9 View Test Output

corr_output_m
## Call:corr.test(x = d2)
## Correlation matrix 
##              moa_maturity efficacy
## moa_maturity         1.00     0.17
## efficacy             0.17     1.00
## Sample Size 
## [1] 3116
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##              moa_maturity efficacy
## moa_maturity            0        0
## efficacy                0        0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option
# Remember to report the p-values from the matrix that are ABOVE the diagonal

10 Write Up Results

To test our hypothesis that maturity and self efficacy would be correlated with one another, we calculated a series of Pearson’s correlation coefficients. Self efficacy met the required assumptions of the test, with meeting the standards of normality and containing no outliers. One variable, maturity, had 22 outliers, however, both variables had appropriate skew and kurtosis.

As predicted, we found that both variables were significantly correlated (ps < .001). The effect size of the correlation was weak (rs > .5; Cohen, 1988). Results can be seen in table 1.

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1
Maturity 3.59 0.43
Self Efficacy 3.13 0.45 .17**
[.14, .21]
Note:
M and SD are used to represent mean and standard deviation, respectively. Values in square brackets indicate the 95% confidence interval. The confidence interval is a plausible range of population correlations that could have caused the sample correlation.
* indicates p < .05
** indicates p < .01.

References

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