LOADING DATA…

setwd("~/Dropbox/PROGRAMMING/R/WeightsExp/Pilot/data")

subjects = list.files(pattern="*.csv")
for (i in 1:length(subjects)){
  assign(paste0("s", i), read.csv(subjects[i]))
} 

setwd("~/Dropbox/PROGRAMMING/R/WeightsExp/Pilot")

#Initialize data frame to hold all subjects
total_NM <- NULL
total_M <- NULL
Subject <- NULL
###########################
#   CLEAN DATA FRAMES 
#   (break into Multiplier and non-Multiplier)
###########################

#loop through and create data frames
for (i in 1:length(subjects)) {

  temp <- eval(as.name(paste0("s", i)))
  
  #remove extraneous rows
  temp <- temp[-c(1:3), -c(1:21)]

  #create non-multiplier data frame
  temp_NM <- temp[c(1:100), c(1:10) ]
  
  #create multiplier data frame
  temp_M <- temp[c(102:401), c(1:15)]
  temp_M <- temp_M[,-c(9:12)]
  
  #renameSomeColumns
  library(plyr)
  temp_NM <- rename(temp_NM, c("numberEntry_3.keys"="acceptReject", "numberEntry_3.rt"="decisionTime"))
  temp_M <- rename(temp_M, c("numberEntry_4.keys"="acceptReject", "numberEntry_4.rt"="decisionTime"))
  
  #clean up acceptReject column
  index <- c("['f']", "['j']")
  values <- c(1, 0) #where 1 is accept and 0 is reject
  temp_NM$acceptReject <- values[match(temp_NM$acceptReject, index)]
  temp_M$acceptReject <- values[match(temp_M$acceptReject, index)]
  
  #clean up Flip column
  index <- c("True", 1, 2)
  values <- c(1, 0, 0) #where 1 is accept and 0 is reject
  temp_M$flip <- values[match(temp_M$flip, index)]
  
  #make summed value a value to 2 decimal places
  temp_NM$summedVal <- format(round(temp_NM$summedVal, 2), nsmall = 2)
  temp_M$summedVal <- format(round(temp_M$summedVal, 2), nsmall = 2)
  
  #make numeric
  temp_NM$summedVal <- as.numeric(temp_NM$summedVal)
  temp_M$summedVal <- as.numeric(temp_M$summedVal)
  
  #remove brackets on RT column
  temp_NM$decisionTime <- gsub("\\[|\\]", "", temp_NM$decisionTime)
  temp_M$decisionTime <- gsub("\\[|\\]", "", temp_M$decisionTime)
  #make Numeric
  temp_NM$decisionTime <- as.numeric(temp_NM$decisionTime)
  temp_M$decisionTime <- as.numeric(temp_M$decisionTime)

  #########
  # Save individual data frames as well (maybe a new loop after)
  
  total_NM <- rbind(total_NM, temp_NM)
  total_M <- rbind(total_M, temp_M)
}

#Make row numbering sequential
rownames(total_NM) <- 1:nrow(total_NM)
rownames(total_M) <- 1:nrow(total_M)

###CHECK THIS OUT###
#Replace NAs in flip column with 0s.
total_M[is.na(total_M)] <- 0

head(total_M)
##   Trial correct faceVal houseVal mult1House mult2Face summedVal earnings
## 1     1       1    0.44    -0.78          1         1     -0.34    32.88
## 2     2       0   -0.28     0.16          3         1      0.20    32.68
## 3     3       1   -0.10    -0.80          3         1     -2.50    32.68
## 4     4       1    0.84    -0.46          2         1     -0.08    32.68
## 5     5       1    0.82     0.40          1         1      1.22    33.90
## 6     6       1    0.38     0.20          1         1      0.58    34.48
##   flip acceptReject decisionTime
## 1    0            0     8.916853
## 2    0            0     2.466852
## 3    0            0     4.816845
## 4    0            0     7.666896
## 5    0            1     2.633707
## 6    0            1     2.466849

PRELIMINARY ANALYSIS

###########################
#   ANALYSIS
###########################

#GROUP
#Percent Correct
mean(total_NM$correct)
## [1] 0.8685714
mean(total_M$correct)
## [1] 0.8857143
#Mean Response Time
mean(total_NM$decisionTime)
## [1] 4.127875
mean(total_M$decisionTime)
## [1] 2.934391
#clean outliers due to distraction
total_NM_clean<- total_NM[!(total_NM$decisionTime>10),]
total_M_clean<- total_M[!(total_M$decisionTime>10),]

#Cleaned Mean Response Time
mean(total_NM_clean$decisionTime)
## [1] 3.073678
mean(total_M_clean$decisionTime)
## [1] 2.466951
#SD Response Time
#Cleaned Mean Response Time
sd(total_NM_clean$decisionTime)
## [1] 2.148276
sd(total_M_clean$decisionTime)
## [1] 1.700813
#Mean response time with multipliers
mean(total_M_clean$decisionTime[total_M_clean$mult1House>1])
## [1] 2.569192
mean(total_M_clean$decisionTime[total_M_clean$mult2Face>1])
## [1] 2.455839
mean(total_M_clean$decisionTime[total_M_clean$mult1House>1])
## [1] 2.569192
mean(total_M_clean$decisionTime[total_M_clean$mult2Face>1])
## [1] 2.455839
#What about RT WITH TWO multipliers
mean(total_M_clean$decisionTime[(total_M_clean$mult2Face>1) & (total_M_clean$mult1House>1)])
## [1] 2.809233
#vs WITHOUT
mean(total_M_clean$decisionTime[(total_M_clean$mult2Face<2) & (total_M_clean$mult1House<2)])
## [1] 2.462117
#with only FACE multiplier
mean(total_M_clean$decisionTime[(total_M_clean$mult2Face>1) & (total_M_clean$mult1House==1)])
## [1] 2.351807
#with only HOUSE multiplier
mean(total_M_clean$decisionTime[(total_M_clean$mult2Face==1) & (total_M_clean$mult1House>1)])
## [1] 2.494322

PLOT STUFF

#RT
hist(total_M_clean$decisionTime, breaks = 50)
abline(v = mean(total_M_clean$decisionTime),
       col = "royalblue",
       lwd = 2)

#median line
abline(v = median(total_M_clean$decisionTime),
       col = "red",
       lwd = 2)

#LEGEND
legend(x = "topright",
       c(as.expression(bquote(Mean == .(mean(total_M_clean$decisionTime)))), as.expression(bquote(Median == .(median(total_M_clean$decisionTime))))),
       col = c("royalblue", "red"),
       lwd = c(2, 2, 2))

# 3D Scatterplot with Coloring and Vertical Lines
# and Regression Plane 
#RT vs. HVal, FVal
library(scatterplot3d) 
s3d <-scatterplot3d(total_M_clean$decisionTime,total_M_clean$faceVal,total_M_clean$houseVal, pch=16, highlight.3d=TRUE,
                    type="h", main="3D Scatterplot")
fit <- lm(total_M_clean$decisionTime ~ total_M_clean$faceVal+total_M_clean$houseVal) 
s3d$plane3d(fit)

#RT vs. Summed Value
library(ggplot2)

ggplot(total_M_clean, aes(x=summedVal, y=decisionTime)) +
  geom_point(shape=1) +    # Use hollow circles
  geom_smooth()            # Add a loess smoothed fit curve with confidence region
## `geom_smooth()` using method = 'gam'

#Effect of Flips?
library(ggplot2)
p <- ggplot(total_M_clean, aes(factor(flip), decisionTime))
p + geom_boxplot()

Summed Value vs. Accept/Reject Decision

Logistic regression curve

plot(total_M_clean$summedVal, total_M_clean$acceptReject)

model <- glm(acceptReject ~ summedVal, data=total_M_clean, family=binomial(link = logit))
summary(model)
## 
## Call:
## glm(formula = acceptReject ~ summedVal, family = binomial(link = logit), 
##     data = total_M_clean)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.4096  -0.3046   0.0619   0.4139   3.6912  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  0.49218    0.07588   6.486 8.82e-11 ***
## summedVal    3.20337    0.14653  21.861  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 2777.6  on 2049  degrees of freedom
## Residual deviance: 1144.5  on 2048  degrees of freedom
## AIC: 1148.5
## 
## Number of Fisher Scoring iterations: 7
xv <- seq(min(total_M_clean$summedVal), max(total_M_clean$summedVal), 0.01)
yv <- predict(model,list(summedVal=xv), type="response")

lines(xv,yv)

#Find the inflection point where there is a 50/50 probability of subject accepting.
p <- 0.5
x <- (log(p/(1-p)) - coef(model)[1]) / coef(model)[2]
x
## (Intercept) 
##   -0.153645


GLM STUFF

t <- lm(correct ~ faceVal * houseVal, data=total_M_clean)
anova(t)
## Analysis of Variance Table
## 
## Response: correct
##                    Df  Sum Sq Mean Sq F value  Pr(>F)    
## faceVal             1   0.259  0.2594  2.7280 0.09876 .  
## houseVal            1   0.189  0.1892  1.9893 0.15857    
## faceVal:houseVal    1   7.639  7.6386 80.3298 < 2e-16 ***
## Residuals        2046 194.555  0.0951                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
t <- lm(acceptReject ~ faceVal * houseVal, data=total_M_clean)
anova(t)
## Analysis of Variance Table
## 
## Response: acceptReject
##                    Df  Sum Sq Mean Sq F value    Pr(>F)    
## faceVal             1 140.810 140.810 1148.86 < 2.2e-16 ***
## houseVal            1  98.304  98.304  802.06 < 2.2e-16 ***
## faceVal:houseVal    1   6.638   6.638   54.16 2.663e-13 ***
## Residuals        2046 250.767   0.123                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
fit <- lm(correct ~ faceVal + houseVal, data=total_M)
summary(fit) 
## 
## Call:
## lm(formula = correct ~ faceVal + houseVal, data = total_M)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.91138  0.09702  0.11062  0.12396  0.14697 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.885193   0.006947 127.413   <2e-16 ***
## faceVal     0.020598   0.011878   1.734   0.0831 .  
## houseVal    0.013209   0.011729   1.126   0.2602    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3181 on 2097 degrees of freedom
## Multiple R-squared:  0.002039,   Adjusted R-squared:  0.001087 
## F-statistic: 2.142 on 2 and 2097 DF,  p-value: 0.1177
fit <- lm(acceptReject ~ faceVal + houseVal, data=total_M)
summary(fit) 
## 
## Call:
## lm(formula = acceptReject ~ faceVal + houseVal, data = total_M)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.21035 -0.27325 -0.00131  0.28556  1.08516 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.574028   0.007809   73.51   <2e-16 ***
## faceVal     0.444957   0.013351   33.33   <2e-16 ***
## houseVal    0.367391   0.013184   27.87   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3575 on 2097 degrees of freedom
## Multiple R-squared:  0.4742, Adjusted R-squared:  0.4737 
## F-statistic: 945.7 on 2 and 2097 DF,  p-value: < 2.2e-16


Linear model if there is a multiplier on FACES

fit <- lm(acceptReject ~ faceVal + houseVal, data=subset(total_M_clean,mult2Face>1 & mult1House==1))
summary(fit) 
## 
## Call:
## lm(formula = acceptReject ~ faceVal + houseVal, data = subset(total_M_clean, 
##     mult2Face > 1 & mult1House == 1))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.10366 -0.17233 -0.00968  0.20768  0.80228 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.58080    0.01426  40.716   <2e-16 ***
## faceVal      0.66388    0.02464  26.942   <2e-16 ***
## houseVal     0.05225    0.02317   2.255   0.0246 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2916 on 442 degrees of freedom
## Multiple R-squared:  0.6221, Adjusted R-squared:  0.6204 
## F-statistic: 363.8 on 2 and 442 DF,  p-value: < 2.2e-16


Linear model if there is a multiplier on HOUSES

fit <- lm(acceptReject ~ faceVal + houseVal, data=subset(total_M_clean,mult1House>1 & mult2Face ==1))
summary(fit)
## 
## Call:
## lm(formula = acceptReject ~ faceVal + houseVal, data = subset(total_M_clean, 
##     mult1House > 1 & mult2Face == 1))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.91038 -0.20640  0.02105  0.20452  1.04226 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.52463    0.01626  32.263  < 2e-16 ***
## faceVal      0.13668    0.02663   5.132 4.41e-07 ***
## houseVal     0.64875    0.02806  23.124  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3264 on 417 degrees of freedom
## Multiple R-squared:  0.5687, Adjusted R-squared:  0.5666 
## F-statistic: 274.9 on 2 and 417 DF,  p-value: < 2.2e-16

T-test on flips trials…no effect…

#T-test compare "flips" to non flips
t.test((total_M_clean$decisionTime[total_M_clean$flip == 1]),(total_M_clean$decisionTime[total_M_clean$flip == 0]))
## 
##  Welch Two Sample t-test
## 
## data:  (total_M_clean$decisionTime[total_M_clean$flip == 1]) and (total_M_clean$decisionTime[total_M_clean$flip == 0])
## t = -1.0262, df = 71.803, p-value = 0.3082
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.7010674  0.2245862
## sample estimates:
## mean of x mean of y 
##   2.23673   2.47497



#T-test to see if if differnce in RT for mult vs. non mult significant?

#T-Test that checks if the means of decision time with NO multipliers and at least ONE multiplier varies
t.test((total_M_clean$decisionTime[total_M_clean$mult2Face == 1 & total_M_clean$mult1House == 1]),(total_M_clean$decisionTime[total_M_clean$mult2Face > 1 | total_M_clean$mult1House >1]))
## 
##  Welch Two Sample t-test
## 
## data:  (total_M_clean$decisionTime[total_M_clean$mult2Face == 1 & total_M_clean$mult1House ==  and (total_M_clean$decisionTime[total_M_clean$mult2Face > 1 | total_M_clean$mult1House >     1]) and     1])
## t = -0.13221, df = 2029.7, p-value = 0.8948
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.1575432  0.1376436
## sample estimates:
## mean of x mean of y 
##  2.462117  2.472067
#T-Test that checks if the means of decision time with NO multipliers and TWO multiplier varies
t.test((total_M_clean$decisionTime[total_M_clean$mult2Face == 1 & total_M_clean$mult1House == 1]),(total_M_clean$decisionTime[total_M_clean$mult2Face > 1 & total_M_clean$mult1House >1]))
## 
##  Welch Two Sample t-test
## 
## data:  (total_M_clean$decisionTime[total_M_clean$mult2Face == 1 & total_M_clean$mult1House ==  and (total_M_clean$decisionTime[total_M_clean$mult2Face > 1 & total_M_clean$mult1House >     1]) and     1])
## t = -2.1611, df = 161.12, p-value = 0.03216
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.66430767 -0.02992467
## sample estimates:
## mean of x mean of y 
##  2.462117  2.809233

The standard package of plots

fit <- lm(acceptReject ~ faceVal + houseVal, data=total_M_clean)
summary(fit) 
## 
## Call:
## lm(formula = acceptReject ~ faceVal + houseVal, data = total_M_clean)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.08561 -0.27063 -0.00188  0.28462  1.08184 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.577366   0.007837   73.67   <2e-16 ***
## faceVal     0.443128   0.013375   33.13   <2e-16 ***
## houseVal    0.369897   0.013230   27.96   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3546 on 2047 degrees of freedom
## Multiple R-squared:  0.4816, Adjusted R-squared:  0.4811 
## F-statistic: 950.8 on 2 and 2047 DF,  p-value: < 2.2e-16
plot(fit)