R-Studio work
# ~ PSYC 300B Final Project -- Additional Planned and Post-Hoc Tests ~ # # === Getting Started === # # SET YOUR WORKING DIRECTORY # The easiest way to set your working directory is to use the Session Menu in RStudio # and click on the "Set Working Directory" option, and then "Choose Directory..." # This will bring up a window that allows you to see your directory structure. # Alternatively, you can set it directly by using one of these commands depending # on the type of computer you have -- obviously, you will need to use your details # Simply uncomment the line that you need # For windows computers setwd("C:/Users/Name/Documents/PSYC 300B/R") # For mac computers setwd("~/Documents/PSYC 300B/R") # If you need to determine what your working director is, simply use this command getwd() # LOAD DATA dataset = read.csv("PSYC300B_FinalProject_DataSet.csv") # INSTALLING PACKAGES AND LOAD LIBRARIES # Install the packages if necessary if(length(find.package('car', quiet=TRUE)) == 0) {install.packages('car')} if(length(find.package('psych', quiet=TRUE)) == 0) {install.packages('psych')} # Loading packages # Note: You will need to load the packages you plan to use every time you start a new R session. library(psych) #need this package to use the describe function library(car) #need this package to use the leveneTest function dataset$Dates = as.factor(dataset$Dates) # === Levene's Test of Homogeneity of Variance === # # We need to let the program know which of our variables is the IV (our factor). Consequently, we define the IV using the function "as.factor". # Turn Social_Media_Preference into a Factor #Conduct Levene's Test Levene_results = leveneTest(Wellbeing ~ Dates, data = dataset, center = mean) print(Levene_results) ### NOTE: For this example, we haven't actually violated the assumption, I am just providing the code # Brown-Forsythe Test # This is the best option when you have violated the assumption of homogeneity of variance and you have a balanced design. # For this, you will need to install the package oneway. # Install package if(length(find.package('onewaytests', quiet=TRUE)) == 0) {install.packages('onewaytests')} # Load package library(onewaytests) #need this package to use the Brown-Forsythe Test # Run the Brown-Forsythe test BF_results = bf.test(Wellbeing ~ Dates, data = dataset) # When we violate the assumption of homogeneity of variance, we cannot run Dunn's, so we do a series of Welch's t-tests instead # you will have to determine your p(alpha)_pc. # Determine the conditions that you want to compare... to do this, we simply index the proper rows test1 <- dataset[dataset$Dates %in% c('1','2'),] # this would pull out the rows for 1 and 2 date conditions test2 <- dataset[dataset$Dates %in% c('3','5'),] # this would pull out the rows for 3 and 5 date conditions t.test(Wellbeing ~ Dates, data = test1, var.equal = FALSE) t.test(Wellbeing ~ Dates, data = test2, var.equal = FALSE) # For the post-hoc tests, we are limited to running Dunnett's test (assuming we have a control group) # Of course, we can always run Dunnett's test, regardless of the results of Levene's (assuming the main ANOVA is significant) #Install package called DescTools if(length(find.package('DescTools', quiet=TRUE)) == 0) {install.packages('DescTools')} # Load package library(DescTools) #need this package to use the DunnettTest function. #The Dunnetts Test will assume that the control is your first group, but you can also # explicitly tell it is a different group by using control = 'group' where group is one of your levels of your IV. DunnettTest(Wellbeing ~ Dates, data = dataset, control ='1')