pkg_list <- c("tidyverse", "psych", "rcompanion", "knitr", "car", "afex", "ez",
              "ggfortify", "Hmisc", "emmeans", "jtools", "apaTables", "dplyr")
# purrr::walk(pkg_list, require, quietly = TRUE, character.only = TRUE)
pacman::p_load(pkg_list, character.only = TRUE)

Hands on practice: data analyses

For this hands on practice, load the spi data set from the psych package.

  1. Check the help file, structure, and first few observations of the data
data("spi", package = "psych")
# Write your code here!
  1. Change the integer variables to new categorical/factor variables by cross-referencing the help page and filling in the pipeline below (note when knitting this document, you may want to change eval to TRUE once you complete this exercise). Use either ifelse(), case_when(), or factor() then make sure that your code appropriately changed each variable with str() or class()
spi <- spi %>%
  mutate(.,
    sex_f = factor(),
    health_f = factor(),
    education_f = factor(),
    smoke_f = factor(),
    exer_f = factor()
  )

# Write your code here!
str(spi)

Descriptives and Chi-squared

  1. Use either describe() or skim() to describe the data by each categorical variable
# Write your code here!
  1. Describe the data by both categorical variables at once
# Write your code here!
  1. Perform a chi-squared test assessing whether there is a differences in smoking by education level
# Write your code here!

Correlation

  1. Get the covariance and correlation matrix for the relationship between three numeric values of your choosing
# Write your code here!
  1. Perform a test of significance on two of the variables selected above
# Write your code here!

Linear Models

Note: For the following questions, make sure to not use any of categorical variables you created above (lm() requires integer variables for the y variable)

  1. Let’s look at the association between education and health. In other words, does education predict health?
# Write code here
  1. Now lets see if there may be an interaction between two variables that predicts health. Let’s see if education and smoking interact to predict health
# Write code here
  1. Extending the lesson now lets add a control variable. Let’s see if the interaction between wellness and education predicts helath contorling for age.
# Write code here
  1. Now lets get the results into a tidy table 4a. First a table of all parameters
# Write code here

4b. Now a table of the golbal parameters

# Write code here

ANOVAs

  1. Let’s look at the association between exercise and health with ANOVA. In other words, does exercise predict health?
# Write code here
  1. Now let’s add a variable that predicts exercise. Let’s see if education and exercise interact to predict health. Are these two variables within-group or between-group factors?
# Write code here
  1. Let’s pretend (for practice) that p1edu, p2edu and education are the education levels at three time points. Let’s see if education and exercise interact to predict health. Are these two variables within-group or between-group factors?
spi %>% 
  gather(key="edu_period", value= "edu", p1edu, p2edu,education)  %>%
  select("edu","edu_period","health", "exer") -> yourdataframe

# Write code here
  1. Choose the proper analysis, which is not limited to ANOVA. 4a. How do sex and age influence exercise?
# Write code here

4b. How do smoke and exercise affect wellness?

# Write code here