library(xlsx)
## Loading required package: rJava
## Loading required package: xlsxjars
library(Hmisc)
## Loading required package: grid
## Loading required package: lattice
## Loading required package: survival
## Loading required package: splines
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## 
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units
library(lattice)

setwd("~/Dropbox/wrk/mindfulness/Pilot Study/imp-cat-01/")
stim <- read.xlsx("imp-cat-01-stim.xlsx", sheetIndex = 1)
stim <- subset(stim, cat1_orient==55)
plot(cat1_orient ~ cat1_freq, stim, xlim=c(1,3), ylim=c(20, 65), type="p",
     xlab="Frequency", ylab="Orientation")
text(cat2_orient ~ cat2_freq, stim, paste("c", 1:5, sep=""))
text(probe_orient ~ probe_freq, stim, paste("p", 1:5, sep=""))

Get the Data

df <- dir("data", pattern=".csv$", full.names=TRUE)  # Get the file names

sd <- sapply(df, read.csv) # Read in all the .csv files
sd  <- sd[sapply(sd, nrow) > 20] # Keep the ones with more than 20 rows (ours have 21)
sd <- lapply(sd, function(x) x[,1:19])

sd.df <- do.call("rbind", sd)
sd.df <- subset(sd.df, !is.na(trials.thisRepN)) # remove the instruction responses
table(sd.df$participant)
## 
##   0   1   2  10  11  12  15 120 121 122 123 128 
##  20 109  80  20  20  20  20  20  20  20  20  20

Compute distances

## Compute all dists from probe
sd.df$cat1_probe_freq_dist  <- abs(sd.df$cat1_freq - sd.df$probe_freq)
sd.df$cat1_probe_orient_dist  <- abs(sd.df$cat1_orient - sd.df$probe_orient)
sd.df$cat2_probe_freq_dist  <- abs(sd.df$cat2_freq - sd.df$probe_freq)
sd.df$cat2_probe_orient_dist  <- abs(sd.df$cat2_orient - sd.df$probe_orient)

sd.df$freq_dist <- ifelse(sd.df$cat1_probe_freq_dist > 0, sd.df$cat1_probe_freq_dist, sd.df$cat2_probe_freq_dist)
sd.df$orient_dist <- ifelse(sd.df$cat1_probe_orient_dist > 0, sd.df$cat1_probe_orient_dist, sd.df$cat2_probe_orient_dist)

sd.df$freq.orient.ratio  <- sd.df$freq_dist / sd.df$orient_dist
sd.df$orent.freq.ratio  <- 1/sd.df$freq.orient.ratio
sd.df$lo.of.ratio <- round(log(sd.df$orent.freq.ratio), 4)

sd.df$orient.resp <- ifelse(sd.df$cat1_probe_orient_dist > 0, sd.df$resp.keys == 'a', sd.df$resp.keys == 'l')
summary.formula(orient.resp ~ factor(lo.of.ratio), sd.df, fun=function(x) sum(x)/length(x))
## orient.resp    N=389
## 
## +-------------------+------+---+-----------+
## |                   |      |N  |orient.resp|
## +-------------------+------+---+-----------+
## |factor(lo.of.ratio)|2.3026| 39|0.9230769  |
## |                   |2.9957| 39|0.8461538  |
## |                   |3.4012| 38|0.7631579  |
## |                   |3.6889| 39|0.6666667  |
## |                   |3.912 | 39|0.6666667  |
## |                   |4.0174| 39|0.7692308  |
## |                   |4.2687| 39|0.6666667  |
## |                   |4.6052| 39|0.4358974  |
## |                   |5.116 | 39|0.2564103  |
## |                   |6.2146| 39|0.1282051  |
## +-------------------+------+---+-----------+
## |Overall            |      |389|0.6118252  |
## +-------------------+------+---+-----------+

Subject by subject

sd.opref <- with(sd.df, summarize(orient.resp, by=llist(participant, lo.of.ratio), FUN=function(x) sum(x)/length(x)))
sd.opref$participant <- factor(sd.opref$participant)
sd.opref$lo.of.ratio <- as.numeric(sd.opref$lo.of.ratio)
sd.opref$orient.resp <- as.numeric(sd.opref$orient.resp)

xyplot(orient.resp ~ lo.of.ratio | participant, sd.opref, type="b")

Overall

sd.opref <- with(sd.df, summarize(orient.resp, by=llist(lo.of.ratio), FUN=function(x) sum(x)/length(x)))
sd.opref <- apply(sd.opref, 2, as.numeric)
plot(orient.resp ~ lo.of.ratio, sd.opref, type="b", ylim=c(0,1))