%\documentclass[a4paper]{article}
\documentclass{article}

%\usepackage{fancyhdr}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{ulem}
\usepackage{comment}
\usepackage{apacite}
\usepackage{makeidx}
\usepackage{hyperref}
\usepackage{michigan-Sweave}
\usepackage{mathpazo}
\usepackage{listing}
\usepackage{comment,apacite,hyperref}

\hypersetup{ps2pdf=TRUE,colorlinks=TRUE,linkcolor=blue,anchorcolor=blue,
  filecolor=blue,pagecolor=blue,urlcolor=blue,citecolor=blue,bookmarksnumbered=true}
%!TEX encoding =  
%% Following makes the margins more narrow than standard article class.
\addtolength{\hoffset}{-0.5in} \addtolength{\textwidth}{1in}
\addtolength{\marginparwidth}{0.25in} \addtolength{\voffset}{-0.5in}
\addtolength{\textheight}{1in}
\addtolength{\parskip}{1em}
\setlength{\parindent}{0em}

\pagestyle{headings}

% Location of figures generated while compiling
% (Note that you have to make this directory first, so adjust this
% line as necessary, or comment out if you don't mind having the
% figures in the same directory as the .Rnw etc. files.
% The string after the / gives a prefix to every figure generated


 %% cache everything 



\setcounter{tocdepth}{3}    % sets how much you want to show in TOC

%\special{!userdict begin /bop-hook{gsave 170 90 translate
%65 rotate /Times-Roman findfont 108 scalefont setfont
%0 0 moveto 0.85 setgray (DRAFT 25.05.22) show grestore}def end}

%\renewcommand\familydefault{\sfdefault}%    use sans serif by default


\title{Analyses and plots for Bartek, Lewis, Vasishth and Smith, 2009}

% \author{Analysis by Shravan Vasishth \\
% University of Potsdam Germany \\
% vasishth@acm.org}

\date{Version dated \today}

% \oddsidemargin -5mm
% \evensidemargin -5mm
% \textwidth 150.2mm
% \textheight 215.96mm
\excludecomment{commentout}


\begin{document}
\setkeys{Gin}{width=.95\textwidth}

%\input{frontmatterheaders}
%\frontmatter

\maketitle

\tableofcontents

\newpage



\section{Description, paradigm details}
This is a replication of Grodner \& Gibson 2005 Experiment 2 using SPR, 
with new materials in which all words are 4-6 letters, and have a frequency
>50-per-million in the American National Corpus First Release. 

Run Fall 2006 and Winter 2007.


\section{Required libraries}

\begin{Schunk}
\begin{Sinput}
> library(reshape)
> library(xtable)
> library(lattice)
> library(Hmisc)
> library(lme4)
> source("shravan-rprofile.r")
> options(device = "pdf", prompt = " ", continue = " ")
\end{Sinput}
\end{Schunk}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%		GG - SPR 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%% DATA PREP

\section{GG SPR initial data prep}

Create figs subdirectory.

\begin{Schunk}
\begin{Sinput}
 if (!is.element("figs", system("ls -d"))[1]) {
     system("mkdir figs")
 }
\end{Sinput}
\end{Schunk}

Merge RT data with length and frequency statistics for each word

\begin{Schunk}
\begin{Sinput}
 raw.spr.data <- read.table("gg-spr06-data.txt")
 colnames(raw.spr.data) <- c("subj", "expt", "item", "condition", "roi", "word", "RT", "embedding", 
     "intervention")
 exp.items <- subset(raw.spr.data, expt == "E1")
 exp.items$word <- tolower(as.character(exp.items$word))
 exp.items$word <- as.character(sub("\\.", "", exp.items$word))
 lex <- read.table("anc-lexicon.txt", quote = "", skip = 1)
 colnames(lex) <- c("word", "freq")
 lex$word <- tolower(as.character(lex$word))
 exp.items <- merge(exp.items, lex, by = c("word"), all.x = TRUE)
 exp.items$len <- nchar(as.character(exp.items$word))
\end{Sinput}
\end{Schunk}


%Translate frequency to words-per-million

%<<freq-as-words-per-million>>=
%roundup <- function(x) trunc(x+0.5) ;
%exp.items$freq<-roundup(exp.items$freq/11.508216)
%@


Merge comprehension Q accuracy with data. GG-SPR output did not have the comprehension questions defined as a separate ROI with a column for accuracy. Thus, for this experiment (but not BB-SPR or either eyetracking expt), accuracy data was created in a different file and read in.

\begin{Schunk}
\begin{Sinput}
 q.data <- read.table("gg-spr06-question-data.txt")
 colnames(q.data) <- c("subj", "expt", "item", "condition", "roi", "response", "correct", 
     "RT")
 q.data <- q.data[, c(1, 2, 3, 4, 7), ]
 q.data$correct <- ifelse(q.data$correct == 1, "correct", "incorrect")
 d <- merge(exp.items, q.data, all.x = TRUE)
\end{Sinput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 d <- subset(d, subj != "108")
\end{Sinput}
\end{Schunk}


Recode position so the first position is 1, not zero.  Then code the
critical verb in each condition and subset out the critical verb.

\begin{Schunk}
\begin{Sinput}
 d$roi <- as.integer(as.character(d$roi)) + 1
\end{Sinput}
\end{Schunk}

\begin{Schunk}
\begin{Sinput}
 mean.freq <- mean(d$freq, na.rm = TRUE)
 mean.len <- mean(d$len, na.rm = TRUE)
 d$cfreq <- d$freq - mean.freq
 d$clen <- (1/d$len) - mean.len
\end{Sinput}
\end{Schunk}

Make item and subject categorical factors, not numerical values:

\begin{Schunk}
\begin{Sinput}
 d$item <- factor(d$item)
 d$subj <- factor(d$subj)
\end{Sinput}
\end{Schunk}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%%  ACCURACY.  Assumes matrix 'd'.  Only result is new column in
%%     'd', which is 'good.subj', identifying good subjects. 




\section{Accuracy}

Count accuracy and errors for each subject:

\begin{Schunk}
\begin{Sinput}
 d.melt <- melt(d, measure = "correct", variable_name = "measure", na.rm = FALSE)
 subj.acc <- cast(d.melt, subj ~ value)
 subj.acc$incorrect[is.na(subj.acc$incorrect)] <- 0
 subj.acc$percent <- round(100 * subj.acc$correct/(subj.acc$correct + subj.acc$incorrect))
\end{Sinput}
\end{Schunk}

\begin{Schunk}
\begin{Sinput}
 xtable(subj.acc, caption = "Accuracies for each subject.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:39 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlrrrr}
  \hline
 & subj & correct & incorrect & NA & percent \\ 
  \hline
1 & 1 & 398 & 176.00 &   0 & 69.00 \\ 
  2 & 2 & 423 & 137.00 &  28 & 76.00 \\ 
  3 & 3 & 428 & 128.00 &  33 & 77.00 \\ 
  4 & 4 & 434 & 142.00 &   0 & 75.00 \\ 
  5 & 5 & 436 & 117.00 &  32 & 79.00 \\ 
  6 & 6 & 372 & 174.00 &  37 & 68.00 \\ 
  7 & 7 & 422 & 152.00 &   0 & 74.00 \\ 
  8 & 8 & 412 & 163.00 &   0 & 72.00 \\ 
  9 & 9 & 312 & 263.00 &   0 & 54.00 \\ 
  10 & 10 & 421 & 155.00 &   0 & 73.00 \\ 
  11 & 11 & 417 & 136.00 &  32 & 75.00 \\ 
  12 & 12 & 450 & 119.00 &   0 & 79.00 \\ 
  13 & 13 & 388 & 186.00 &   0 & 68.00 \\ 
  14 & 14 & 456 & 119.00 &   0 & 79.00 \\ 
  15 & 15 & 377 & 198.00 &   0 & 66.00 \\ 
  16 & 16 & 470 & 106.00 &   0 & 82.00 \\ 
  17 & 17 & 453 & 100.00 &  32 & 82.00 \\ 
  18 & 18 & 395 & 174.00 &   0 & 69.00 \\ 
  19 & 19 & 416 & 158.00 &   0 & 72.00 \\ 
  20 & 20 & 391 & 184.00 &   0 & 68.00 \\ 
  21 & 21 & 435 & 140.00 &   0 & 76.00 \\ 
  22 & 22 & 487 & 89.00 &   0 & 85.00 \\ 
  23 & 23 & 377 & 176.00 &  32 & 68.00 \\ 
  24 & 24 & 458 & 111.00 &   0 & 80.00 \\ 
  25 & 25 & 460 & 114.00 &   0 & 80.00 \\ 
  26 & 26 & 405 & 170.00 &   0 & 70.00 \\ 
  27 & 27 & 416 & 159.00 &   0 & 72.00 \\ 
  28 & 28 & 450 & 126.00 &   0 & 78.00 \\ 
  29 & 29 & 423 & 149.00 &   0 & 74.00 \\ 
  30 & 30 & 430 & 139.00 &   0 & 76.00 \\ 
  31 & 31 & 439 & 135.00 &   0 & 76.00 \\ 
  32 & 32 & 445 & 130.00 &   0 & 77.00 \\ 
  33 & 33 & 501 & 74.00 &   0 & 87.00 \\ 
  34 & 34 & 450 & 126.00 &   0 & 78.00 \\ 
  35 & 35 & 407 & 146.00 &  32 & 74.00 \\ 
  36 & 36 & 426 & 143.00 &   0 & 75.00 \\ 
  37 & 37 & 472 & 102.00 &   0 & 82.00 \\ 
  38 & 38 & 357 & 218.00 &   0 & 62.00 \\ 
  39 & 39 & 497 & 78.00 &   0 & 86.00 \\ 
  40 & 40 & 389 & 187.00 &   0 & 68.00 \\ 
  41 & 41 & 413 & 159.00 &   0 & 72.00 \\ 
  42 & 42 & 366 & 203.00 &   0 & 64.00 \\ 
  43 & 43 & 451 & 123.00 &   0 & 79.00 \\ 
  44 & 44 & 375 & 200.00 &   0 & 65.00 \\ 
  45 & 45 & 464 & 111.00 &   0 & 81.00 \\ 
  46 & 46 & 321 & 255.00 &   0 & 56.00 \\ 
  47 & 47 & 462 & 91.00 &  32 & 84.00 \\ 
  48 & 48 & 428 & 141.00 &   0 & 75.00 \\ 
  49 & 49 & 412 & 162.00 &   0 & 72.00 \\ 
   \hline
\end{tabular}
\caption{Accuracies for each subject.}
\end{center}
\end{table}\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 xtable(sort_df(subj.acc, vars = "percent"), caption = "Accuracies for each subject, sorted by accuracy from worst to best.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:39 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlrrrr}
  \hline
 & subj & correct & incorrect & NA & percent \\ 
  \hline
9 & 9 & 312 & 263.00 &   0 & 54.00 \\ 
  46 & 46 & 321 & 255.00 &   0 & 56.00 \\ 
  38 & 38 & 357 & 218.00 &   0 & 62.00 \\ 
  42 & 42 & 366 & 203.00 &   0 & 64.00 \\ 
  44 & 44 & 375 & 200.00 &   0 & 65.00 \\ 
  15 & 15 & 377 & 198.00 &   0 & 66.00 \\ 
  6 & 6 & 372 & 174.00 &  37 & 68.00 \\ 
  13 & 13 & 388 & 186.00 &   0 & 68.00 \\ 
  20 & 20 & 391 & 184.00 &   0 & 68.00 \\ 
  23 & 23 & 377 & 176.00 &  32 & 68.00 \\ 
  40 & 40 & 389 & 187.00 &   0 & 68.00 \\ 
  1 & 1 & 398 & 176.00 &   0 & 69.00 \\ 
  18 & 18 & 395 & 174.00 &   0 & 69.00 \\ 
  26 & 26 & 405 & 170.00 &   0 & 70.00 \\ 
  8 & 8 & 412 & 163.00 &   0 & 72.00 \\ 
  19 & 19 & 416 & 158.00 &   0 & 72.00 \\ 
  27 & 27 & 416 & 159.00 &   0 & 72.00 \\ 
  41 & 41 & 413 & 159.00 &   0 & 72.00 \\ 
  49 & 49 & 412 & 162.00 &   0 & 72.00 \\ 
  10 & 10 & 421 & 155.00 &   0 & 73.00 \\ 
  7 & 7 & 422 & 152.00 &   0 & 74.00 \\ 
  29 & 29 & 423 & 149.00 &   0 & 74.00 \\ 
  35 & 35 & 407 & 146.00 &  32 & 74.00 \\ 
  4 & 4 & 434 & 142.00 &   0 & 75.00 \\ 
  11 & 11 & 417 & 136.00 &  32 & 75.00 \\ 
  36 & 36 & 426 & 143.00 &   0 & 75.00 \\ 
  48 & 48 & 428 & 141.00 &   0 & 75.00 \\ 
  2 & 2 & 423 & 137.00 &  28 & 76.00 \\ 
  21 & 21 & 435 & 140.00 &   0 & 76.00 \\ 
  30 & 30 & 430 & 139.00 &   0 & 76.00 \\ 
  31 & 31 & 439 & 135.00 &   0 & 76.00 \\ 
  3 & 3 & 428 & 128.00 &  33 & 77.00 \\ 
  32 & 32 & 445 & 130.00 &   0 & 77.00 \\ 
  28 & 28 & 450 & 126.00 &   0 & 78.00 \\ 
  34 & 34 & 450 & 126.00 &   0 & 78.00 \\ 
  5 & 5 & 436 & 117.00 &  32 & 79.00 \\ 
  12 & 12 & 450 & 119.00 &   0 & 79.00 \\ 
  14 & 14 & 456 & 119.00 &   0 & 79.00 \\ 
  43 & 43 & 451 & 123.00 &   0 & 79.00 \\ 
  24 & 24 & 458 & 111.00 &   0 & 80.00 \\ 
  25 & 25 & 460 & 114.00 &   0 & 80.00 \\ 
  45 & 45 & 464 & 111.00 &   0 & 81.00 \\ 
  16 & 16 & 470 & 106.00 &   0 & 82.00 \\ 
  17 & 17 & 453 & 100.00 &  32 & 82.00 \\ 
  37 & 37 & 472 & 102.00 &   0 & 82.00 \\ 
  47 & 47 & 462 & 91.00 &  32 & 84.00 \\ 
  22 & 22 & 487 & 89.00 &   0 & 85.00 \\ 
  39 & 39 & 497 & 78.00 &   0 & 86.00 \\ 
  33 & 33 & 501 & 74.00 &   0 & 87.00 \\ 
   \hline
\end{tabular}
\caption{Accuracies for each subject, sorted by accuracy from worst to best.}
\end{center}
\end{table}\end{Schunk}


Who are the bad/lazy subjects?

\begin{Schunk}
\begin{Sinput}
 d$good.subj <- subj.acc$percent[d$subj] > 70
\end{Sinput}
\end{Schunk}



\clearpage

Count accuracy and errors for each item:

\begin{Schunk}
\begin{Sinput}
 item.acc <- cast(d.melt, item ~ value)
 item.acc$incorrect[is.na(item.acc$incorrect)] <- 0
 item.acc$percent <- round(100 * item.acc$correct/(item.acc$correct + item.acc$incorrect))
\end{Sinput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 xtable(item.acc, caption = "Accuracies for each item.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:39 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlrrrr}
  \hline
 & item & correct & incorrect & NA & percent \\ 
  \hline
1 & 1 & 489 & 428.00 &   0 & 53.00 \\ 
  2 & 2 & 711 & 306.00 &   0 & 70.00 \\ 
  3 & 3 & 387 & 425.00 &   0 & 48.00 \\ 
  4 & 4 & 799 & 16.00 &   0 & 98.00 \\ 
  5 & 5 & 879 & 256.00 &   0 & 77.00 \\ 
  6 & 6 & 551 & 363.00 &   0 & 60.00 \\ 
  7 & 7 & 693 & 427.00 &  70 & 62.00 \\ 
  8 & 8 & 713 & 126.00 & 220 & 85.00 \\ 
  9 & 9 & 716 & 219.00 &   0 & 77.00 \\ 
  10 & 10 & 1040 & 20.00 &   0 & 98.00 \\ 
  11 & 11 & 768 & 294.00 &   0 & 72.00 \\ 
  12 & 12 & 876 & 211.00 &   0 & 81.00 \\ 
  13 & 13 & 680 & 237.00 &   0 & 74.00 \\ 
  14 & 14 & 333 & 439.00 &   0 & 43.00 \\ 
  15 & 15 & 675 & 112.00 &   0 & 86.00 \\ 
  16 & 16 & 555 & 260.00 &   0 & 68.00 \\ 
  17 & 17 & 659 & 280.00 &   0 & 70.00 \\ 
  18 & 18 & 596 & 269.00 &   0 & 69.00 \\ 
  19 & 19 & 854 & 39.00 &   0 & 96.00 \\ 
  20 & 20 & 826 & 44.00 &   0 & 95.00 \\ 
  21 & 21 & 693 & 299.00 &   0 & 70.00 \\ 
  22 & 22 & 882 & 55.00 &   0 & 94.00 \\ 
  23 & 23 & 896 & 19.00 &   0 & 98.00 \\ 
  24 & 24 & 526 & 388.00 &   0 & 58.00 \\ 
  25 & 25 & 811 & 0.00 &   0 & 100.00 \\ 
  26 & 26 & 133 & 835.00 &   0 & 14.00 \\ 
  27 & 27 & 1011 & 21.00 &   0 & 98.00 \\ 
  28 & 28 & 451 & 413.00 &   0 & 52.00 \\ 
  29 & 29 & 818 & 97.00 &   0 & 89.00 \\ 
  30 & 30 & 666 & 346.00 &   0 & 66.00 \\ 
   \hline
\end{tabular}
\caption{Accuracies for each item.}
\end{center}
\end{table}\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 xtable(sort_df(item.acc, vars = "percent"), caption = "Accuracies for each item, sorted by accuracy from worst to best.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:39 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlrrrr}
  \hline
 & item & correct & incorrect & NA & percent \\ 
  \hline
26 & 26 & 133 & 835.00 &   0 & 14.00 \\ 
  14 & 14 & 333 & 439.00 &   0 & 43.00 \\ 
  3 & 3 & 387 & 425.00 &   0 & 48.00 \\ 
  28 & 28 & 451 & 413.00 &   0 & 52.00 \\ 
  1 & 1 & 489 & 428.00 &   0 & 53.00 \\ 
  24 & 24 & 526 & 388.00 &   0 & 58.00 \\ 
  6 & 6 & 551 & 363.00 &   0 & 60.00 \\ 
  7 & 7 & 693 & 427.00 &  70 & 62.00 \\ 
  30 & 30 & 666 & 346.00 &   0 & 66.00 \\ 
  16 & 16 & 555 & 260.00 &   0 & 68.00 \\ 
  18 & 18 & 596 & 269.00 &   0 & 69.00 \\ 
  2 & 2 & 711 & 306.00 &   0 & 70.00 \\ 
  17 & 17 & 659 & 280.00 &   0 & 70.00 \\ 
  21 & 21 & 693 & 299.00 &   0 & 70.00 \\ 
  11 & 11 & 768 & 294.00 &   0 & 72.00 \\ 
  13 & 13 & 680 & 237.00 &   0 & 74.00 \\ 
  5 & 5 & 879 & 256.00 &   0 & 77.00 \\ 
  9 & 9 & 716 & 219.00 &   0 & 77.00 \\ 
  12 & 12 & 876 & 211.00 &   0 & 81.00 \\ 
  8 & 8 & 713 & 126.00 & 220 & 85.00 \\ 
  15 & 15 & 675 & 112.00 &   0 & 86.00 \\ 
  29 & 29 & 818 & 97.00 &   0 & 89.00 \\ 
  22 & 22 & 882 & 55.00 &   0 & 94.00 \\ 
  20 & 20 & 826 & 44.00 &   0 & 95.00 \\ 
  19 & 19 & 854 & 39.00 &   0 & 96.00 \\ 
  4 & 4 & 799 & 16.00 &   0 & 98.00 \\ 
  10 & 10 & 1040 & 20.00 &   0 & 98.00 \\ 
  23 & 23 & 896 & 19.00 &   0 & 98.00 \\ 
  27 & 27 & 1011 & 21.00 &   0 & 98.00 \\ 
  25 & 25 & 811 & 0.00 &   0 & 100.00 \\ 
   \hline
\end{tabular}
\caption{Accuracies for each item, sorted by accuracy from worst to best.}
\end{center}
\end{table}\end{Schunk}


\clearpage

Count accuracy and errors for each condition

\begin{Schunk}
\begin{Sinput}
 condition.acc <- cast(d.melt, condition ~ value)
 condition.acc$incorrect[is.na(condition.acc$incorrect)] <- 0
 condition.acc$percent <- round(100 * condition.acc$correct/(condition.acc$correct + condition.acc$incorrect))
\end{Sinput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 xtable(condition.acc, caption = "Accuracies for each condition.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:40 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlrrrr}
  \hline
 & condition & correct & incorrect & NA & percent \\ 
  \hline
1 & a & 2810 & 666.00 &  61 & 81.00 \\ 
  2 & b & 3406 & 837.00 &   0 & 80.00 \\ 
  3 & c & 3730 & 1012.00 &   0 & 79.00 \\ 
  4 & d & 3258 & 1149.00 & 229 & 74.00 \\ 
  5 & e & 3682 & 1602.00 &   0 & 70.00 \\ 
  6 & f & 3801 & 1978.00 &   0 & 66.00 \\ 
   \hline
\end{tabular}
\caption{Accuracies for each condition.}
\end{center}
\end{table}\end{Schunk}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%%  SPILLOVER.  Assumes matrix 'd'.  New columns are added to 'd', indicating the length, frequency and RT for the region prior to a given observation.


\subsection{Self-paced reading spill-over preprocessing}


Compute spillover information.

\begin{Schunk}
\begin{Sinput}
 matrix <- subset(d, select = c(subj, condition, item, word, roi, RT, len, freq, correct))
 matrix$roi <- matrix$roi + 1
 matrix$prev.RT <- matrix$RT
 matrix$prev.len <- matrix$len
 matrix$prev.freq <- matrix$freq
 matrix$RT <- NULL
 matrix$len <- NULL
 matrix$freq <- NULL
 d <- merge(d, matrix, by = c("subj", "item", "condition", "roi", "correct"), all.x = TRUE)
 d$word <- d$word.x
 d$word.x <- NULL
 d$word.y <- NULL
\end{Sinput}
\end{Schunk}

\begin{Schunk}
\begin{Sinput}
 mean.prev.len <- mean(d$prev.len, na.rm <- TRUE, trim = 0)
 mean.prev.freq <- mean(d$prev.freq, na.rm <- TRUE, trim = 0)
 d$prev.cfreq <- d$prev.freq - mean.prev.freq
 d$prev.clen <- (1/d$prev.len) - mean.prev.len
\end{Sinput}
\end{Schunk}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%% TRIMMING 
%%    Trims at 3SD by condition, replicating GG-05 procedure. Assumes
%%    matrix 'd' which has columns ``RT'' and ``condition''.  Produces a
%%    melted matrix d.melt.

\newpage 

\section{Trimming}


Now we do a quick and dirty trim of the RTs by 3SDs (this replicates the
original Grodner \& Gibson (2005) procedure).

\begin{Schunk}
\begin{Sinput}
 d.melt <- melt(d, measure = "RT", variable_name = "measure", na.rm = FALSE)
 full.table <- length(d.melt$value)
\end{Sinput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 RT.sd <- cast(d.melt, condition ~ ., function(x) c(m = round(mean(x)), sd = sd(x)))
 RT.sd$high.cutoff <- RT.sd$m + 3 * RT.sd$sd
 RT.sd$low.cutoff <- RT.sd$m - 3 * RT.sd$sd
 for (c in levels(d.melt$condition)) {
     d.melt$value[(d.melt$condition == RT.sd$condition[c]) & (d.melt$value > RT.sd$high.cutoff[c])] <- NA
     d.melt$value[(d.melt$condition == RT.sd$condition[c]) & (d.melt$value < RT.sd$low.cutoff[c])] <- NA
 }
 d.melt <- d.melt[!is.na(d.melt$value), ]
 trimmed.table <- print(length(d.melt$value))
\end{Sinput}
\begin{Soutput}
[1] 28221
\end{Soutput}
\begin{Sinput}
 print((full.table - trimmed.table)/full.table)
\end{Sinput}
\begin{Soutput}
[1] 0
\end{Soutput}
\end{Schunk}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%% gg-spr06 PLOTS of ALL REGIONS
%%
%%    Assumes a matrix d.melt. 

\begin{Schunk}
\begin{Sinput}
 rtlimits <- c(0, 1200)
 sdlimits <- c(0, 1200)
\end{Sinput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 ci <- function(scores) {
     m <- mean(scores)
     stderr <- se(scores)
     len <- length(scores)
     upper <- m + stderr
     lower <- m - stderr
     return(data.frame(lower = lower, upper = upper))
 }
 plot.intervals <- function(xrange, lower, upper, width, lty = "solid", col = "grey", lwd = 1) {
     for (j in 1:(length(xrange))) {
         lines(x = rep(xrange[j], 2), y = c(lower[j], upper[j]), lty = lty, col = col, lwd = lwd, 
             type = "l")
         arrows(x0 = as.numeric(xrange[j]), y0 = lower[j], x1 = as.numeric(xrange[j]), y1 = upper[j], 
             length = width, angle = 90, code = 3, lty = lty, col = col, lwd = lwd)
     }
 }
 add.axes.and.title <- function(title, line = 0.8) {
     axis(1, at = 1:3, labels = c("none", "PP", "RC"))
     axis(1, at = 4:6, labels = c("none", "PP", "RC"))
     lab.pos <- rtlimits[1] - diff(rtlimits) * 0.23
     text(labels = c("Matrix", "Embedded"), x = c(2, 5), y = c(lab.pos, lab.pos), cex = 1.1)
     mtext(title, side = 3, line = line, cex = 1.2 * 0.8, font = 2)
 }
\end{Sinput}
\end{Schunk}


\newpage


\section{Plots of RTs by condition, subject, and item}

\subsection{All the subjects}


\begin{Schunk}
\begin{Sinput}
 plot.data <- d.melt[d.melt$roi <= 11, ]
 reorder.frame <- data.frame(condition = c(rep("a", 11), rep("b", 11), rep("c", 11), rep("d", 
     11), rep("e", 11), rep("f", 11)), roi = rep(1:11, 6), plot.order = c(1, 5, 11, NA, NA, 
     NA, NA, NA, NA, NA, NA, 1, 5, 8, 9, 10, 11, NA, NA, NA, NA, NA, 1, 5, 6, 7, 8, 9, 10, 
     11, NA, NA, NA, 1, 2, 3, 4, 5, 11, NA, NA, NA, NA, NA, 1, 2, 3, 4, 5, 8, 9, 10, 11, 
     NA, NA, 1:11))
 plot.data <- merge(plot.data, reorder.frame)
\end{Sinput}
\end{Schunk}



\begin{Schunk}
\begin{Sinput}
 color1 <- gray(0.7)
 color2 <- gray(0.3)
 color3 <- "black"
 lty1 <- "dashed"
 lty2 <- "solid"
\end{Sinput}
\end{Schunk}






\begin{Schunk}
\begin{Sinput}
 trim.value <- 60000
 rt.mean <- cast(plot.data, condition + plot.order ~ ., function(x) c(m = round(mean(x)), 
     CI = round(ci(x))), subset = ((value < trim.value)))
 rt.mean <- rt.mean[!(rt.mean$plot.order == "NA"), ]
 rt.mean <- rt.mean[-c(4, 11, 20, 27, 37), ]
 rt.sd.subj <- cast(d.melt, condition + subj ~ ., function(x) round(sd(x)), subset = ((value < 
     trim.value)))
 rt.sd <- cast(d.melt, condition ~ ., function(x) round(mean(x, na.rm = TRUE)))
 dummy <- data.frame(region = c(1:11), m = rep(-999, 11))
 subtitle <- "Error bars are one standard error."
\end{Sinput}
\end{Schunk}



\begin{center}
\begin{Schunk}
\begin{Sinput}
 plot(m ~ region, data = dummy, xaxt = "n", type = "n", cex.axis = 1.2, cex.lab = 1.5, cex.main = 1.5, 
     cex.sub = 0.7, xlab = "Region", ylab = "Reading time (ms)", main = "Experiment 1 self-paced reading times", 
     sub = subtitle, ylim = rtlimits)
 axis(side = 1, at = 1:11, labels = FALSE)
 mtext(side = 1, at = c(1:11), line = 1, cex = 0.7, c("The", "(admini-", "who", "the)", "nurse", 
     "(who", "was", "(from", "the", "clinic))", "supervised"))
 mtext(side = 1, at = c(1:11), line = 2, cex = 0.7, c("", "strator", "", "", "", "", "", 
     "", "", "", ""))
 legend(x = rt.mean$plot.order[1], y = 1000, legend = c("matrix - unmodified", "PP", "RC", 
     "embedded - unmodified", "PP", "RC"), lwd = c(2), lty = c(lty1, lty1, lty1, lty2, lty2, 
     lty2), col = c(color1, color2, color3, color1, color2, color3), pch = c(1, 1, 1, 15, 
     15, 15), bty = "n", cex = 0.9)
 plot.intervals(xrange = rep(11, 6), lower = rt.mean$CI.lower[rt.mean$plot.order == 11], 
     upper = rt.mean$CI.upper[rt.mean$plot.order == 11], width = 0.05, lwd = 1, col = "black", 
     lty = "solid")
 condition <- "a"
 point.type <- c(1)
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, cex = 1.2, col = color1)
 condition <- "b"
 dom1 <- 8:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order > 7]
 color <- color2
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty1, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "c"
 dom1 <- 5:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order > 4]
 color <- color3
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty1, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "d"
 dom1 <- 1:5
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order < 6]
 point.type <- c(15)
 color <- color1
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "e"
 dom1 <- 1:5
 dom2 <- 8:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order < 6]
 range2 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order > 7]
 color <- color2
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
 lines(x = dom2, y = range2, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "f"
 dom1 <- 1:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order]
 color <- color3
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
\end{Sinput}
\end{Schunk}
\includegraphics{figs/-all-roi-gg-spr-by-condition}


\end{center}



\newpage

\subsection{Accurate subjects only}

Now lets look at the RT pattern for the accurate subjects (defined above).

\begin{Schunk}
\begin{Sinput}
 trim.value <- 60000
 rt.mean <- cast(plot.data, condition + plot.order ~ ., function(x) c(m = round(mean(x)), 
     CI = round(ci(x))), subset = ((value < trim.value) & (good.subj == TRUE)))
 rt.mean <- rt.mean[!(rt.mean$plot.order == "NA"), ]
 rt.mean <- rt.mean[-c(4, 11, 20, 27, 37), ]
\end{Sinput}
\end{Schunk}



\begin{center}
\begin{Schunk}
\begin{Sinput}
 plot(m ~ region, data = dummy, xaxt = "n", type = "n", cex.axis = 1.2, cex.lab = 1.5, cex.main = 1.5, 
     cex.sub = 0.7, xlab = "Region", ylab = "Reading time (ms)", main = "Experiment 1 self-paced reading times", 
     sub = subtitle, ylim = rtlimits)
 axis(side = 1, at = 1:11, labels = FALSE)
 mtext(side = 1, at = c(1:11), line = 1, cex = 0.7, c("The", "(admini-", "who", "the)", "nurse", 
     "(who", "was", "(from", "the", "clinic))", "supervised"))
 mtext(side = 1, at = c(1:11), line = 2, cex = 0.7, c("", "strator", "", "", "", "", "", 
     "", "", "", ""))
 legend(x = rt.mean$plot.order[1], y = 1000, legend = c("matrix - unmodified", "PP", "RC", 
     "embedded - unmodified", "PP", "RC"), lwd = c(2), lty = c(lty1, lty1, lty1, lty2, lty2, 
     lty2), col = c(color1, color2, color3, color1, color2, color3), pch = c(1, 1, 1, 15, 
     15, 15), bty = "n", cex = 0.9)
 plot.intervals(xrange = rep(11, 6), lower = rt.mean$CI.lower[rt.mean$plot.order == 11], 
     upper = rt.mean$CI.upper[rt.mean$plot.order == 11], width = 0.05, lwd = 1, col = "black", 
     lty = "solid")
 condition <- "a"
 point.type <- c(1)
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, cex = 1.2, col = color1)
 condition <- "b"
 dom1 <- 8:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order > 7]
 color <- color2
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty1, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "c"
 dom1 <- 5:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order > 4]
 color <- color3
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty1, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "d"
 dom1 <- 1:5
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order < 6]
 point.type <- c(15)
 color <- color1
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "e"
 dom1 <- 1:5
 dom2 <- 8:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order < 6]
 range2 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order > 7]
 color <- color2
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
 lines(x = dom2, y = range2, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
 condition <- "f"
 dom1 <- 1:11
 range1 <- rt.mean$m[rt.mean$condition == condition & rt.mean$plot.order]
 color <- color3
 points(rt.mean$plot.order[rt.mean$condition == condition], rt.mean$m[rt.mean$condition == 
     condition], pch = point.type, col = color, cex = 1.2)
 lines(x = dom1, y = range1, lty = lty2, col = color, pch = point.type, lwd = 1, type = "o")
\end{Sinput}
\end{Schunk}
\includegraphics{figs/-all-roi-gg-spr-by-condition-good-subj-only}

\end{center}


\begin{Schunk}
\begin{Sinput}
 d <- d.melt
\end{Sinput}
\end{Schunk}


\begin{Schunk}
\begin{Sinput}
 d$criticalv1 <- ifelse((d$condition == "a" & d$roi == 3) | (d$condition == "b" & d$roi == 
     6) | (d$condition == "c" & d$roi == 8) | (d$condition == "d" & d$roi == 6) | (d$condition == 
     "e" & d$roi == 9) | (d$condition == "f" & d$roi == 11), "yes", "no")
 d <- subset(d, criticalv1 == "yes")
 d$criticalv1 <- factor(d$criticalv1)
 full.table.v1 <- print(length(d$value))
\end{Sinput}
\begin{Soutput}
[1] 1470
\end{Soutput}
\end{Schunk}


\subsection{Set up contrasts}

It's often nice to be able to interpret the contrast coefficients of contrasts as if
they represent the estimated mean difference between the two groups
represented by the contrast.  In order to ensure this, the contrasts must
be normalized as follows (e.g. see p. B-13 in Maxwell \& Delaney second
edition).  This new function will also check to be sure 
that the contrasts are orthogonal, and they are in fact contrasts.

\begin{Schunk}
\begin{Sinput}
 normalize.and.check.contrasts <- function(contr) {
     for (col in 1:ncol(contr)) {
         pos.score <- mean(contr[contr[, col] > 0, col])
         neg.score <- mean(contr[contr[, col] < 0, col])
         diff <- pos.score - neg.score
         contr[, col] <- contr[, col]/diff
     }
     if (!isTRUE(all.equal(mean(apply(contr, MARGIN = 2, FUN = mean)), 0))) 
         message("WARNING!!! Contrast matrix contains non-contrasts!!")
     m <- t(contr) %*% contr
     should.be.diag <- m - diag(diag(m))
     check.diag <- all(sapply(as.vector(should.be.diag), FUN = function(x) {
         return(all.equal(x, 0))
     }))
     if (!check.diag) 
         message("WARNING!!! Contrast matrix is non-orthogonal.")
     return(contr)
 }
\end{Sinput}
\end{Schunk}

Now set up and normalize the first contrast set.

\begin{Schunk}
\begin{Sinput}
 conditions <- as.factor(c("a", "b", "c", "d", "e", "f"))
 contr.set1.orig <- cbind(c1 = c(-1, -1, -1, 1, 1, 1), c2 = c(-2, 1, 1, 0, 0, 0), c3 = c(0, 
     -1, 1, 0, 0, 0), c4 = c(0, 0, 0, -2, 1, 1), c5 = c(0, 0, 0, 0, -1, 1))
 rownames(contr.set1.orig) <- conditions
 contr.set1 <- normalize.and.check.contrasts(contr.set1.orig)
\end{Sinput}
\end{Schunk}

Let's take a look at the codings:

\begin{Schunk}
\begin{Sinput}
 xtable(contr.set1.orig, caption = "Original coding of contrast set 1.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:43 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
  \hline
 & c1 & c2 & c3 & c4 & c5 \\ 
  \hline
a & -1.00 & -2.00 & 0.00 & 0.00 & 0.00 \\ 
  b & -1.00 & 1.00 & -1.00 & 0.00 & 0.00 \\ 
  c & -1.00 & 1.00 & 1.00 & 0.00 & 0.00 \\ 
  d & 1.00 & 0.00 & 0.00 & -2.00 & 0.00 \\ 
  e & 1.00 & 0.00 & 0.00 & 1.00 & -1.00 \\ 
  f & 1.00 & 0.00 & 0.00 & 1.00 & 1.00 \\ 
   \hline
\end{tabular}
\caption{Original coding of contrast set 1.}
\end{center}
\end{table}\begin{Sinput}
 xtable(contr.set1, caption = "Normalized coding of contrast set 1.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:43 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
  \hline
 & c1 & c2 & c3 & c4 & c5 \\ 
  \hline
a & -0.50 & -0.67 & 0.00 & 0.00 & 0.00 \\ 
  b & -0.50 & 0.33 & -0.50 & 0.00 & 0.00 \\ 
  c & -0.50 & 0.33 & 0.50 & 0.00 & 0.00 \\ 
  d & 0.50 & 0.00 & 0.00 & -0.67 & 0.00 \\ 
  e & 0.50 & 0.00 & 0.00 & 0.33 & -0.50 \\ 
  f & 0.50 & 0.00 & 0.00 & 0.33 & 0.50 \\ 
   \hline
\end{tabular}
\caption{Normalized coding of contrast set 1.}
\end{center}
\end{table}\end{Schunk}


For the second contrast set we only have two contrasts of interest and use the R contrast function to find
orthogonal contrasts to fill out the set. 


Contrast c6 is interaction of embedding and locality.
Contrast c7 is interaction of embedding and modification type
Contrasts 8-10 are not theoretically interesting; weights are assigned by R.



\begin{Schunk}
\begin{Sinput}
 conditions <- as.factor(c("a", "b", "c", "d", "e", "f"))
 contr.set2.orig <- cbind(c6 = c(2, -1, -1, -2, 1, 1), c7 = c(0, 1, -1, 0, -1, 1))
 contrasts(conditions, how.many = 5) <- contr.set2.orig
 contr.set2.orig <- contrasts(conditions)
 colnames(contr.set2.orig) <- c("c6", "c7", "c8", "c9", "c10")
 contr.set2 <- normalize.and.check.contrasts(contr.set2.orig)
\end{Sinput}
\end{Schunk}

Let's take a look at the codings:

\begin{Schunk}
\begin{Sinput}
 xtable(contr.set2.orig, caption = "Original coding of contrast set 2.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:43 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
  \hline
 & c6 & c7 & c8 & c9 & c10 \\ 
  \hline
a & 2.00 & 0.00 & 0.11 & -0.46 & -0.52 \\ 
  b & -1.00 & 1.00 & -0.56 & 0.25 & -0.34 \\ 
  c & -1.00 & -1.00 & -0.41 & -0.47 & 0.33 \\ 
  d & -2.00 & 0.00 & 0.69 & -0.01 & -0.17 \\ 
  e & 1.00 & -1.00 & 0.01 & 0.71 & 0.01 \\ 
  f & 1.00 & 1.00 & 0.17 & -0.02 & 0.69 \\ 
   \hline
\end{tabular}
\caption{Original coding of contrast set 2.}
\end{center}
\end{table}\begin{Sinput}
 xtable(contr.set2, caption = "Normalized coding of contrast set 2.")
\end{Sinput}
% latex table generated in R 2.10.1 by xtable 1.5-6 package
% Mon Apr  5 02:36:44 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
  \hline
 & c6 & c7 & c8 & c9 & c10 \\ 
  \hline
a & 0.75 & 0.00 & 0.15 & -0.64 & -0.76 \\ 
  b & -0.38 & 0.50 & -0.77 & 0.35 & -0.50 \\ 
  c & -0.38 & -0.50 & -0.56 & -0.66 & 0.48 \\ 
  d & -0.75 & 0.00 & 0.94 & -0.01 & -0.24 \\ 
  e & 0.38 & -0.50 & 0.02 & 0.98 & 0.02 \\ 
  f & 0.38 & 0.50 & 0.23 & -0.02 & 1.00 \\ 
   \hline
\end{tabular}
\caption{Normalized coding of contrast set 2.}
\end{center}
\end{table}\end{Schunk}


Now we merge in the contrasts to the data frame:

\begin{Schunk}
\begin{Sinput}
 conditions <- as.factor(c("a", "b", "c", "d", "e", "f"))
 contr.set1 <- as.data.frame(contr.set1)
 contr.set1$condition <- conditions
 contr.set2 <- as.data.frame(contr.set2)
 contr.set2$condition <- conditions
 d <- merge(d, contr.set1, by = c("condition"))
 d <- merge(d, contr.set2, by = c("condition"))
\end{Sinput}
\end{Schunk}



\begin{Schunk}
\begin{Sinput}
 gg.spr.data <- d
 write.table(gg.spr.data, file = "gg-spr-analysis-input.txt", sep = "\t", quote = FALSE, 
     row.names = FALSE, col.names = TRUE)
\end{Sinput}
\end{Schunk}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%		BB-SPR 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%% DATA PREP
%%

%\SweaveInput{bb-spr-dataprep.rnw}

%\SweaveInput{center-len-freq.rnw}

%\SweaveInput{spr-factorize.rnw}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%%  ACCURACY.  Assumes matrix 'd'.  Only result is new column in
%%%     'd', which is 'good.subj', identifying good subjects. 

%
%\SweaveInput{accuracy.rnw}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%%  SPILLOVER.  Assumes matrix 'd'.  New columns are added to 'd', indicating the length, frequency and RT for the region prior to a given observation.

%\SweaveInput{spr-spillover.rnw}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% TRIMMING 
%%%    Trims at 3SD by condition, replicating GG-05 procedure. Assumes
%%%    matrix 'd' which has columns ``RT'' and ``condition''.  Produces a
%%%    melted matrix d.melt.

%\SweaveInput{trimming.rnw}

%
%<<reassign-data>>=
%bb.d.melt <- d.melt;
%@ 

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% bb-spr06  PLOTS of ALL REGIONS
%%%
%%%    Assumes a matrix d.melt.  

%<<set-plot-limits>>=
%rtlimits <- c(0, 500);
%sdlimits <- c(0, 500);
%@

%<<reassign-data>>=
%d.melt <- bb.d.melt;
%@ 

%
%\SweaveInput{all-roi-bb-plots.rnw}

%<<reassign-to-d-bb-spr>>=
%d <- d.melt
%@

%\SweaveInput{subset-v1.rnw}
%\SweaveInput{contrasts.rnw}

%

%<<output-final-formatted-data>>=
%bb.spr.data <- d;
%write.table(bb.spr.data,file="bb-spr-analysis-input.txt",sep="\t",quote=FALSE,row.names=FALSE,col.names=TRUE)
%@

%%
%%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%		GG - EYE 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%
%\SweaveInput{gg-eye-frequency-accuracy.rnw}

%
%<<remove-incorrectly-designed-items>>=
%## get rid of bad items. They were designed incorrectly. Item 2 uses the verb phrase "chatted with"--technically an intransitive verb. Item 25 

%data <- subset(gg.eye.data,(item!=25 & item!=2)) 
%@
%		
%<<get-rid-of-bad-subjects>>=
%#get rid of bad subjects (<70\% accuracy)

%	data<-data[data$subject!=11,]
%	data<-data[data$subject!=29,]
%	data<-data[data$subject!=30,]
%	data<-data[data$subject!=33,]
%@

%\SweaveInput{eye-factorize.rnw}

%
%<<add-new-measure>>=

%data$FFDX<-ifelse(data$FFD==data$FPRT | data$FFD==0,NA,data$FFD);
%@

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% SPILL-OVER PROCESSING
%%%    Assumes data is in 'data'.  Adds new spill-over columns to 'data' data-frame.

%\SweaveInput{spill-over-preprocess.rnw}

%<<save-gg-spill-over>>=
%write.table(data,file="gg-spillover-data.txt",sep="\t",quote=FALSE,row.names=FALSE,col.names=TRUE)
%@ 

%
%<<get-spill-over>>=
%data <- read.table(file="gg-spillover-data.txt",header=TRUE)
%data$subject <- factor(data$subject);
%data$trial <- ordered(data$trial);
%data$item <- factor(data$item)
%@

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% EYE processing

%<<reassign-data>>=
%d<-data
%@

%
%\SweaveInput{center-len-freq.rnw}
%\SweaveInput{calculate-arpd.rnw}

%%the data, as processed in calculate-arpd, is written in 'd-mat.txt'

%\SweaveInput{reshape-eye-data.rnw}

%<<reassign-data>>=
%gg.eye.data<-d.rs
%@
%\SweaveInput{all-region-gg-eye-plots.rnw}

%

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  
%%% SUBSET to critical verb region (v1).
%%%    Yields new variable 'd'.

%<<reassign-data>>=
%d<-gg.eye.data
%@

%\SweaveInput{subset-v1.rnw}

%

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% Define CONTRASTS and do initial means and CIs. Incorrect trials can be excluded in contrasts.rnw.
%%%     

%
%\SweaveInput{contrasts.rnw}
%\SweaveInput{get-rt-means.rnw}

%%d.cast will be used for mean RT plots
%%d will be used in the lmer models
%<<gg-eye-data-ready-for-analysis>>=
%gg.d.cast<-d.cast;
%gg.eye<-d
%@

%

%<<output-final-formatted-data>>=
%write.table(gg.eye,file="gg-eye-analysis-input.txt",sep="\t",quote=FALSE,row.names=FALSE,col.names=TRUE)
%@

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%		BB - EYE 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

%\SweaveInput{bb-eye-frequency.rnw}
%\SweaveInput{bb-eye-accuracy.rnw}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% Remove mistakes
%%%     

%Remove mistake in stimuli, item 9 had a low frequency verb; 13 was missing the subject in condition F; 36 had low frequency verb.

%<<remove-mistakes>>=
%bb.data <- subset(bb.data,(item!=9 
%& item!=13 
%& item!=36)) 

%@ 

%<<rename-data>>=
%data <- bb.data;
%@ 

%<<cut-partial-subject>>=

%#get rid of subject 702; he didn't finish the experiment

%data<-data[data$subject!="s702",]

%@

%\SweaveInput{eye-factorize.rnw}

%

%<<add-new-measure>>=

%data$FFDX<-ifelse(data$FFD==data$FPRT,NA,data$FFD);
%@

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% SPILL-OVER PROCESSING     
%%%   Assumes data is in 'data'.  Adds new spill-over columns to 'data' data-frame.

%\SweaveInput{spill-over-preprocess.rnw}

%<<save-bb-spill-over>>=
%write.table(data,file="bb-spillover-data.txt",sep="\t",quote=FALSE,row.names=FALSE,col.names=TRUE)
%@ 

%
%<<get-spill-over-and-re-factorize>>=
%data <- read.table(file="bb-spillover-data.txt",header=TRUE)
%data$subject <- factor(data$subject);
%data$trial <- ordered(data$trial);
%data$item <- factor(data$item)
%@

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% EYE processing
%<<reassign-data>>=
%d<-data
%@

%
%\SweaveInput{center-len-freq.rnw}
%\SweaveInput{calculate-arpd.rnw}
%\SweaveInput{reshape-eye-data.rnw}

%
%<<reassign-data>>=
%bb.eye.data<-d.rs;
%@

%\SweaveInput{all-region-bb-eye-plots.rnw}

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% SUBSET to critical verb region (v1).

%<<reassign-data>>=
%d<-bb.eye.data
%@

%\SweaveInput{subset-v1.rnw}

%

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% Define CONTRASTS 

%\SweaveInput{contrasts.rnw}
%\SweaveInput{get-rt-means.rnw}

%%d.cast will be used for mean RT plots
%%d will be used in the lmer models
%<<bb-eye-data-ready-for-analysis>>=
%bb.d.cast <- d.cast;
%bb.eye <- d
%@

%

%<<output-final-formatted-data>>=
%write.table(d, file="bb-eye-analysis-input.txt",sep="\t",quote=FALSE,row.names=FALSE,col.names=TRUE)
%@

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 
%%% RT plotting

%\SweaveInput{main-RT-fig.rnw}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 
%% HPD plotting


%%\SweaveInput{hpd-prep.rnw}
%%\SweaveInput{hpd-plots.rnw}


\begin{Schunk}
\begin{Sinput}
 options(prompt = "> ", continue = "+ ")
\end{Sinput}
\end{Schunk}

\end{document}
