library(psych) # for the describe() command and the corr.test() command
library(apaTables) # to create our correlation table
library(kableExtra) # to create our correlation tableCorrelation HW
Loading Libraries
Importing Data
d <- read.csv(file="Data/mydata.csv", header=T)
#
# since we're focusing on our continuous variables, we're going to drop our categorical variables. this will make some stuff we're doing later easier.
d <- subset(d, select=-c(gender, sibling))State Your Hypotheses - PART OF YOUR WRITEUP
We predict that the need to belong and social media usage is positively correlated with each other, while narcissism and perceived stress are negatively correlated with each other.
State your hypotheses. Remember, you are looking at the correlations between all four of your continuous/quantitatve variables. Depending on what you predict, you might need to write several sentences to describe all of the relationships. Make sure all four variables are included.
Check Your Assumptions
Pearson’s Correlation Coefficient Assumptions
- Should have two measurements for each participant for each variable (confirmed by earlier procedures – we dropped any participants with missing data)
- Variables should be continuous and normally distributed, or assessments of the relationship may be inaccurate (will do below and check for skew and kurtosis)
- Outliers should be identified and removed, or results will be inaccurate (will do below)
- Relationship between the variables should be linear, or they will not be detected (will do below)
Checking for Outliers
Outliers can mask potential effects and cause Type II error (you assume there is no relationship when there really is one, e.g., false negative).
Note: You are not required to screen out outliers or take any action based on what you see here. This is something you will check and then discuss in your write-up.
# using the scale() command to standardize our variable, viewing a histogram, and then counting statistical outliers
d$npi <- scale(d$npi, center=T, scale=T)
hist(d$npi)sum(d$npi < -3 | d$npi > 3)[1] 0
d$belong <- scale(d$belong, center=T, scale=T)
hist(d$belong)sum(d$belong < -3 | d$belong > 3)[1] 7
d$socmeduse <- scale(d$socmeduse, center=T, scale=T)
hist(d$socmeduse)sum(d$socmeduse < -3 | d$socmeduse > 3)[1] 0
d$stress <- scale(d$stress, center=T, scale=T)
hist(d$stress)sum(d$stress < -3 | d$stress > 3)[1] 0
Checking for Linear Relationships
Non-linear relationships cannot be detected by Pearson’s correlation (the type of correlation we’re doing here). This means that you may underestimate the relationship between a pair of variables if they have a non-linear relationship, and thus your understanding of what’s happening in your data will be inaccurate.
Visually check that relationships are linear and write a brief description of any potential nonlinearity. You will have to use your judgement. There are no penalties for answering ‘wrong’, so try not to stress out about it too much – just do your best.
# use scatterplots to examine your continuous variables together
plot(d$npi, d$belong)plot(d$npi, d$socmeduse)plot(d$npi, d$stress)plot(d$belong, d$socmeduse)plot(d$belong, d$stress)plot(d$socmeduse, d$stress)Check Your Variables
describe(d) vars n mean sd median trimmed mad min max range skew kurtosis
npi 1 3152 0 1 -0.41 -0.13 0.74 -0.91 2.34 3.25 0.94 -0.68
belong 2 3152 0 1 0.11 0.03 0.98 -3.20 2.92 6.12 -0.26 -0.12
socmeduse 3 3152 0 1 0.06 0.03 0.86 -2.73 2.39 5.13 -0.31 0.27
stress 4 3152 0 1 -0.09 0.00 0.99 -2.92 2.75 5.67 0.03 -0.17
se
npi 0.02
belong 0.02
socmeduse 0.02
stress 0.02
# also use histograms to examine your continuous variables
hist(d$npi)hist(d$belong)hist(d$socmeduse)hist(d$stress)Issues with My Data - PART OF YOUR WRITEUP
We found no outliers for the variables involving narcissism, social media usage, and stress. However, we did find 7 outliers in the need to belong variable.
Make a note right here if you have any outliers. Make sure you describe what variable the outliers are in and how many there are.
All assumptions were met and there were no clear indications of non-linearity (I think. My plots look kind of crazy.)
Make a note here if you have any indications of non-linearity in your variable pairings.
Skew and kurtosis were normal (between -2 and 2), therefore we proceeded with Pearson’s Correlation.
Make a note here if you have any skew/kurtosis and describe what variables it occurs in.
Briefly describe any issues with your data and how they might impact the interpretation of your results. As usual, this should be written in an appropriate scientific tone.
Run Pearson’s Correlation
There are two ways to run Pearson’s correlation in R. You can calculate each correlation one-at-a-time using multiple commands, or you can calculate them all at once and report the scores in a matrix. The matrix output can be confusing at first, but it’s more efficient. We’ll do it both ways.
Run a Single Correlation
corr_output <- corr.test(d$belong, d$socmeduse)View Single Correlation
Strong effect: Between |0.50| and |1| Moderate effect: Between |0.30| and |0.49| Weak effect: Between |0.10| and |0.29| Trivial effect: Less than |0.09|
corr_outputCall:corr.test(x = d$belong, y = d$socmeduse)
Correlation matrix
[,1]
[1,] 0.28
Sample Size
[1] 3152
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
Create a Correlation Matrix
corr_output_m <- corr.test(d)View Test Output
Strong effect: Between |0.50| and |1| Moderate effect: Between |0.30| and |0.49| Weak effect: Between |0.10| and |0.29| Trivial effect: Less than |0.09|
corr_output_mCall:corr.test(x = d)
Correlation matrix
npi belong socmeduse stress
npi 1.00 -0.06 0.06 -0.06
belong -0.06 1.00 0.28 0.30
socmeduse 0.06 0.28 1.00 0.09
stress -0.06 0.30 0.09 1.00
Sample Size
[1] 3152
Probability values (Entries above the diagonal are adjusted for multiple tests.)
npi belong socmeduse stress
npi 0 0 0 0
belong 0 0 0 0
socmeduse 0 0 0 0
stress 0 0 0 0
To see confidence intervals of the correlations, print with the short=FALSE option
Write Up Results
We predicted that the need to belong and social media usage are positively correlated with each other, while narcissism and stress are negatively correlated with each other. We found no outliers for the variables involving narcissism, social media usage, and stress. However, we did find 7 outliers in the need to belong variable. All assumptions were met and there were no clear indications of non-linearity (I think. My plots look kind of crazy.) Skew and kurtosis were normal (between -2 and 2), therefore we proceeded with Pearson’s Correlation. Both parts of our hypothesis was supported by our data, finding significant results between each variable pairing (p < .001). Full data values and confidence intervals can be found in Table 1. Our effect size for the relationship between social media usage and the need to belong was weak, whereas it was trivial for the relationship between narcissism and stress (Cohen, 1988).
| Variable | M | SD | 1 | 2 | 3 |
|---|---|---|---|---|---|
| Narcissism | -0.00 | 1.00 | |||
| Need to Belong | -0.00 | 1.00 | -.06** | ||
| [-.10, -.03] | |||||
| Social Media Usage | 0.00 | 1.00 | .06** | .28** | |
| [.02, .09] | [.25, .31] | ||||
| Stress | -0.00 | 1.00 | -.06** | .30** | .09** |
| [-.10, -.03] | [.27, .33] | [.06, .13] | |||
| 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.