1 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
library(broom) # for the augment() command
library(ggplot2) # to visualize our results
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha

2 Importing Data

# import the dataset you cleaned previously
# this will be the dataset you'll use throughout the rest of the semester
# use ARC data downloaded previous for lab
d <- read.csv(file="data/cleaned.csv", header=T)

3 Correlation Coefficient: State Your Hypothesis

We predict that support, social media usage, subjective well being, and need to belong will all be correlated with each other. Furthermore, we predict that social media usage will be higher in participants who report lower scores in support.

4 Correlation Coefficient: Check Your Variables

# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame':    3182 obs. of  6 variables:
##  $ race    : chr  "white" "white" "white" "other" ...
##  $ gender  : chr  "f" "m" "m" "f" ...
##  $ support : num  6 6.75 5.17 5.58 6 ...
##  $ SocMedia: num  4.27 2.09 3.09 3.18 3.36 ...
##  $ swb     : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ belong  : num  3.4 3.4 3.6 3.6 3.2 3.4 3.5 3.2 3.5 2.7 ...
# since we're focusing on our continuous variables, we're going to subset them into their own dataframe. this will make some stuff we're doing later easier.
cont <- subset(d, select=c(support, SocMedia, swb, belong))

# you can use the describe() command on an entire dataframe (d) or just on a single variable (d$pss)
describe(cont)
##          vars    n mean   sd median trimmed  mad min max range  skew kurtosis
## support     1 3176 5.53 1.13   5.75    5.65 0.99   1   7     6 -1.10     1.39
## SocMedia    2 3175 3.13 0.78   3.18    3.16 0.67   1   5     4 -0.31     0.26
## swb         3 3178 4.47 1.32   4.67    4.53 1.48   1   7     6 -0.36    -0.46
## belong      4 3175 3.31 0.49   3.30    3.33 0.44   1   5     4 -0.33     0.64
##            se
## support  0.02
## SocMedia 0.01
## swb      0.02
## belong   0.01
# our fake variable has high kurtosis, which I'll ignore. 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 manuscript

# also use histograms to examine your continuous variables
hist(d$support)

hist(d$SocMedia)

hist(d$swb)

hist(d$belong)

# last, use scatterplots to examine your continuous variables together
plot(d$support, d$SocMedia)

plot(d$support, d$swb)

plot(d$support, d$belong)

plot(d$SocMedia, d$swb)

plot(d$SocMedia, d$belong)

plot(d$swb, d$belong)

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 Correlation Coefficient: 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.

d$support_std <- scale(d$support, center=T, scale=T)
hist(d$support_std)

sum(d$support_std < -3 | d$support_std > 3)
## [1] NA
d$SocMedia_std <- scale(d$SocMedia, center=T, scale=T)
hist(d$SocMedia_std)

sum(d$SocMedia_std < -3 | d$SocMedia_std > 3)
## [1] NA
d$swb_std <- scale(d$swb, center=T, scale=T)
hist(d$swb_std)

sum(d$swb_std < -3 | d$swb_std > 3)
## [1] NA
d$belong_std <- scale(d$belong, center=T, scale=T)
hist(d$belong_std)

sum(d$belong_std < -3 | d$belong_std > 3)
## [1] NA
 d <- na.omit(d)

5.2 Correlation Coefficient: Issues with My Data

All but two of my variables met the assumptions for Pearson’s correlation coefficient. Variable “belong” and “support” was found to have outliers in the dataset, which is a violation of Pearson’s third assumption. “Support” has the most with 43 outliers, and “belong” has 17. Though this is the case, both variables seem to have skew and kurtosis values that fall within the range of univariate normality. These two variables should be taken into account when evaluated, due to their potential influence on distorting relationships.

6 Correlation Coefficient: Create a Correlation Matrix

corr_output_m <- corr.test(cont)

7 Correlation Coefficient: View Test Output

corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix 
##          support SocMedia   swb belong
## support     1.00     0.21  0.47   0.08
## SocMedia    0.21     1.00  0.11   0.28
## swb         0.47     0.11  1.00  -0.05
## belong      0.08     0.28 -0.05   1.00
## Sample Size 
##          support SocMedia  swb belong
## support     3176     3171 3173   3171
## SocMedia    3171     3175 3173   3170
## swb         3173     3173 3178   3173
## belong      3171     3170 3173   3175
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##          support SocMedia  swb belong
## support        0        0 0.00   0.00
## SocMedia       0        0 0.00   0.00
## swb            0        0 0.00   0.01
## belong         0        0 0.01   0.00
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option

8 Correlation Coefficient: Write Up Results

I used Pearson’s correlation coefficient to test our hypothesis that support, social media usage, subjective well being, and need to belong would be correlated with one another. Out of all the variables, two did not fufill all of Pearson’s assumptions of testing. Variable “support” and “belong were found to have outliars–”support” having the most out of the two. Despite this, both were still were in the range of univariate normality when looking at their skew and kurtosis values. Outliars may impact the significance of relationships, so keeping these variables in mind are essential to minimizing risks.

I found that all variables were significantly correlated (all ps < .001), except for one pair. Variables “swb” and “belong” showed a p-value of 0.01, meaning their correlation may not be as strongly supported as the others. The effect sizes of all correlations fell into the small and medium range (rs < .5; Cohen, 1988). Our second hypothesis predicts that social media usage will be higher in participants who report lower scores in support. The table shows that there is a positive correlation between these two variables, concluding that our second hypothesis is likely supported as well.

Table 1: Means, standard deviations, and correlations with confidence intervals
Variable M SD 1 2 3
Perceived Social Support 5.53 1.13
Social Media Use 3.13 0.78 .21**
[.17, .24]
Subjective Well Being 4.47 1.32 .47** .11**
[.45, .50] [.07, .14]
Need to belong 3.31 0.49 .08** .28** -.05**
[.04, .11] [.24, .31] [-.08, -.01]
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.

9 Simple Regression: State Your Hypothesis

We hypothesize that perceived social support will significantly predict subjective well-being (measured by the SWLS), and that the relationship will be positive.

10 Simple Regression: Check Your Variables

# you only need to check the variables you're using in the current analysis
# although you checked them previously, it's always a good idea to look them over again and be sure that everything is correct
str(d)
## 'data.frame':    3159 obs. of  10 variables:
##  $ race        : chr  "white" "white" "white" "other" ...
##  $ gender      : chr  "f" "m" "m" "f" ...
##  $ support     : num  6 6.75 5.17 5.58 6 ...
##  $ SocMedia    : num  4.27 2.09 3.09 3.18 3.36 ...
##  $ swb         : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ belong      : num  3.4 3.4 3.6 3.6 3.2 3.4 3.5 3.2 3.5 2.7 ...
##  $ support_std : num [1:3159, 1] 0.4135 1.0748 -0.3213 0.0461 0.4135 ...
##  $ SocMedia_std: num [1:3159, 1] 1.4625 -1.3347 -0.0526 0.0639 0.297 ...
##  $ swb_std     : num [1:3159, 1] -0.104 -0.23 -1.994 0.526 -0.608 ...
##  $ belong_std  : num [1:3159, 1] 0.179 0.179 0.583 0.583 -0.226 ...
##  - attr(*, "na.action")= 'omit' Named int [1:23] 61 185 199 304 325 421 511 728 743 1130 ...
##   ..- attr(*, "names")= chr [1:23] "61" "185" "199" "304" ...
# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(d)
##              vars    n mean   sd median trimmed  mad   min  max range  skew
## race*           1 3159 5.54 2.12   7.00    5.88 0.00  1.00 7.00  6.00 -0.98
## gender*         2 3159 1.28 0.49   1.00    1.21 0.00  1.00 3.00  2.00  1.40
## support         3 3159 5.54 1.13   5.75    5.66 0.99  1.00 7.00  6.00 -1.08
## SocMedia        4 3159 3.13 0.78   3.18    3.16 0.67  1.00 5.00  4.00 -0.31
## swb             5 3159 4.47 1.32   4.67    4.53 1.48  1.00 7.00  6.00 -0.36
## belong          6 3159 3.31 0.49   3.40    3.33 0.44  1.00 5.00  4.00 -0.31
## support_std     7 3159 0.00 0.99   0.19    0.11 0.87 -4.00 1.30  5.29 -1.08
## SocMedia_std    8 3159 0.00 1.00   0.06    0.03 0.86 -2.73 2.39  5.13 -0.31
## swb_std         9 3159 0.00 1.00   0.15    0.05 1.12 -2.62 1.91  4.54 -0.36
## belong_std     10 3159 0.00 1.00   0.18    0.03 0.90 -4.67 3.41  8.08 -0.31
##              kurtosis   se
## race*           -0.67 0.04
## gender*          0.89 0.01
## support          1.32 0.02
## SocMedia         0.26 0.01
## swb             -0.45 0.02
## belong           0.54 0.01
## support_std      1.32 0.02
## SocMedia_std     0.26 0.02
## swb_std         -0.45 0.02
## belong_std       0.54 0.02
# also use histograms to examine your continuous variables (iv = efficacy & dv = swb)
hist(d$support)

hist(d$swb)

# last, use scatterplots to examine your continuous variables together
plot(d$support, d$swb)

11 Simple Regression: Run a Simple Regression

# to calculate standardized coefficients, we have to standardize our IV
d$support_std <- scale(d$support, center=T, scale=T)
hist(d$support_std)

# use the lm() command to run the regression
# dependent/outcome variable on the left, idependent/predictor variable on the right
reg_model <- lm(swb ~ support_std, data = d)

12 Check Your Assumptions

12.1 Simple Regression 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
  • Residuals should be normal and have constant variance note: we will not be evaluating whether our data meets these assumptions in this lab/homework – we’ll come back to them next week when we talk about multiple linear regression

12.2 Simple Regression: Create plots and view residuals

model.diag.metrics <- augment(reg_model)

ggplot(model.diag.metrics, aes(x = support_std, y = swb)) +
  geom_point() +
  stat_smooth(method = lm, se = FALSE) +
  geom_segment(aes(xend = support_std, yend = .fitted), color = "red", size = 0.3)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'

12.3 Simple Regression: Check linearity with Residuals vs Fitted plot

This plot (below) shows the residuals for each case and the fitted line. The red line is the average residual for the specified point of the dependent variable. If the assumption of linearity is met, the red line should be horizontal. This indicates that the residuals average to around zero. You can see that for this lab, the plot shows some non-linearity because there are more datapoints below the regression line than here are above it. Thus, there are some negative residuals that don’t have positive residuals to cancel them out. However, a bit of deviation is okay – just like with skewness and kurtosis, there’s a range that we can work in before non-normality or non-linearity becomes a critical issue.

For some examples of good Residuals vs Fitted plot and ones that show serious errors, check out this page. I’ve included the images in our video and talk about them more in-depth there. But to summarize quickly, you can see the first case has a plot in which the red line sticks pretty closely to the zero line, while the other cases show some serious deviation. Ours is much closer to the ‘good’ plot than it is to the ‘serious issues’ plots. So we’ll consider our data okay and proceed with our analysis. Obviously, this is quite subjective. I’ll talk a bit about why this is in the video, but the key takeaway is that these evaluations are closely tied to the context of our sample, our data, and what we’re studying. It’s almost always a judgement call.

You’ll notice in the bottom right corner, there are some points with numbers included: these are cases or participants (indicated by row number) who have the most influence on the regression line (and so they might outliers).We’ll talk more about outliers in the next section.

To summarize: our plot suggests there is some minor non-linearity. For your homework, you’ll simply need to generate this plot and talk about how your plot compares to the good and problematic plots linked to above. Is it closer to the ‘good’ plots or one of the ‘bad’ plots? This is going to be a judgement call, and that’s okay! In practice, you’ll always be making these judgement calls as part of a team, so this assignment is just about getting experience with it, not making the perfect call.

plot(reg_model, 1)

12.4 Simple Regression: Check for outliers

The plots below both address leverage, or how much each data point is able to influence the regression line. Outliers are points that have undue influence on the regression line, the way that Bill Gates entering the room has an undue influence on the mean income.

The first plot, Cook’s distance, is a visualization of a score called (you guessed it) Cook’s distance, calculated for each case (aka row or participant) in the dataframe. Cook’s distance tells us how much the regression would change if the point was removed. Ideally, we want all points to have the same influence on the regression line, although we accept that there will be some variability. The cutoff for a high Cook’s distance score is .5 (not .05, which is our cutoff for statistical significance). For our data, some points do exert more influence than others but they’re generally equal, and none of them are close to the cutoff.

The second plot also includes the residuals in the examination of leverage. The standardized residuals are on the y-axis and leverage is on the x-axis; this shows us which points have high residuals (are far from the regression line) and high leverage. Point that have large residuals and high leverage are especially worrisome, because they are far from the regression line but are also exerting a large influence on it. The red line indicates the average residual across points with the same amount of leverage. As usual, we want this line to stay as close to the mean line (or the zero line) as possible.

Because the leverage in our plot is low, part of it actually cut off! If you check the first set of plots on this page (note that Residuals vs Leverage is the fourth in the grid) you can see there are curved red lines in the corners of the Residuals vs Leverage plots. This is the .5 cutoff for Cook’s distance, and so any points appearing past these lines is a serious outlier that needs to be removed. On this page you can also see Residuals vs Leverage plots with severe deviations from the mean line, which makes our deviations appear much less serious.

Our data doesn’t have any severe outliers. For your homework, you’ll simply need to generate these plots, assess Cook’s distance in your dataset, and then identify any potential cases that are prominent outliers. Since we have some cutoffs, that makes this process is a bit less subjective than some of the other assessments we’ve done here, which is a nice change!

# Cook's distance
plot(reg_model, 4)

# Residuals vs Leverage
plot(reg_model, 5)

12.5 Simple Regression: Issues with My Data

When testing for a simple linear regression, I found that my variables are slightly deviated from being linear. This is shown in the Residuals vs Leverage plot, where the data points are shown to not fall within the middle near the right side of the graph. These points can also be interpreted as outliers. When checking Cook’s distance, I found more evidence of a few potential outliers, though they are not extreme when you compare them to the overall graph. They also fall below the cut off for Cook’s distance, so they can still be used relevant to the data.

13 Simple Regression: View Test Output

summary(reg_model)
## 
## Call:
## lm(formula = swb ~ support_std, data = d)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.0550 -0.7860  0.0918  0.8228  4.3936 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.47473    0.02072  215.96   <2e-16 ***
## support_std  0.62417    0.02072   30.12   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.165 on 3157 degrees of freedom
## Multiple R-squared:  0.2232, Adjusted R-squared:  0.223 
## F-statistic: 907.1 on 1 and 3157 DF,  p-value: < 2.2e-16
# note for section below: to type lowercase Beta below (ß) you need to hold down Alt key and type 225 on numeric keypad. If that doesn't work you should be able to copy/paste it from somewhere else

14 Simple Regression: Write Up Results

To test our hypothesis that perceived social support will significantly predict subjective well-being (measured by the SWLS), and that the relationship will be positive, we used a simple linear regression to model the relationship between the variables. We confirmed that our data met the assumptions of a linear regression, checking the linearity of the relationship using a Residuals vs Fitted plot and checking for outliers using Cook’s distance and a Residuals vs Leverage plot. Note: we are skipping the assumptions of normality and homogeneity of variance for this assignment.

A simple linear regression was used to evaluate our hypothesis that there will be a positive relationship on how perceived social support will predict subjective well-being (measured by the SWLS). We first screened our variables to meet the assumptions of a simple linear regression using Cook’s distance and Residuals vs Leverage plot. Potential outliers were assessed and found to not surpass the cutoff. From this, it was safe to assume that all variables met the assumptions to continue with a simple linear regression.

Our hypothesis was supported as perceived social support significantly predicted subjective well being. Adj. R2 = .22, F(1,3157) = 907.1, p < .001. Perceived social support and subjective well being was also found to have a positive relationship, ß = .62, t(3157) = 30.12, p < .001 (refer to Figure 1). According to Cohen (1988), this constitutes a large effect size (> .50).

References

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