# 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(car)
library(tidyverse)
library(correlation)
library(gridExtra)
library(car)
# Once you have done this, you should see them ticked in the "Packages" list - you can also manually tick the boxes.
# If you need to install any of these, the code is install.packages("name of package")
# Make sure you can then see it in the "Packages" list, and call it using library("name of package") or just tick it in the "Packages" list
# 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.