Correlation HW

Author

Riley Midkiff

Loading Libraries

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/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, mhealth))

State Your Hypotheses - PART OF YOUR WRITEUP

We predict that openness, mental flexibility, and brief resilience will be positively correlated, and that all three of these variables will be negatively correlated with worry.

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 blew)
  • 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$big5_open <- scale(d$big5_open, center=T, scale=T)
hist(d$big5_open)

sum(d$big5_open < -3 | d$big5_open > 3)
[1] 1
d$pswq <- scale(d$pswq, center=T, scale=T)
hist(d$pswq)

sum(d$pswq < -3 | d$pswq > 3)
[1] 0
d$mfq_state <- scale(d$mfq_state, center=T, scale=T)
hist(d$mfq_state)

sum(d$mfq_state < -3 | d$mfq_state > 3)
[1] 0
d$brs <- scale(d$brs, center=T, scale=T)
hist(d$brs)

sum(d$brs < -3 | d$brs > 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$big5_open, d$pswq)

plot(d$big5_open, d$mfq_state)

plot(d$big5_open, d$brs)

plot(d$pswq, d$mfq_state)

plot(d$pswq, d$brs)

plot(d$mfq_state, d$brs)

Check Your Variables

describe(d)
          vars   n mean sd median trimmed  mad   min  max range  skew kurtosis
big5_open    1 408    0  1   0.04    0.08 0.89 -3.85 1.53  5.38 -0.78     0.61
pswq         2 408    0  1   0.12    0.06 1.03 -2.65 1.84  4.50 -0.45    -0.63
mfq_state    3 408    0  1   0.02    0.03 1.10 -2.71 2.25  4.96 -0.29    -0.28
brs          4 408    0  1  -0.03    0.00 1.12 -1.92 2.61  4.53  0.07    -0.68
            se
big5_open 0.05
pswq      0.05
mfq_state 0.05
brs       0.05
# also use histograms to examine your continuous variables
hist(d$big5_open)

hist(d$pswq)

hist(d$mfq_state)

hist(d$brs)

Issues with My Data - PART OF YOUR WRITEUP

We found one outlier in the openness variable. There is indication of non-linearity in pairings involved with the openness variable.

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$brs, d$pswq)

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_output
Call:corr.test(x = d$brs, y = d$pswq)
Correlation matrix 
      [,1]
[1,] -0.61
Sample Size 
[1] 408
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_m
Call:corr.test(x = d)
Correlation matrix 
          big5_open  pswq mfq_state   brs
big5_open      1.00 -0.05      0.18  0.11
pswq          -0.05  1.00     -0.53 -0.61
mfq_state      0.18 -0.53      1.00  0.67
brs            0.11 -0.61      0.67  1.00
Sample Size 
[1] 408
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
          big5_open pswq mfq_state  brs
big5_open      0.00 0.33         0 0.06
pswq           0.33 0.00         0 0.00
mfq_state      0.00 0.00         0 0.00
brs            0.03 0.00         0 0.00

 To see confidence intervals of the correlations, print with the short=FALSE option

Write Up Results

We expect that participants who are more open, mentally flexible, and resilient will experience less worry. We found one outlier in the openness variable. There is indication of non-linearity in pairings involved with the openness variable. As expected our positive mental aspects were positively correlated with each other and were negatively correlated with worry in Table 1. For the significant results, brief resilience paired with worry and mental flexibility had a strong effect and a p value = 0. Mental flexibility paired with worry had a strong effect and p value = 0. Mental flexibility paired with openness had a weak effect and p value = 0. Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Openness (BIG5_OPEN) -0.00 1.00
Worry (PSWQ) 0.00 1.00 -.05
[-.14, .05]
Mental Flexibility (MFQ_STATE) 0.00 1.00 .18** -.53**
[.09, .28] [-.60, -.46]
Brief Resilience (BRS) -0.00 1.00 .11* -.61** .67**
[.01, .20] [-.67, -.55] [.61, .72]
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.