title: “Correlation Homework”
author: “William Schultz”
date: “2026-11-08”
output:
html_document:
toc: true
toc_float:
toc_collapsed: true
toc_depth: 3
number_sections: true
editor_options:
chunk_output_type: console

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

Importing Data

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

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

State Your Hypothesis

There will be a significant relationship between Need to Belong Scale, Satisfaction with Life Scale and Markers of adulthood.Specifically, all three variables will be Positively related.

#In your HW, paste in your correlation hypothesis from your Hypothesis Activity assignment – make any necessary revisions based on grader feedback. Delete this reminder in your HW.]

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':    3070 obs. of  7 variables:
##  $ ResponseID      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ edu             : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ income          : chr  "1 low" "1 low" "rather not say" "rather not say" ...
##  $ belong          : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ swb             : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ npi             : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
# 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(belong, swb, moa_independence))

# 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
## belong              1 3070 3.24 0.60   3.30    3.25 0.59 1.3   5   3.7 -0.26
## swb                 2 3070 4.47 1.32   4.67    4.53 1.48 1.0   7   6.0 -0.36
## moa_independence    3 3070 3.54 0.47   3.67    3.61 0.49 1.0   4   3.0 -1.44
##                  kurtosis   se
## belong              -0.15 0.01
## swb                 -0.46 0.02
## moa_independence     2.51 0.01
# NOTE: Our fake variable has high kurtosis, which we'll ignore for the lab because we created it to be problematic. If you have high skew or kurtosis for any of your project variables, you will need to discuss it below in the Issues with My Data and Write up Results sections, as well as in your final project manuscript if your data does not meet the normality assumption.


# also use histograms to examine your continuous variables
# Because we are looking at 3 variables, we will have 3 histograms.

hist(d$belong)

hist(d$swb)

hist(d$moa_independence)

# last, use scatterplots to examine your continuous variables together, for each pairing
# because we are looking at 3 variables, we will have 3 pairings/plots. 

plot(d$belong, d$swb)

plot(d$belong, d$moa_independence)

plot(d$swb, d$moa_independence)

Check Your Assumptions

Pearson’s Correlation Coefficient Assumptions

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

Checking for Outliers

Note: For correlations, you will NOT screen out outliers or take any action based on what you see here. This is something you will simply check and then discuss in your write-up.We will learn how to removed outliers in later analyses.

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

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

sum(d2$belong < -3 | d2$belong > 3)
## [1] 6
d2$swb<- scale(d2$swb, center=T, scale=T)
hist(d2$swb)

sum(d2$swb < -3 | d2$swb > 3)
## [1] 0
d2$moa_independence<- scale(d2$moa_independence, center=T, scale=T)
hist(d2$moa_independence)

sum(d2$moa_independence < -3 | d2$moa_independence > 3)
## [1] 51

Issues with My Data

Two of my variables meet all of the assumptions of Pearson’s correlation coefficient. One variable, Markers of audulthood had high kurtosis (2.51) and 51 outliers. Outliers can distort the relationship between two variables and sway the correlation in their direction.

Run a Single Correlation

corr_output <- cor.test(d2$belong, d2$swb)

View Single Correlation

corr_output
## 
##  Pearson's product-moment correlation
## 
## data:  d2$belong and d2$swb
## t = -8.3423, df = 3068, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.1833422 -0.1141574
## sample estimates:
##        cor 
## -0.1489321

Create a Correlation Matrix

corr_output_m <- corr.test(d2)

View Test Output

corr_output_m
## Call:corr.test(x = d2)
## Correlation matrix 
##                  belong   swb moa_independence
## belong             1.00 -0.15             0.02
## swb               -0.15  1.00             0.10
## moa_independence   0.02  0.10             1.00
## Sample Size 
## [1] 3070
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##                  belong swb moa_independence
## belong             0.00   0             0.29
## swb                0.00   0             0.00
## moa_independence   0.29   0             0.00
## 
##  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!

Remember, Pearson’s r is also an effect size! We don’t report effect sizes for non-sig correlations.

Write Up Results

To test our hypothesis that Need to belong scale, Satisfaction with Life Scale, and Markers of Adulthood would be correlated with one another, we calculated a series of Pearson’s correlation coefficients. Two of the variables (Need to belong Scale and Satisfaction with Life Scale) met the required assumptions of the test, with both meeting the standards of normality and containing no outliers. One variable, Markers of Adulthood, had high kurtosis (2.51), 51 outliers.Any significant results involving Markers of Adulthood should be evaluated carefully.

We found that Need to Belong Scale and Satisfaction with Life Scale were significantly negatively correlated (r=-.15, p<.001), and Satisfaction with Life Scale and Markers of Adulthood were significantly positively correlated (r=.10, p<.001). Need to Belong Scale and Markers of Adulthood were not significantly correlated (r=.02, p=0.59). The effect sizes of the significant correlations were weak (Cohen, 1988).

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2
belong 3.24 0.60
swb 4.47 1.32 -.15**
[-.18, -.11]
moa_independence 3.54 0.47 .02 .10**
[-.02, .05] [.07, .14]
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.