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 dataset you cleaned previously; this will be the dataset you'll use throughout the rest of the semester

3 State Your Hypothesis

I predict that general self efficacy will significantly predict narcissistic personality and the relationship will be positive. Additionally I predict there will be a significant relationship between inventory of the dimensions of emerging adulthood and the need to belong scale.

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':    3155 obs. of  7 variables:
##  $ ResponseId: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender    : chr  "f" "m" "m" "f" ...
##  $ sibling   : chr  "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
##  $ idea      : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ belong    : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ efficacy  : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ 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(idea, belong, efficacy, npi))

# 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 kurtosis
## idea        1 3155 3.57 0.38   3.62    3.62 0.37 1.0   4   3.0 -1.49     4.14
## belong      2 3155 3.23 0.60   3.30    3.25 0.59 1.3   5   3.7 -0.26    -0.12
## efficacy    3 3155 3.13 0.45   3.10    3.13 0.44 1.1   4   2.9 -0.26     0.51
## npi         4 3155 0.28 0.31   0.15    0.24 0.23 0.0   1   1.0  0.94    -0.69
##            se
## idea     0.01
## belong   0.01
## efficacy 0.01
## npi      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

hist(d$idea)

hist(d$belong)

hist(d$efficacy)

hist(d$npi)

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

plot(d$idea, d$belong)

plot(d$idea, d$efficacy)

plot(d$idea, d$npi)

plot(d$belong, d$efficacy)

plot(d$belong, d$npi)

plot(d$efficacy, d$npi)

5 Check Your Assumptions

5.1 Pearson’s Correlation Coefficient Assumptions

  • Should have two measurements 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 check and then discuss in your write-up.

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

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

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

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

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

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

5.2 Issues with My Data

Three of my variables had outliers. Idea had 35 outliers, Belong had 7 outliers, and Efficacy had 16 outliers. Additionally, idea had a h ad high kurtosis (4.14). Outliers can distort the relationship between two variables and sway the correlation in their direction. Pearson’s r may underestimate the strength of a non-linear relationship and distort the relationship direction. Any correlations with my variables idea, belong, and efficacy should be evaluated carefully due to these risks. Due to the measurement of these variables it does not appear that any of these correlations were truly linear.

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

6 Run a Single Correlation

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

7 View Single Correlation

corr_output
## Call:corr.test(x = d2$efficacy, y = d2$npi)
## Correlation matrix 
##      [,1]
## [1,] 0.17
## Sample Size 
## [1] 3155
## 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 
##          idea belong efficacy   npi
## idea     1.00   0.22     0.04  0.01
## belong   0.22   1.00    -0.26 -0.06
## efficacy 0.04  -0.26     1.00  0.17
## npi      0.01  -0.06     0.17  1.00
## Sample Size 
## [1] 3155
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##          idea belong efficacy npi
## idea     0.00      0     0.08 0.5
## belong   0.00      0     0.00 0.0
## efficacy 0.04      0     0.00 0.0
## npi      0.50      0     0.00 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 my hypothesis that efficacy and belong were significantly related with a positive relationship, we calculated a series of Pearson’s correlation coefficients. The same Pearson’s correlation coefficient calcualtions were performed to test my second hypothesis that idea and npi are significantly related. Three of the variables (idea, belong, and efficacy) did not meet the required assumptions of the test, with all three meeting the standards of normality and containing outliers (35, 7, 16 respectively). Any significant results involving idea, belong, and efficacy should be evaluated carefully. There also did not appear to be any linear relationships between any of the variables.

Three of the variables were significantly correlated (all ps < .001), however idea and npi are not (p-value=0.5). The effect sizes for all tests except efficacy and belong were small (rs<0.2; Cohen, 1988). For belong and efficacy the effect size was medium (0.5>rs>0.2; Cohen, 1988).

[In your HW, revise the above two paragraphs to fit your results. Make sure to discuss ALL predicted correlations and if sig or not.]

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Ineventory of Dimensions of Emerging Adulthood 3.57 0.38
Need to Belong 3.23 0.60 .22**
[.19, .26]
General Self Efficacy 3.13 0.45 .04* -.26**
[.00, .07] [-.29, -.23]
Narcissitc Personality 0.28 0.31 .01 -.06** .17**
[-.02, .05] [-.10, -.03] [.13, .20]
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.