1 Loading Libraries

library(psych) # for the describe() command
## Warning: package 'psych' was built under R version 4.2.3
library(car) # for the vif() command
## Warning: package 'car' was built under R version 4.2.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.2.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:psych':
## 
##     logit
library(sjPlot) # to visualize our results

2 Importing Data

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

3 State Your Hypothesis

We hypothesize that mental flexibility as a state (measured by the mfq_state), mental flexibility as a trait (measured by the mfq_26), and neuroticism (measured by big5_neu) will significantly predict the pandemic anxiety scale (measured by the pas_covid).

4 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':    1207 obs. of  6 variables:
##  $ gender   : chr  "female" "male" "female" "female" ...
##  $ mhealth  : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ big5_neu : num  6 6.67 4 4 2.67 ...
##  $ mfq_26   : num  4.2 3.35 4.65 4.65 4.5 4.3 5.25 4.45 5 4.7 ...
##  $ mfq_state: num  3.62 3 5.88 4 4.62 ...
##  $ pas_covid: num  3.22 4.56 3.33 4.22 3.22 ...
cont <- na.omit(subset(d, select=c(big5_neu, mfq_26, mfq_state, pas_covid)))
cont$mfq_26 <- scale(cont$mfq_26, center=T, scale=T)
cont$mfq_state <- scale(cont$mfq_state, center=T, scale=T)
cont$pas_covid <- scale(cont$pas_covid, center=T, scale=T)

# you can use the describe() command on an entire dataframe (d) or just on a single variable
describe(cont)
##           vars    n mean   sd median trimmed  mad   min  max range  skew
## big5_neu     1 1207 4.36 1.52   4.67    4.40 1.48  1.00 7.00  6.00 -0.28
## mfq_26       2 1207 0.00 1.00   0.05    0.03 1.00 -3.76 2.51  6.27 -0.34
## mfq_state    3 1207 0.00 1.00   0.14    0.06 0.94 -3.16 1.92  5.08 -0.57
## pas_covid    4 1207 0.00 1.00  -0.01    0.02 0.97 -3.27 2.59  5.86 -0.18
##           kurtosis   se
## big5_neu     -0.78 0.04
## mfq_26        0.22 0.03
## mfq_state     0.25 0.03
## pas_covid    -0.02 0.03
# also use histograms to examine your continuous variables
hist(cont$big5_neu)

hist(cont$mfq_26)

hist(cont$mfq_state)

hist(cont$pas_covid)

# last, use scatterplots to examine your continuous variables together
plot(cont$big5_neu, cont$mfq_26)

plot(cont$big5_neu, cont$mfq_state)

plot(cont$big5_neu, cont$pas_covid)

plot(cont$mfq_26, cont$mfq_state)

plot(cont$mfq_26, cont$pas_covid)

plot(cont$pas_covid, cont$mfq_state)

5 View Your Correlations

corr_output_m <- corr.test(cont)
corr_output_m
## Call:corr.test(x = cont)
## Correlation matrix 
##           big5_neu mfq_26 mfq_state pas_covid
## big5_neu      1.00  -0.54     -0.56      0.33
## mfq_26       -0.54   1.00      0.76     -0.19
## mfq_state    -0.56   0.76      1.00     -0.25
## pas_covid     0.33  -0.19     -0.25      1.00
## Sample Size 
## [1] 1207
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##           big5_neu mfq_26 mfq_state pas_covid
## big5_neu         0      0         0         0
## mfq_26           0      0         0         0
## mfq_state        0      0         0         0
## pas_covid        0      0         0         0
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option

6 Run a Multiple Linear Regression

# use this commented out section only if you need to remove outliers
# to drop a single outlier, remove the # at the beginning of the line and use this code:
cont$row_id <- 1:nrow(cont)
#cont <- subset(cont, row_id!=c(636))

# to drop multiple outliers, remove the # at the beginning of the line and use this code:
cont <- subset(cont, row_id!=c(636) & row_id!=c(710))

# use the lm() command to run the regression
# dependent/outcome variable on the left, independent/predictor variables on the right
reg_model <- lm(big5_neu ~ mfq_26 + mfq_state + pas_covid, data = cont)

7 Check Your Assumptions

7.1 Multiple Linear Regression Assumptions

  • Observations should be independent
  • Number of cases should be adequate (N ≥ 80 + 8m, where m is the number of IVs)
  • Independent variables should not be too correlated (aka multicollinearity)
  • 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

7.2 Count Number of Cases

For your homework, if you don’t have the required number of cases you’ll need to drop one of your independent variables. Reach out to me and we can figure out the best way to proceed!

needed <- 80 + 8*3
nrow(cont) >= needed
## [1] TRUE

7.3 Check multicollinearity

  • Higher values indicate more multicollinearity
  • Cutoff is usually 5

For your homework, you will need to discuss multicollinearity and any high values, but you don’t have to drop any variables.

vif(reg_model)
##    mfq_26 mfq_state pas_covid 
##  2.540368  2.616749  1.068753

7.4 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.

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)

7.5 Check for outliers using Cook’s distance and a Residuals vs Leverage plot

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. 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. Points 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.

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)

7.6 Check homogeneity of variance in a Scale-Location plot

This plot is similar to the one’s we’ve seen, but it shows us the standardized residuals across the range of the regression line. Because the residuals are standarized, large residuals (whether positive or negative) are at the top of the plot, while small residuals (whether positive or negative) are at the bottom of the plot. If the assumption of homogeneity of variance (also called homoscedasticity) is met, the red line should be mostly horizontal. If it deviates from the mean line, that means that the variance is smaller or larger at that point of the regression line.

Once again, you can check out this page for some other examples of this type of plot. (Notice that the Scale-Location plot is the third in the grids.) Our Scale-Location plot has some issues! We also still have our influential points indicated; this time, they’re in the top right corner, indicating that they have large residuals (are vary from the regression line).

For your homework, you’ll simply need to generate this plot and talk about how your plot compares to the ones pictured. Is it closer to the ‘good’ plots or one of the ‘bad’ plots? Again, this is a judgement call! It’s okay if feel uncertain, and you won’t be penalized for that.

plot(reg_model, 3)

7.7 Check normality of residuals with a Q-Q plot

This plot is a bit new. It’s called a Q-Q plot and shows the standardized residuals plotted against a normal distribution. If our variables are perfectly normal, the points will fit on the dashed line perfectly. This page shows how different types of non-normality appear on a Q-Q plot.

It’s normal for Q-Q plots show a bit of deviation at the ends, and ours shows some deviation in the top right corner. It doesn’t perfectly fit any of the provided images, but it suggests some negative/right skew, which fits what we saw with the describe() command earlier.

This page shows some examples that help us put our Q-Q plot into context. Although it isn’t perfect, we don’t have any serious issues and are okay to proceed. For your homework, you’ll simply need to generate this plot and talk about how your plot compares to the ones pictured. Does it seem like any skew or kurtosis is indicated by your plot? Is it closer to the ‘good’/‘bad’ plots from the second link?

plot(reg_model, 2)

7.8 Issues with My Data

Before interpreting our results, we assessed our variables to see if they met the assumptions for a multiple linear regression. We analyzed a Scale-Location plot and detected some issues with homogeneity of variance, as well as some issues with linearity in a Residuals vs Fitted plot. However, we did not detect any outliers (visually analyzing a Residuals vs Leverage plot) or any serious issues with the normality of our residuals (visually analyzing a Q-Q plot).

8 View Test Output

summary(reg_model)
## 
## Call:
## lm(formula = big5_neu ~ mfq_26 + mfq_state + pas_covid, data = cont)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.0933 -0.8532  0.1125  0.8244  3.5587 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.36117    0.03421 127.465  < 2e-16 ***
## mfq_26      -0.42767    0.05481  -7.803 1.31e-14 ***
## mfq_state   -0.45014    0.05555  -8.103 1.30e-15 ***
## pas_covid    0.30861    0.03536   8.728  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.188 on 1201 degrees of freedom
## Multiple R-squared:  0.3889, Adjusted R-squared:  0.3874 
## F-statistic: 254.8 on 3 and 1201 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

9 Write Up Results

To test our hypothesis that mental flexibility as a state (measured by the mfq_state), mental flexibility as a trait (measured by the mfq_26), and neuroticism (measured by big5_neu) will significantly predict the pandemic anxiety scale (measured by the pas_covid), we used a multiple linear regression to model the relationship between the variables. We confirmed that our data met the assumptions of a linear regression, and although there were some issues with homogeneity of variance and linearity we continued with the analysis anyway.

Our model was statistically significant, Adj. R2 = .39, F(1201) = 254.8, p < .002. The relationship between neuroticism and the pandemic anxiety scale and mental flexibility as a state and trait was positive and has a large effect size (per Cohen, 1988), while the relationships between our mental flexibility as a trait and state and our outcome (neuroticism) were negative and had effect sizes that were small or trivial. Full output from the regression model is reported in Table 1.

Table 1: Regression model of neuroticism
  Neuroticism (big5_neu)
Predictors Estimates SE CI p
Intercept 4.36 0.03 4.29 – 4.43 <0.001
Mental Flexibility as a Trait (mfq_26) -0.43 0.05 -0.54 – -0.32 <0.001
Mental Flexibility as a State (mfq_state) -0.45 0.06 -0.56 – -0.34 <0.001
Pandemic Anxiety Scale (pas_covid) 0.31 0.04 0.24 – 0.38 <0.001
Observations 1205
R2 / R2 adjusted 0.389 / 0.387


 

References

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