*Experiment 4
version 16
clear all
macro drop _all
set linesize 80
set more off
set scheme s1manual
set graphics off /* create graphs without them popping up on screen */


//----------- open data file
use data/Expt4_2023_forAnalysis, clear

// how many trials ?
preserve
bysort subject trial: keep if _n == 1
di _N
restore

* # subjects
tab subject if cond == "cmp" , nofreq
display r(r)

tab subject if cond == "pseudo" , nofreq
display r(r)



//# ----- INITIATION
tabstat keytime if keytime > 50 & keytime < 1500 & keyNum == 1, by(wordtype)  statistics(n mean sem sd min max) columns(statistics)
tabstat keytime if keytime > 50 & keytime < 1500 & keyNum == 2, by(wordtype)  statistics(n mean sem sd min max) columns(statistics)

mixed keytime i.wordtypeN##i.keyNum  i.keyNum##c.stim_lg10wf keyNum##c.stimlen|| _all:R.subject || item: if keytime > 50 & keytime < 1500 & inlist(keyNum, 1,2) , mle nogroup nolrtest nofvlabel noretable

contrast i.wordtypeN##i.keyNum
contrast i.keyNum@i.wordtypeN
contrast i.keyNum#c.stim_lg10wf
contrast i.keyNum#c.stim_lg10wf
margins i.keyNum, dydx(c.stim_lg10wf)


quietly margin keyNum, at(stim_lg10wf=(.3/4))
marginsplot,  ytitle ("Predicted latencies (ms)") xtitle("Word frequency") title("") legend(subtitle("Letter position") rows(2) ring(0) pos(11)) name(E4_i_wdfreq, replace) aspectratio(.75) legend(off)


*with  c1 variables
mixed keytime i.wordtypeN##c.c1_lg10wf i.wordtypeN##c.c1len ///
	|| _all:R.subject || item: if keytime > 50 & keytime < 1500 & inlist(keyNum, 1) , mle nogroup nolrtest nofvlabel noretable

contrast i.wordtypeN#c.c1len
margins , dydx(c.c1len)
contrast i.wordtypeN#c.c1_lg10wf
margins i.wordtypeN, dydx(c.c1_lg10wf)

*with  c2 variables
mixed keytime ///
	i.wordtypeN##c.c2_lg10wf i.wordtypeN##c.c2len ///
	|| _all:R.subject || item: if keytime > 50 & keytime < 1500 & inlist(keyNum, 1) , mle nogroup nolrtest nofvlabel noretable

contrast i.wordtypeN#c.c2len
margins , dydx(c2len)
contrast i.wordtypeN#c.c2_lg10wf
margins , dydx(c2_lg10wf)


margin wordtypeN, at(c1_lg10wf = (.6(2)6))
marginsplot,  ytitle ("Predicted latencies (ms)") xtitle("Frequency of first constituent") title("") legend(subtitle("Wordtype") rows(2) ring(0) pos(2)) aspectratio(.75)
graph save  "output/figures/fig_E4_i_wfreq", replace
graph export  "output/figures/fig_E4_i_wfreq.png", replace*/



//# ----- BOUNDARY
//# -- Descriptive stats
tabstat keytime if keytime > 50 & keytime < 1000 & dist == -1, by(wordtype)  statistics(n mean sem sd min max) columns(statistics)
tabstat keytime if keytime > 50 & keytime < 1000 &  dist == 0, by(wordtype)  statistics(n mean sem sd min max) columns(statistics)

mixed keytime i.distNum##i.wordtypeN ///
	|| _all:R.subject || item: if keytime > 50 & keytime < 1000 & inrange(dist,-1,0) , mle nogroup nolrtest nofvlabel noretable

contrast i.distNum#i.wordtypeN
contrast i.distNum@i.wordtypeN



* Is the Boundary Effect Influenced by Word and Orthographic Properties?

* Word properties
mixed keytime i.distNum##i.wordtypeN##c.stim_lg10wf ///
	i.distNum##i.wordtypeN##c.bgJonesMewhortboundary ///
	|| _all:R.subject || item: if keytime > 50 & keytime < 1000 & inrange(dist,-1,0) , mle nogroup nolrtest nofvlabel noretable
contrast i.distNum#c.stim_lg10wf#i.wordtypeN
contrast i.distNum#c.stim_lg10wf@i.wordtypeN


contrast i.distNum#i.wordtypeN#c.bgJonesMewhortboundary
contrast i.distNum#c.bgJonesMewhortboundary@wordtypeN
margins i.distNum#2.wordtypeN, dydx(c.bgJonesMewhortboundary)
margins i.distNum#1.wordtypeN, dydx(c.bgJonesMewhortboundary)

quietly margins distNum#wordtypeN, at(stim_lg10wf=(0.3/4))
marginsplot,  by( wordtypeN ) xtitle(Word frequency) ytitle(Predicted latencies (ms)) byopts(title(" ")) subtitle(, bcolor(none) ) legend(order(3 "end of C1" 4 "start of C2" ))ylabel(150(50)350)   name(E4_b_wfreq, replace) aspectratio(.75)

quietly margins distNum#wordtypeN, at(bgJonesMewhortboundary=(3(2)14))
marginsplot,  by( wordtypeN ) xtitle(Bigram frequency) ytitle(Predicted latencies (ms)) byopts(title(" ")) subtitle(, bcolor(none) ) legend(order(3 "end of C1" 4 "start of C2" ))ylabel(150(50)350)  name(E4_b_bgfreq, replace) aspectratio(.75)

* combined and save as stata & png formats
grc1leg2 E4_b_wfreq E4_b_bgfreq, cols(1) ///
	imargin(0 0 0 0) ycommon   xsize(5) ysize(5)  pos(5)
graph save  "output/figures/fig_E4_b_combined_wfreq_bgfreq", replace
graph export  "output/figures/fig_E4_b_combined_wfreq_bgfreq.png", replace

*Do properties of the (pseudo) constituent influence processing at the boundary region, and if so does morphological structure influence this?

* C1 properties
mixed keytime c.stim_lg10wf i.distNum##i.wordtypeN##c.bgJonesMewhortboundary ///
	i.distNum##i.wordtypeN##c.c1_lg10wf i.distNum##i.wordtypeN##c.c1len || _all:R.subject || item: if keytime > 50 & keytime < 1000 & inrange(dist,-1,0)  , mle nogroup nolrtest nofvlabel noretable
contrast i.distNum#i.wordtypeN#c.c1len

* length
* do 0 and -1 slopes differ for p and c.
contrast r.distNum#c.c1len@wordtypeN
* do slope differs from 0 for pos 0 and -1 for c
margins distNum, dydx(c1len) subpop(if wordtypeN==1)
* do slope differs from 0 for pos 0 and -1 for p
margins distNum, dydx(c1len) subpop(if wordtypeN==2)

*frequency
contrast i.distNum#i.wordtypeN#c.c1_lg10wf
contrast r.distNum#c.c1_lg10wf@wordtypeN
* do slope differs from 0 for pos 0 and -1 for c
margins distNum, dydx(c1_lg10wf) subpop(if wordtypeN==1)
* do slope differs from 0 for pos 0 and -1 for p
margins distNum, dydx(c1_lg10wf) subpop(if wordtypeN==2)


quietly margins distNum#wordtypeN, at(c1len=(3/6))
marginsplot,  ytitle ("Predicted latencies (ms)") xtitle("Length of first constituent") title("") by(wordtypeN) legend( order(3 "end of C1" 4 "start of C2") rows(2) ring(0) pos(11)) byopts(title(" ")) subtitle(, bcolor(none) ) ylabel(150(50)350) name(E4_b_c1len, replace) aspectratio(.75)

quietly margins distNum#wordtypeN, at(c1_lg10wf=(0.3/5))
marginsplot,  by( wordtypeN ) xtitle(Frequency of the first constituent) ytitle(Predicted latencies (ms)) byopts(title(" ")) subtitle(, bcolor(none) ) legend(order(3 "end of C1" 4 "start of C2" ))ylabel(150(50)350) name(E4_b_c1freq, replace) aspectratio(.75)


* combined and save as stata & png formats
grc1leg2 E4_b_c1len E4_b_c1freq, cols(1) ///
	imargin(0 0 0 0) ycommon legendfrom(E4_b_c1len)  xsize(5) ysize(5)  pos(5)
graph save  "output/figures/fig_E4_b_c1", replace
graph export  "output/figures/fig_E4_b_c1.png", replace



* C2 properties
mixed keytime c.stim_lg10wf i.distNum##i.wordtypeN##c.bgJonesMewhortboundary ///
	i.distNum##i.wordtypeN##c.c2_lg10wf i.distNum##i.wordtypeN##c.c2len || _all:R.subject || item: if keytime > 50 & keytime < 1000 & inrange(dist,-1,0) , mle nogroup nolrtest nofvlabel noretable

* length
contrast i.distNum#i.wordtypeN#c.c2len
* do 0 and -1 slopes differ for p and c
contrast r.distNum#c.c2len@wordtypeN
* do slopes differ from 0 for p
margins distNum, dydx(c2len) subpop(if wordtypeN==2)
* do slopes differ from 0 for c
margins , dydx(c2len) subpop(if wordtypeN==1)

*frequency
contrast i.distNum#i.wordtypeN#c.c2_lg10wf
contrast i.wordtypeN#c.c2_lg10wf
contrast i.distNum#c.c2_lg10wf
margins i.distNum, dydx(c2_lg10wf)


quietly margins distNum#wordtypeN, at(c2len=(3/8))
marginsplot,  by( wordtypeN ) xtitle(Length of the second constituent) ytitle(Predicted latencies (ms)) byopts(title(" ")) subtitle(, bcolor(none) ) legend(order(3 "end of C1" 4 "start of C2" )) name(E4_b_c2len, replace) aspectratio(.75) ylabel(150(50)350)

quietly margins distNum, at(c2_lg10wf=(0.3/5))

* without  confidence interval, then the keys are 3 and 4, because 1 and 2 are the lines for the ci
marginsplot,  ytitle ("Predicted latencies (ms)") xtitle("Frequency of second constituent") title("") legend( order(3 "end of C1" 4 "start of C2")  rows(2) ring(0) pos(11)) name(E4_b_c2freq, replace)  aspectratio(.75) ylabel(150(50)350)

* combined and save as stata & png formats
grc1leg2 E4_b_c2len E4_b_c2freq, cols(1) ///
	imargin(0 0 0 0) ycommon legendfrom(E4_b_c2len)  xsize(5) ysize(5)  pos(5)
graph save  "output/figures/fig_E4_b_c2", replace
graph export  "output/figures/fig_E4_b_c2.png", replace


//--------------------
//------------ syllable
cd "/Users/christinagagne/Dropbox/Analyses/2023_JEPLMC__analysis"
use data/Expt4_2023_forAnalysis_withSyll_pseudos, clear

preserve
drop if firstsyll == 0 & firstsyll_pre == 0
*+M+S items are the same
gen distsyll = -1 if dist == -1 & firstsyll == 1
replace distsyll = 0 if dist == 0 & firstsyll == 1
*-M+S items shift up one position
replace distsyll = -1 if dist == -2 & firstsyll == 0
replace distsyll = 0 if dist == -1 & firstsyll == 0
egen distsyllNum = group(distsyll), label(distsyllNum, replace)

*re-label to make it easier to convey what's being measured
gen boundarytype = firstsyll
label variable boundarytype "boundarytype"
label define cmpsetlabel 1 "+S+M", modify
label define boundarylabel 1 "+C1+S" 0 "-C1+S"
label values boundarytype boundarylabel

table boundarytype

codebook distsyllNum
mixed keytime i.boundarytype##i.distsyllNum || _all:R.subject || item: if keytime > 50 & keytime < 1000 & inrange(distsyllNum,1,2), mle nogroup nolrtest nofvlabel noretable
contrast boundarytype##distsyllNum
contrast distsyllNum@boundarytype

quietly margins distsyllNum#boundarytype
marginsplot, noci ytitle ("Predicted latencies (ms)") xtitle("Position relative to the start of the first letter of the second syllable") title("") legend(rows(2) ring(0) pos(11)) byopts(title(" ")) subtitle(, bcolor(none) )

graph save  "output/figures/fig_E4_b_firstsyll", replace
graph export  "output/figures/fig_E4_b_firstsyll.png", replace
restore


*whether the PseudoC1 corresponds to any syllable (ie.not only the first one)
mixed keytime i.atSyll##i.distNum##atSyll_pre || _all:R.subject || item: if keytime > 50 & keytime < 1000 & inrange(dist,-1,0)  , mle nogroup nolrtest nofvlabel noretable

contrast  i.atSyll##i.distNum##atSyll_pre /*no interaction */
contrast  i.atSyll#i.distNum
contrast  i.distNum@atSyll
margins  i.distNum#atSyll#atSyll_pre

marginsplot,  by(atSyll_pre, label("no preceding syllable boundary" "preceding syllable boundary")) ytitle ("Predicted latencies (ms)") xtitle("Position relative to pseudo-morpheme boundary") title("") legend(rows(2) ring(0) pos(11)) byopts(title(" ")) subtitle(, bcolor(none) )   legend( order(3 "not at syllable boundary" 4 "at syllable boundary"))


graph save  "output/figures/fig_E4_b_atsyll", replace
graph export  "output/figures/fig_E4_b_atsyll.png", replace