#CHAPTER 4: THE R SOFTWARE IN ACTION ###################################################### #################################################### #################################################### #4.2: “R” INSTALLATION AND SESSOION #################################################### #INSTALL PACKAGES #install.packages("ggm") #install.packages("polycor") #install.packages("Hmisc") #install.packages("psych") #install.packages("ggplot2") #install.packages("rlm") #install.packages("MASS") #install.packages("pastecs") #LIBRARIES TO LOAD library(ggm); library(Hmisc); library(polycor); library(psych) library(ggplot2); library(rlm); library(MASS); library(pastecs) #################################################### #################################################### #4.3: BASICS OF R DEMONSTRATED #################################################### #4.3.2: IMPORT DATA TO THE SESSION #################################################### #READ CSV FILE INTO SESSSION # provided by the user to inform of location of data TaskData <- read.csv("C:\\\\ScoreComplexPrep.csv", header=TRUE, sep=",") #################################################### #4.3.3: KNOW THY DATA ANALYSIS #################################################### #INSPECT FIRST SIX CASES head(TaskData) #INSPECT MAKEUP OF VARIABLES str(TaskData) #DESCRIPTIVE STATISTICS summary(TaskData) #ADDITIONAL DESCRIPTIVE STATISTICS describe(TaskData) #PAIRS PANEL pairs.panels(TaskData[c("Score","Complex","Prep")]) #Q-Q GRAPHIC FOR NORMALITY par(mfrow = c(2, 2)) anx<- qqnorm(TaskData$Complex, main = "Q-Q Complexity "); qqline(TaskData$Complex) rev<- qqnorm(TaskData$Prep, main = "Q-Q Preparation "); qqline(TaskData$Prep) exm<- qqnorm(TaskData$Score, main = "Q-Q Efficiency Score "); qqline(TaskData$Score) par(mfrow = c(1, 1)) #################################################### #4.3.4: CORRELATION ANALYSIS #################################################### #CREATE DATAFRAME FROM THE THREE VARIABLES OF INTEREST TaskData2<- TaskData[,c("Score","Complex","Prep")] head(TaskData2) #CORRELATION WITH PEARSON AND SPEARMAN cor(TaskData2, method="pearson") cor(TaskData2, method="spearman") #CONVERT TASKDATA2 TO A MATRIX TaskMatrix<- as.matrix(TaskData2) #DETERMINE P-VALUES TO CORRELATIONS rcorr(TaskMatrix, type = "pearson") rcorr(TaskMatrix, type = "spearman") #INTERVAL TO CORRELATION WITH PEARSON cor.test(TaskData$Complex, TaskData$Score, method="pearson") cor.test(TaskData$Complex, TaskData$Prep, method="pearson") cor.test(TaskData$Prep, TaskData$Score, method="pearson") #################################################### #4.3.5: PARTIAL CORRELATION ANALYSIS #################################################### #R SQUARED cor(TaskData2, method="pearson")^2 cor(TaskData2, method="spearman")^2 #PARTIAL CORRELATION TO SCORE AND COMPLEX cor(TaskData2, method="pearson") pcExAn<- pcor(c("Score", "Complex", "Prep"), var(TaskData2)) pcExAn pcExAn^2 pcor.test(pcExAn, 1, 103) #ALTERNATIVE PARTIAL SCORE TO COMPLEX pcExRe<- pcor(c("Exam", "Revise", "Anxiety"), var(examData2)) pcExRe pcExRe^2 pcor.test(pcExRe, 1, 103) #################################################### #4.3.6: REGRESSION ANALYSIS #################################################### ##Conduct linear model to test for interaction between predictor variables #MODEL OF MAIN EFFECTS TaskPred <- lm(Score ~ Complex + Prep, TaskData) summary(TaskPred) confint(TaskPred) #MODEL WITH INTERACTION TaskPredIntr <- lm(Score ~ Complex * Prep, TaskData) summary(TaskPredIntr) confint(TaskPredIntr) #ROBUST MODEL FOR NONNORMAL DATA rlm.Tasklm <- rlm(Score ~ Complex + Prep, data = TaskData) summary(rlm.Tasklm)