When we speak sets of words, the more words we speak, the longer it takes. The spokenduration data set describes data from speeded spoken duration of words. Each column decribes whether a particular word was on the list they spoke, but we will ignore those columns for the moment. The last column tells how long it took to speak the word.

subject cult    dare    fate    guess   hint    mood    oath    plea    rush    verb    zeal    time
s02 1   1   0   1   0   0   0   1   1   0   1   4.886719
s02 1   1   0   0   0   1   1   0   0   0   0   2.738281
s02 0   0   1   0   1   1   1   1   0   1   0   4.179688
s02 1   0   0   1   0   0   1   1   0   0   1   3.628906

Read in the data, and compute a list-length using the following functions:

data <- read.csv("spokenduration.csv")
data$length <- rowSums(data[,2:12])   #numerical list-length
data$lengthC <- as.factor(rowSums(data[,2:12]))  #categorical listlength

1. Categorical effect of length

First, we’d like to determine whether there is a significant relationship between list length and spoken duration. * Calculate the length of each list (the sum of the middle 11 columns). * Create a regression predicting time-to-speak using list length. * Recode the list length as a factor, and create as model predicting total time. * Use an anova() to compare these two models. Is there any advantage or benefit to treating list length as a category rather than a linear fator? * Using just the second model with the categorical predictor, compute the effect size (e.g. use sjstats::anova_stats).
* Also, compute a post-hoc Tukey HSD test and describe which differences are significant at a p=.05 level.

summary(lm(time~length,data=data))
## 
## Call:
## lm(formula = time ~ length, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.0580 -0.5033  0.2740  0.9030  5.2623 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.1901     0.4741   0.401    0.689    
## length        0.8478     0.1021   8.305 1.11e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.69 on 215 degrees of freedom
## Multiple R-squared:  0.2429, Adjusted R-squared:  0.2394 
## F-statistic: 68.98 on 1 and 215 DF,  p-value: 1.11e-14
summary(lm(time~lengthC,data=data))
## 
## Call:
## lm(formula = time ~ lengthC, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1680 -0.5068  0.1939  0.8710  5.1523 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.8384     0.2285  12.421  < 2e-16 ***
## lengthC4      0.6371     0.3278   1.944   0.0533 .  
## lengthC5      1.4755     0.3232   4.566 8.42e-06 ***
## lengthC6      2.5484     0.3232   7.885 1.61e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.695 on 213 degrees of freedom
## Multiple R-squared:  0.2461, Adjusted R-squared:  0.2355 
## F-statistic: 23.17 on 3 and 213 DF,  p-value: 5.063e-13
c = (lm(time~length,data=data))
c1 = (lm(time~lengthC,data=data))

anova(c,c1)
## Analysis of Variance Table
## 
## Model 1: time ~ length
## Model 2: time ~ lengthC
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1    215 614.35                           
## 2    213 611.77  2    2.5812 0.4494 0.6386
library(sjstats)
## Warning: package 'sjstats' was built under R version 4.0.3
effectsize::cohens_f(c1)
## Parameter | Cohen's f (partial) |       90% CI
## ----------------------------------------------
## lengthC   |                0.57 | [0.44, 0.68]
TukeyHSD(aov(c1))
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = c1)
## 
## $lengthC
##          diff         lwr      upr     p adj
## 4-3 0.6370875 -0.21173713 1.485912 0.2131346
## 5-3 1.4754972  0.63865652 2.312338 0.0000496
## 6-3 2.5483665  1.71152584 3.385207 0.0000000
## 5-4 0.8384096 -0.01041502 1.687234 0.0542585
## 6-4 1.9112790  1.06245431 2.760104 0.0000001
## 6-5 1.0728693  0.23602868 1.909710 0.0058012

There is no difference in treating it as a factor or a linear predictor. The Cohen’s F effect size is 0.57 so this categorical predictor can predict 57% of variance. The Tukey Post-hoc test found significant differences between lengths 5-3, 6-3, 5-4 (p = 0.054),6-4,6-5.

2. Estimating spoken time per word

Now, make a regression model predicting total time by the set of 11 word indicator variables. Depending on the contrasts you created, the coefficients will tell you the mean contribution to the whole that each word produced. Which words appeared ‘easy’ to say? Which words were hard? make a bargraph showing mean estimated duration per word.

#data[,2:12] = data.frame(apply(data[2:12], 2, as.factor)) #not working 

for(i in 2:12){
  data[,i] = as.factor(data[,i])
}

model = lm(time~cult+dare+fate+guess+hint+mood+oath+plea+rush+verb+zeal,data=data)
summary(model)
## 
## Call:
## lm(formula = time ~ cult + dare + fate + guess + hint + mood + 
##     oath + plea + rush + verb + zeal, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9616 -0.5092  0.2334  0.9124  5.2629 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.1605     0.4983   0.322 0.747714    
## cult1         0.9297     0.2502   3.717 0.000260 ***
## dare1         0.5937     0.2572   2.309 0.021955 *  
## fate1         0.8099     0.2479   3.267 0.001273 ** 
## guess1        0.8730     0.2578   3.386 0.000850 ***
## hint1         0.8384     0.2502   3.351 0.000960 ***
## mood1         0.8236     0.2573   3.200 0.001591 ** 
## oath1         0.8716     0.2454   3.552 0.000475 ***
## plea1         0.7486     0.2441   3.066 0.002458 ** 
## rush1         0.9605     0.2529   3.798 0.000192 ***
## verb1         1.0669     0.2559   4.170 4.49e-05 ***
## zeal1         0.9006     0.2551   3.531 0.000512 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.719 on 205 degrees of freedom
## Multiple R-squared:  0.2537, Adjusted R-squared:  0.2136 
## F-statistic: 6.334 on 11 and 205 DF,  p-value: 5.416e-09
d = c(0.7,1.9,3.1,4.3,5.5,6.7,7.9,9.1,10.3,11.5,12.7)
names = colnames(data[,2:12])

barplot(model$coefficients[2:12]+model$coefficients[1],ylim=c(0,1.5),ylab="Time (sec)",xlab="Word",col=rainbow(11),
        xaxt="n", main = 'Estimated Time depending on word')
axis(1,d,labels=names)

As you can see from the barplot, the two easiest words to say are dare and plea which take 0.76 and 0.90 seconds to say respectively. The two most difficult ones are verb and cult which take 1.16 and 1.08 s to say respectively. (Calculations = coef + intercept)

3. Tricky words and ANCOVA

Now, suppose that the number of words in the list is the main determiner of how long it takes to say them, EXCEPT when the word VERB is in the list. That is, adding ‘verb’ creates some sort of tongue-twister, so we suspect its present interacts with length, when it is a list with a lot of other words, people get tongue-tied and must slow down.

Make a model predicting speaking time by whether ‘verb’ was in the list, along with length. This will tell us the extra time involved to say the list when verb was on it (in comparison to other lists of the same length. Provide an interpretation for each coefficient of the model. Here, length is just a standard covariate (ANCOVA), so describe why adding length to the model tells you something that just using ‘verb’ as the predictor won’t.

Then, include the verb* length interaction (be sure to code the contrasts of length with an orthogonal contrast like contr.poly). Compare the interaction model to the standard ANCOVA model. Interpret the results. Regardless of the results, if there were a significant interaction between verb and length, how would you interpret this?

library(car)
## Warning: package 'car' was built under R version 4.0.2
## Loading required package: carData
## Registered S3 methods overwritten by 'car':
##   method                          from
##   influence.merMod                lme4
##   cooks.distance.influence.merMod lme4
##   dfbeta.influence.merMod         lme4
##   dfbetas.influence.merMod        lme4
model2 = lm(time~verb+lengthC,data=data)
model3 = lm(time~lengthC,data=data)
#modell = lm(time~length+0,data=data) shouldn't we ignore the intercept? how can time be 0.1901 
#when length = 0 even though the intercept is not significant

summary(model2)
## 
## Call:
## lm(formula = time ~ verb + lengthC, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.2790 -0.4991  0.2590  0.9049  5.2955 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.7645     0.2394  11.550  < 2e-16 ***
## verb1         0.2542     0.2452   1.036    0.301    
## lengthC4      0.6524     0.3281   1.988    0.048 *  
## lengthC5      1.4339     0.3256   4.404 1.69e-05 ***
## lengthC6      2.4790     0.3300   7.513 1.59e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.694 on 212 degrees of freedom
## Multiple R-squared:  0.2499, Adjusted R-squared:  0.2357 
## F-statistic: 17.66 on 4 and 212 DF,  p-value: 1.596e-12
summary(model3)
## 
## Call:
## lm(formula = time ~ lengthC, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1680 -0.5068  0.1939  0.8710  5.1523 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.8384     0.2285  12.421  < 2e-16 ***
## lengthC4      0.6371     0.3278   1.944   0.0533 .  
## lengthC5      1.4755     0.3232   4.566 8.42e-06 ***
## lengthC6      2.5484     0.3232   7.885 1.61e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.695 on 213 degrees of freedom
## Multiple R-squared:  0.2461, Adjusted R-squared:  0.2355 
## F-statistic: 23.17 on 3 and 213 DF,  p-value: 5.063e-13
Anova(model2)
## Anova Table (Type II tests)
## 
## Response: time
##           Sum Sq  Df F value    Pr(>F)    
## verb        3.08   1  1.0742    0.3012    
## lengthC   175.93   3 20.4246 1.151e-11 ***
## Residuals 608.69 212                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
contrasts(data$lengthC)=contr.poly(4)
contrasts(data$lengthC)
##           .L   .Q         .C
## 3 -0.6708204  0.5 -0.2236068
## 4 -0.2236068 -0.5  0.6708204
## 5  0.2236068 -0.5 -0.6708204
## 6  0.6708204  0.5  0.2236068
model4 = lm(time~verb*lengthC,data=data)

summary(model4)
## 
## Call:
## lm(formula = time ~ verb * lengthC, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7985 -0.5130  0.2498  0.8581  5.9666 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      3.84985    0.14774  26.058  < 2e-16 ***
## verb1            0.18059    0.24437   0.739   0.4607    
## lengthC.L        1.31503    0.30381   4.329 2.33e-05 ***
## lengthC.Q       -0.25648    0.29548  -0.868   0.3864    
## lengthC.C       -0.14013    0.28691  -0.488   0.6258    
## verb1:lengthC.L  1.12915    0.47746   2.365   0.0189 *  
## verb1:lengthC.Q  0.97237    0.48873   1.990   0.0479 *  
## verb1:lengthC.C  0.09211    0.49976   0.184   0.8540    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.667 on 209 degrees of freedom
## Multiple R-squared:  0.2841, Adjusted R-squared:  0.2601 
## F-statistic: 11.85 on 7 and 209 DF,  p-value: 1.09e-12
Anova(model4,type = 'III')
## Anova Table (Type III tests)
## 
## Response: time
##               Sum Sq  Df  F value    Pr(>F)    
## (Intercept)  1887.45   1 679.0337 < 2.2e-16 ***
## verb            1.52   1   0.5462 0.4607183    
## lengthC        58.75   3   7.0450 0.0001557 ***
## verb:lengthC   27.75   3   3.3276 0.0205789 *  
## Residuals     580.94 209                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(model2,model4)
## Analysis of Variance Table
## 
## Model 1: time ~ verb + lengthC
## Model 2: time ~ verb * lengthC
##   Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
## 1    212 608.69                              
## 2    209 580.94  3    27.749 3.3276 0.02058 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The predictor verb alone does not tell us much and it is not significant when the covariable length is included either (F = 1.07, p = 0.26).We can’t conclude that the word “verb” is a tongue twister. On the other part, length is a very strong predictor which also logically makes sense since the more words we pronounce the longer it takes for us to finish a phrase. There seems to be a significant interaction between verb and length (p = 0.0205789). This means that predicted time varies not only on the two main effects which are the inclusion or not of the word “verb” and the length of the phrase but also depending on whether the word “verb” is included or not across the different levels of the other categorical variable “length” (thus the result of predicted time is not only explained by the sums of the two variables). This could make sense if we think that the word verb could be pronounced faster or slower in the presence of more or less words. Furthermore, the inclusion of this interaction resulted in a better model (F = 3.32, p = 0.02).

4. Subject effects

Next, we might expect that different people speak more quickly or more slowly. We’d like to incorporate an overall speed-by-subject factor. To do so, reset the contrasts of subject to use sum-to-zero coding (so that they will all be coded with respect to the mean), and add subject to the model. Use a type-II ANOVA to test whether subject accounts for a significant proportion of variance along with length as a categorical predictor. Compute the effect sizes (eta^2 and omega^2) . Then do a post-hoc Tukey test to determine whether any individual participants were significantly faster or slower than you’d expect. Describe your findings in words.

data[,1] = as.factor(data[,1])
contrasts(data$lengthC)=contr.poly(4)
contrasts(data$subject) = contr.sum(8) #the intercept becomes the mean of all means and the levels of the subject the difference between of that level and the intercept In other words, if one level is not significant it means it is not significantly different from the mean of all means.


model5 = lm(time~subject+verb*lengthC,data=data)

contrasts(data$subject)
##     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## s01    1    0    0    0    0    0    0
## s02    0    1    0    0    0    0    0
## s03    0    0    1    0    0    0    0
## s04    0    0    0    1    0    0    0
## s05    0    0    0    0    1    0    0
## s06    0    0    0    0    0    1    0
## s07    0    0    0    0    0    0    1
## s08   -1   -1   -1   -1   -1   -1   -1
summary(model5)
## 
## Call:
## lm(formula = time ~ subject + verb * lengthC, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.5339 -0.5361 -0.0807  0.3785  6.1966 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      3.911401   0.092686  42.201  < 2e-16 ***
## subject1         0.900158   0.191341   4.704 4.71e-06 ***
## subject2        -0.531780   0.190776  -2.787  0.00582 ** 
## subject3        -2.948636   0.186985 -15.769  < 2e-16 ***
## subject4        -0.388347   0.186270  -2.085  0.03834 *  
## subject5         0.133196   0.192634   0.691  0.49008    
## subject6         0.236561   0.188772   1.253  0.21160    
## subject7         1.264518   0.188892   6.694 2.09e-10 ***
## verb1            0.205161   0.153250   1.339  0.18216    
## lengthC.L        1.632565   0.192055   8.500 4.16e-15 ***
## lengthC.Q        0.003051   0.185815   0.016  0.98692    
## lengthC.C       -0.098069   0.180738  -0.543  0.58800    
## verb1:lengthC.L  0.422643   0.304918   1.386  0.16725    
## verb1:lengthC.Q  0.356660   0.309471   1.152  0.25048    
## verb1:lengthC.C  0.215075   0.318629   0.675  0.50045    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.044 on 202 degrees of freedom
## Multiple R-squared:  0.7285, Adjusted R-squared:  0.7097 
## F-statistic: 38.72 on 14 and 202 DF,  p-value: < 2.2e-16
model6 = lm(time~subject+lengthC,data=data)
Anova(model6)
## Anova Table (Type II tests)
## 
## Response: time
##           Sum Sq  Df F value    Pr(>F)    
## subject   384.68   7  49.849 < 2.2e-16 ***
## lengthC   197.26   3  59.644 < 2.2e-16 ***
## Residuals 227.10 206                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
omega_sq(model6)
##      term omegasq
## 1 subject   0.467
## 2 lengthC   0.239
eta_sq(model6)
##      term etasq
## 1 subject 0.477
## 2 lengthC 0.243
#anova_stats(model6)
TukeyHSD(aov(model6))
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = model6)
## 
## $subject
##                diff        lwr         upr     p adj
## s02-s01 -1.49484178 -2.3783230 -0.61136060 0.0000143
## s03-s01 -3.92727305 -4.8029806 -3.05156546 0.0000000
## s04-s01 -1.31775822 -2.1934658 -0.44205063 0.0001895
## s05-s01 -0.81820923 -1.7099862  0.07356773 0.0982130
## s06-s01 -0.65195982 -1.5354410  0.23152136 0.3210780
## s07-s01  0.38160507 -0.5018761  1.26508625 0.8893276
## s08-s01  0.42498710 -0.4507205  1.30069469 0.8140902
## s03-s02 -2.43243127 -3.2996894 -1.56517316 0.0000000
## s04-s02  0.17708356 -0.6901746  1.04434167 0.9984995
## s05-s02  0.67663255 -0.2068486  1.56011373 0.2744548
## s06-s02  0.84288196 -0.0322248  1.71798872 0.0682314
## s07-s02  1.87644685  1.0013401  2.75155361 0.0000000
## s08-s02  1.91982888  1.0525708  2.78708699 0.0000000
## s04-s03  2.60951483  1.7501770  3.46885261 0.0000000
## s05-s03  3.10906382  2.2333562  3.98477141 0.0000000
## s06-s03  3.27531323  2.4080551  4.14257134 0.0000000
## s07-s03  4.30887812  3.4416200  5.17613623 0.0000000
## s08-s03  4.35226015  3.4929224  5.21159793 0.0000000
## s05-s04  0.49954899 -0.3761586  1.37525658 0.6564526
## s06-s04  0.66579840 -0.2014597  1.53305651 0.2715421
## s07-s04  1.69936329  0.8321052  2.56662140 0.0000002
## s08-s04  1.74274532  0.8834075  2.60208310 0.0000001
## s06-s05  0.16624941 -0.7172318  1.04973059 0.9991154
## s07-s05  1.19981430  0.3163331  2.08329548 0.0012014
## s08-s05  1.24319633  0.3674887  2.11890392 0.0005646
## s07-s06  1.03356489  0.1584581  1.90867165 0.0088160
## s08-s06  1.07694692  0.2096888  1.94420503 0.0045894
## s08-s07  0.04338203 -0.8238761  0.91064014 0.9999999
## 
## $lengthC
##          diff       lwr      upr     p adj
## 4-3 0.6417676 0.1157485 1.167787 0.0097436
## 5-3 1.4754972 0.9569045 1.994090 0.0000000
## 6-3 2.5334900 2.0148973 3.052083 0.0000000
## 5-4 0.8337295 0.3077104 1.359749 0.0003373
## 6-4 1.8917223 1.3657032 2.417741 0.0000000
## 6-5 1.0579928 0.5394002 1.576585 0.0000019

The model shows that the variable subject was a significant predictor and accounts for 47% of the total variance, almost twice of the total variance explained by the variable length. Subject number 1 was significantly slower than number 2, 3 and 4. Subject number 2 was significantly slower than subject 3 and significantly faster than all others but similar to subjects 4,5,6. Subject 3 was significantly faster than anyone else (average of 1.02 s). Subject 4 was significantly faster than subjects 1,7, and 8 but significantly slower than subject 3. The 2 slowest ones where number 7 and 8.

5. Subject x length interactions

Not everyone pronounces words the same. It may be true that individuals interact with word-length. Add the subject by length interaction to the subject + length model. Do this in an lm and examine the coefficients. Describe the effects–which people are slower, or which length:subject interaction terms are significantly different than the baseline? Perform a post-hoc tukey test and determine which participants have significantly different durations for 6-item lists (e.g., the result for 6:s06-6:s01 shows whether participants s06 and s01 differed in how they pronounced 6-item lists).

Hint: create the entire set of tukey tests using TukeyHSD(model,which=“Length:subject”), then filter this matrix based on the rownames() and substr(). This will depend on what you named your variables, but substr(rownames(tukey$"length:subject"),1,1) and substr(rownames(tukey$"length:subject"),7,7) will extract the two length codes from the tukey table row names, which are formatted like: 6:s08-6:s03

model6 = lm(time~subject+lengthC+subject:lengthC,data=data)

summary(model6)
## 
## Call:
## lm(formula = time ~ subject + lengthC + subject:lengthC, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.9877 -0.5156 -0.0670  0.2712  7.4353 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         4.014590   0.065461  61.328  < 2e-16 ***
## subject1            0.908959   0.176519   5.149 6.65e-07 ***
## subject2           -0.580741   0.173566  -3.346 0.000993 ***
## subject3           -2.991292   0.170563 -17.538  < 2e-16 ***
## subject4           -0.381778   0.170563  -2.238 0.026391 *  
## subject5            0.138102   0.176519   0.782 0.435000    
## subject6            0.258987   0.173566   1.492 0.137363    
## subject7            1.286796   0.173566   7.414 4.27e-12 ***
## lengthC.L           1.887847   0.130130  14.507  < 2e-16 ***
## lengthC.Q           0.212600   0.130922   1.624 0.106107    
## lengthC.C          -0.003364   0.131709  -0.026 0.979652    
## subject1:lengthC.L -0.150485   0.352744  -0.427 0.670157    
## subject2:lengthC.L -0.142645   0.342033  -0.417 0.677126    
## subject3:lengthC.L -1.602723   0.340822  -4.703 5.01e-06 ***
## subject4:lengthC.L -1.209540   0.340822  -3.549 0.000490 ***
## subject5:lengthC.L  0.085121   0.352744   0.241 0.809581    
## subject6:lengthC.L  0.867932   0.342033   2.538 0.011988 *  
## subject7:lengthC.L  1.041959   0.342033   3.046 0.002655 ** 
## subject1:lengthC.Q -0.206089   0.353037  -0.584 0.560093    
## subject2:lengthC.Q  0.187000   0.347132   0.539 0.590742    
## subject3:lengthC.Q  0.241362   0.341125   0.708 0.480117    
## subject4:lengthC.Q -0.448091   0.341125  -1.314 0.190619    
## subject5:lengthC.Q -0.242037   0.353037  -0.686 0.493834    
## subject6:lengthC.Q -0.038772   0.347132  -0.112 0.911189    
## subject7:lengthC.Q  0.243780   0.347132   0.702 0.483394    
## subject1:lengthC.C -0.079823   0.353330  -0.226 0.821515    
## subject2:lengthC.C  0.002303   0.352158   0.007 0.994789    
## subject3:lengthC.C  0.327169   0.341428   0.958 0.339193    
## subject4:lengthC.C -0.132397   0.341428  -0.388 0.698628    
## subject5:lengthC.C -0.392128   0.353330  -1.110 0.268523    
## subject6:lengthC.C  0.043918   0.352158   0.125 0.900888    
## subject7:lengthC.C  0.080853   0.352158   0.230 0.818662    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9623 on 185 degrees of freedom
## Multiple R-squared:  0.7889, Adjusted R-squared:  0.7535 
## F-statistic:  22.3 on 31 and 185 DF,  p-value: < 2.2e-16
t = TukeyHSD(aov(model6),which='subject:lengthC')
filter = as.numeric(substr(rownames(t$`subject:lengthC`),5,5))== 6 & as.numeric(substr(rownames(t$`subject:lengthC`),11,11)) == 6 #filter out 5th and 11th number which has to be equal to 6

only_length_6 = t$`subject:lengthC`[,4][filter]
diff = t$`subject:lengthC`[,2][filter]

significant_differences = only_length_6[ only_length_6< 0.05]
diff = diff[ only_length_6< 0.05]

students = names(as.list(significant_differences))
p_values = as.numeric(substr(significant_differences,1,20))
diff = as.numeric(substr(diff,1,20))

data.frame(students=students,p_values = p_values,time_diff = diff)
##       students     p_values   time_diff
## 1  s03:6-s01:6 6.958878e-13 -6.53612514
## 2  s04:6-s01:6 1.771188e-02 -4.11034367
## 3  s03:6-s02:6 6.161695e-07 -5.26659385
## 4  s07:6-s02:6 1.752815e-04  0.73173219
## 5  s08:6-s02:6 4.631379e-05  0.87682119
## 6  s04:6-s03:6 1.958367e-03  0.44936638
## 7  s05:6-s03:6 6.903430e-09  1.80197799
## 8  s06:6-s03:6 4.405365e-13  2.72782609
## 9  s07:6-s03:6 1.436629e-13  4.02191095
## 10 s08:6-s03:6 1.139089e-13  4.16699995
## 11 s06:6-s04:6 6.203691e-03  0.30204462
## 12 s07:6-s04:6 3.057381e-08  1.59612948
## 13 s08:6-s04:6 6.180777e-09  1.74121848
## 14 s07:6-s05:6 3.017340e-02  0.08211195
## 15 s08:6-s05:6 1.151516e-02  0.22720095

There seems to be an interaction between subjects 3,4,6,7 and length at level 4 (which is the 2nd one). For the 6-item list only the table shows the significantly different durations between all subjects. Subject 3 was insanely fast with an average of 6.5 seconds faster than subject 1, 5.2 faster than subject 2, 0.4 seconds faster than subject 4, 1.8 seconds faster than subject 5, and 2.7,4.02 and 4.16 faster than subject 6,7 and 8. Subject 4 was faster than subject 1 by 4.11 seconds. Subjects 7 and 8 were slower than subject 2 by 0.73 and 0.87 seconds respectively.

6. ANCOVA

Finally, run the interaction model again, but use length as a continuous predictor instead of a categorical run both the regresion and anova models. Look at and interpret the sets of coefficients in the regression model, and interpret the results of a Type-II ANOVA. Run a post-hoc test on subject, and compute eta^2 and omega. describe in words how you would interpret each part of this.

model7 = lm(time~subject+length+subject:length,data=data)
summary(model7)
## 
## Call:
## lm(formula = time ~ subject + length + subject:length, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.8926 -0.5372 -0.0917  0.2532  7.7347 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      0.22151    0.26530   0.835 0.404741    
## subject1         1.20149    0.72205   1.664 0.097669 .  
## subject2        -0.27886    0.69930  -0.399 0.690481    
## subject3         0.22799    0.69428   0.328 0.742966    
## subject4         2.04624    0.69428   2.947 0.003585 ** 
## subject5        -0.03165    0.71208  -0.044 0.964591    
## subject6        -1.48759    0.69930  -2.127 0.034617 *  
## subject7        -0.79784    0.69930  -1.141 0.255265    
## length           0.84366    0.05712  14.771  < 2e-16 ***
## subject1:length -0.06640    0.15461  -0.429 0.668059    
## subject2:length -0.06617    0.15017  -0.441 0.659937    
## subject3:length -0.71615    0.14969  -4.784 3.32e-06 ***
## subject4:length -0.54031    0.14969  -3.609 0.000387 ***
## subject5:length  0.04001    0.15461   0.259 0.796095    
## subject6:length  0.38787    0.15017   2.583 0.010508 *  
## subject7:length  0.46396    0.15017   3.090 0.002288 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9452 on 201 degrees of freedom
## Multiple R-squared:  0.7787, Adjusted R-squared:  0.7622 
## F-statistic: 47.15 on 15 and 201 DF,  p-value: < 2.2e-16
Anova(model7)
## Anova Table (Type II tests)
## 
## Response: time
##                Sum Sq  Df F value    Pr(>F)    
## subject        384.89   7  61.541 < 2.2e-16 ***
## length         194.89   1 218.133 < 2.2e-16 ***
## subject:length  49.87   7   7.974 1.542e-08 ***
## Residuals      179.59 201                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(model7, type = 'III')
## Anova Table (Type III tests)
## 
## Response: time
##                 Sum Sq  Df  F value    Pr(>F)    
## (Intercept)      0.623   1   0.6971   0.40474    
## subject         14.992   7   2.3971   0.02239 *  
## length         194.936   1 218.1788 < 2.2e-16 ***
## subject:length  49.872   7   7.9740 1.542e-08 ***
## Residuals      179.587 201                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(aov(model7),which='subject')
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: length
## Warning in replications(paste("~", xx), data = mf): non-factors ignored:
## subject, length
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = model7)
## 
## $subject
##                diff         lwr         upr     p adj
## s02-s01 -1.49484178 -2.29040884 -0.69927472 0.0000009
## s03-s01 -3.92727305 -4.71584006 -3.13870603 0.0000000
## s04-s01 -1.31775822 -2.10632523 -0.52919121 0.0000196
## s05-s01 -0.81820923 -1.62124657 -0.01517189 0.0423873
## s06-s01 -0.65195982 -1.44752688  0.14360725 0.1971573
## s07-s01  0.38160507 -0.41396199  1.17717213 0.8228273
## s08-s01  0.42498710 -0.36357991  1.21355412 0.7186592
## s03-s02 -2.43243127 -3.21338960 -1.65147294 0.0000000
## s04-s02  0.17708356 -0.60387477  0.95804189 0.9970688
## s05-s02  0.67663255 -0.11893451  1.47219961 0.1601631
## s06-s02  0.84288196  0.05485599  1.63090794 0.0267010
## s07-s02  1.87644685  1.08842088  2.66447282 0.0000000
## s08-s02  1.91982888  1.13887055  2.70078721 0.0000000
## s04-s03  2.60951483  1.83568869  3.38334097 0.0000000
## s05-s03  3.10906382  2.32049680  3.89763083 0.0000000
## s06-s03  3.27531323  2.49435490  4.05627156 0.0000000
## s07-s03  4.30887812  3.52791979  5.08983645 0.0000000
## s08-s03  4.35226015  3.57843401  5.12608629 0.0000000
## s05-s04  0.49954899 -0.28901802  1.28811600 0.5248640
## s06-s04  0.66579840 -0.11515993  1.44675673 0.1579254
## s07-s04  1.69936329  0.91840496  2.48032162 0.0000000
## s08-s04  1.74274532  0.96891918  2.51657146 0.0000000
## s06-s05  0.16624941 -0.62931765  0.96181648 0.9982550
## s07-s05  1.19981430  0.40424724  1.99538136 0.0001827
## s08-s05  1.24319633  0.45462932  2.03176335 0.0000732
## s07-s06  1.03356489  0.24553892  1.82159086 0.0020881
## s08-s06  1.07694692  0.29598859  1.85790525 0.0009368
## s08-s07  0.04338203 -0.73757630  0.82434036 0.9999998
eta_sq(model7)
##             term etasq
## 1        subject 0.477
## 2         length 0.240
## 3 subject:length 0.061
omega_sq(model7)
##             term omegasq
## 1        subject   0.469
## 2         length   0.239
## 3 subject:length   0.054

In this model only subject 4 and 6 are significant predictors for time. Moreover, other significant predictors are the interaction between subject 3,4,6,7 and length as a continuous variable. Length appear to also be a significant predictor. The Type-II ANOVA tells us that there are significant effects for each main effect after the inclusion of the other main effects. However, a type III Anova could have been more exact since now the main effects are evaluated after the effect of the other main effects and the interaction. Here now, according to the Tukey test, there exist multiple significant differences between subjects and only these subjects do not differ s08-s07, s06-s05, s05-s04, s06-s04, s05-s02, s04-s02, s08-s01, s07-s01, s06-s01. Subject is again the best variable and accounts for 47.7% of the total variance while length accounts for 24% and the interaction only 6.1% according to eta-squared.Similar results are obtained by calculating omega squared.