15 Workshop 18: Multiple Regression with Binary and Interactive Variables
15.1 Packages, Prepare, and Import Data
# Set the working directory -WD- so R knows where the data lives. Do this by going Session > Set working directory > Choose directory
# You can check the working directory...
getwd()
#Before doing anything, need to make sure the right packages are installed and open. We will use all of these......
install.packages(tidyverse)
install.packages(correlation)
install.packages(gridExtra)
install.packages(cocor)
library(tidyverse)
library(correlation)
library(gridExtra)
library(cocor)
# Once you have done this, you should see them ticked in the "Packages" list - you can also manually tick the boxes.
# Now get R to open our dataset
mydata <- read_csv("WS_data_R_optimism.csv")
# To check the data have opened ok, you can view the data...
view(mydata)
# You can also check the number of participants (obs) and the number of variables in the "Environment" tab.
# Next, we need to tell R which variables are continuous (as.numeric) and which are categorical (as.factor).
# You can check the names of the variables with this...
names(mydata)
mydata$p_num <- as.numeric(mydata$p_num)
mydata$optimism <- as.numeric(mydata$optimism)
mydata$positive_SC <- as.numeric(mydata$positive_SC)
mydata$negative_SC <- as.numeric(mydata$negative_SC)
mydata$age_years <- as.numeric(mydata$age_years)
mydata$extra_curr <- as.factor(mydata$extra_curr)
mydata$reading_age <- as.numeric(mydata$reading_age)
# Before getting into correlations, we might want a summary of our variables. For the continuous variables, we get descriptives. For the categorical/binary variables, we get frequencies.
summary(mydata)
# We should run the zero order correlations for all continuous variables, the outcome variable and the predictors.
mydata %>%
dplyr::select(optimism, positive_SC, negative_SC) %>%
correlation(p_adjust = "none")
# In addition to giving you the r and p values, it gives the N, so check that this is correct. To write up the correlation, remember that df = N-2.15.2 Exercise One
# EXERCISE ONE - running the multiple regression
# Now, let's run a multiple regression, but this time with continuous, binary and interactive predictors
# We have SWL as the outcome variable that we want to predict
# Then two continuous predictors (positive and negative SC), one binary (extra curricular) and two interactions (each SC measure interacting with extra curricular).
model <- lm(optimism ~ positive_SC + negative_SC + extra_curr + positive_SC*extra_curr + negative_SC*extra_curr, data = mydata)
summary(model)15.3 Exercise Two
# EXERCISE TWO - interpreting the overall model
# No additional bits to run for this, just pull out the relevant stats from the analysis you just ran.15.4 Exercise Three
# EXERCISE THREE - interpreting the continuous predictors and graph any significant predictors with a scatterplot
# First, from the analysis already run, pull out the B, t and p for each continuous predictor and interpret significant findings.
# Then create a scatterplot for any significant continuous predictors.
plot1 <- ggplot(mydata, aes(x = negative_SC, y = optimism)) +
geom_point() +
geom_smooth(method = "lm",
se = FALSE) +
theme_classic()
print(plot1)15.5 Exercise Four
# EXERCISE FOUR - interpreting the binary predictor, and if significant, create a graph and descriptives to aid interpretation
# First, from the analysis already run, pull out the B, t and p for each continuous predictor and interpret significant findings.
# Now, calculate the descriptive statistics (mean and SD) for each of the groups.
descriptives_bygroup <- mydata %>% # Tell R which data set to use. %>% means "and then" so tells R to move on and do something else
group_by(extra_curr ) %>% # group_by is telling R to split the data file - put the variable to split by in brackets
summarise(mean_optimism = mean(optimism), sd_optimism = sd(optimism)) # Ask for the mean and standard deviation. statistic_calculated = statistic(variable)
# You then need R to "print" - or display - the calculated descriptives in the console window.
print(descriptives_bygroup)
# Next, we want to create a boxplot for each significant binary predictor. We do this in exactly the same way as for graphing an independent t test, so you can go back to that lecture/workshop if needed.
ggplot(mydata, aes(x = extra_curr, y = optimism)) +
geom_boxplot() +
labs(title = "Optimism by doing extracurricular activities",
x = "Extracurricular activitiies",
y = "Mean optimism score") +
theme_classic()15.6 Exercise Five
# EXERCISE FIVE - interpreting the interactive predictors
# First, from the analysis already run, pull out the B, t and p for each interactive predictor and interpret significant findings.
# For a significant interactive predictor, follow the four step process to statistically compare correlations.
# STEP 1: We need to tell R which subgroups within our dataset we want to look at - so identify 0 and 1 from the "extra curricular" variable, and name each one.
None <- mydata[mydata$extra_curr == "0", ]
Activities <- mydata[mydata$extra_curr == "1", ]
# STEP 2: Now we run the two correlations - we need to tell R first which subgroup to use from the naming we just did, and which continuous variable to correlate.
cor.test(None$optimism, None$positive_SC,
method = "pearson")
cor.test(Activities$optimism, Activities$positive_SC,
method = "pearson")
# STEP 3: To compare the correlations statistically, we need the N and the r for each group. The r value is the final value in the output we just created - the final line, under corr.
# To get the N for each group, we ran the "summary" earlier, but you can do it again to save scrolling.
summary(mydata)
# Now we can statistically compare our r values. Make a note of which group you consider to be "1" and which is "2". For this, it will be 1 is single and 2 is in a relationship.
# First, make sure "cocor" is ticked in the "Packages" tab.
# r1 is the first r-value, in this case 0.2009678
# r2 is the second r-value, in this case 0.6345603
# n1 is the first sample size, in this case 73
# n2 is the second sample size, in this case 77
# the code looks like this, so just replace the values as needed... cocor.indep.groups(r1, r2, n1, n2)
cocor.indep.groups(0.2009678, 0.6345603, 73, 77)
# STEP 4: graph these two correlations on the same plot to aid interpretation.
plot_cc <- ggplot(mydata, aes(x = positive_SC, y = optimism, colour = extra_curr)) +
geom_point(aes(shape = extra_curr)) +
geom_smooth(aes(linetype = extra_curr), method = "lm", se = FALSE) +
labs(title = "Positive self compassion vs Optimism by Extra curricular activities",
x = "Positive self compassion",
y = "Optimism") +
theme_classic() +
scale_color_manual(values = c("0" = "grey", "1" = "black ")) +
scale_linetype_manual(values = c("0" = "solid", "1" = "dashed")) +
scale_shape_manual(values = c("0" = 16, "1" = 3))
print(plot_cc)
# WORKSHOP FINISHED - YAY!!!