library(lme4)
## 載入需要的套件:Matrix
library(lmerTest)
## 
## 載入套件:'lmerTest'
## 下列物件被遮斷自 'package:lme4':
## 
##     lmer
## 下列物件被遮斷自 'package:stats':
## 
##     step
library(readxl)
Exp1_SCJT_ANOVA_MEM_final <- read_excel("MEM/Exp1 SCJT_ANOVA&MEM_final.xls")
View(Exp1_SCJT_ANOVA_MEM_final)
library(readxl)
Exp1_Naming_ANOVA_MEM_final <- read_excel("MEM/Exp1 Naming_ANOVA&MEM_final.xls")
View(Exp1_Naming_ANOVA_MEM_final)

##Exp1(friends) SCJT_ACC

#隨機截距&隨機斜率
m1_lme = lmer(ACC~Task*Condition+(1+Condition|Subject)+(1|ID),data=Exp1_SCJT_ANOVA_MEM_final)
summary(m1_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: ACC ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##    Data: Exp1_SCJT_ANOVA_MEM_final
## 
## REML criterion at convergence: -1637.2
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.5618 -0.0460  0.0562  0.3896  1.3783 
## 
## Random effects:
##  Groups   Name               Variance Std.Dev. Corr 
##  Subject  (Intercept)        0.002009 0.04482       
##           Conditionunlearned 0.002200 0.04690  -0.81
##  ID       (Intercept)        0.002624 0.05122       
##  Residual                    0.032814 0.18115       
## Number of obs: 3081, groups:  Subject, 28; ID, 23
## 
## Fixed effects:
##                           Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)              9.415e-01  1.755e-02  8.562e+01  53.660   <2e-16 ***
## Task                     3.247e-03  3.264e-03  3.004e+03   0.995    0.320    
## Conditionunlearned       2.145e-02  1.770e-02  1.684e+02   1.212    0.227    
## Task:Conditionunlearned -1.483e-15  4.616e-03  3.004e+03   0.000    1.000    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.558              
## Condtnnlrnd -0.574  0.553       
## Tsk:Cndtnnl  0.395 -0.707 -0.783
#只有隨機截距
m2_lme = lmer(ACC~Task*Condition+(1|Subject)+(1|ID),data=Exp1_SCJT_ANOVA_MEM_final)
summary(m2_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: ACC ~ Task * Condition + (1 | Subject) + (1 | ID)
##    Data: Exp1_SCJT_ANOVA_MEM_final
## 
## REML criterion at convergence: -1614.3
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.4898 -0.0390  0.0966  0.3214  1.2890 
## 
## Random effects:
##  Groups   Name        Variance  Std.Dev.
##  Subject  (Intercept) 0.0008901 0.02984 
##  ID       (Intercept) 0.0026687 0.05166 
##  Residual             0.0333182 0.18253 
## Number of obs: 3081, groups:  Subject, 28; ID, 23
## 
## Fixed effects:
##                           Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)              9.413e-01  1.648e-02  8.352e+01  57.117   <2e-16 ***
## Task                     3.247e-03  3.289e-03  3.029e+03   0.987    0.324    
## Conditionunlearned       2.149e-02  1.544e-02  3.031e+03   1.392    0.164    
## Task:Conditionunlearned -1.786e-15  4.651e-03  3.029e+03   0.000    1.000    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.599              
## Condtnnlrnd -0.468  0.639       
## Tsk:Cndtnnl  0.423 -0.707 -0.904
anova(m1_lme, m2_lme)
## refitting model(s) with ML (instead of REML)
## Data: Exp1_SCJT_ANOVA_MEM_final
## Models:
## m2_lme: ACC ~ Task * Condition + (1 | Subject) + (1 | ID)
## m1_lme: ACC ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##        npar     AIC     BIC logLik deviance  Chisq Df Pr(>Chisq)    
## m2_lme    7 -1634.6 -1592.4 824.32  -1648.6                         
## m1_lme    9 -1652.6 -1598.3 835.28  -1670.6 21.931  2  1.729e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#選擇m1,隨機截距&隨機斜率

##Exp1(friends) SCJT_RT

#隨機截距&隨機斜率
m3_lme = lmer(RT~Task*Condition+(1+Condition|Subject)+(1|ID),data=Exp1_SCJT_ANOVA_MEM_final)
summary(m3_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: RT ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##    Data: Exp1_SCJT_ANOVA_MEM_final
## 
## REML criterion at convergence: 39347.4
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.9716 -0.5049 -0.1537  0.2395 16.4580 
## 
## Random effects:
##  Groups   Name               Variance Std.Dev. Corr 
##  Subject  (Intercept)         7707.8   87.79        
##           Conditionunlearned   286.7   16.93   -0.82
##  ID       (Intercept)         1996.3   44.68        
##  Residual                    33012.6  181.69        
## Number of obs: 2963, groups:  Subject, 28; ID, 23
## 
## Fixed effects:
##                         Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)              745.371     22.138   62.137  33.670  < 2e-16 ***
## Task                     -24.234      3.355 2884.480  -7.224 6.43e-13 ***
## Conditionunlearned       -46.936     16.004  490.407  -2.933  0.00352 ** 
## Task:Conditionunlearned    1.945      4.716 2883.924   0.412  0.68011    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.456              
## Condtnnlrnd -0.474  0.630       
## Tsk:Cndtnnl  0.324 -0.711 -0.886
#只有隨機截距
m4_lme = lmer(RT~Task*Condition+(1|Subject)+(1|ID),data=Exp1_SCJT_ANOVA_MEM_final)
summary(m4_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: RT ~ Task * Condition + (1 | Subject) + (1 | ID)
##    Data: Exp1_SCJT_ANOVA_MEM_final
## 
## REML criterion at convergence: 39350.8
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.0186 -0.5065 -0.1546  0.2437 16.3645 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Subject  (Intercept)  6493     80.58  
##  ID       (Intercept)  1974     44.42  
##  Residual             33082    181.88  
## Number of obs: 2963, groups:  Subject, 28; ID, 23
## 
## Fixed effects:
##                         Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)              745.164     21.116   73.998  35.289  < 2e-16 ***
## Task                     -24.235      3.358 2910.578  -7.217 6.75e-13 ***
## Conditionunlearned       -46.874     15.697 2910.837  -2.986  0.00285 ** 
## Task:Conditionunlearned    1.928      4.721 2910.542   0.408  0.68302    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.478              
## Condtnnlrnd -0.376  0.643       
## Tsk:Cndtnnl  0.340 -0.711 -0.904
anova(m3_lme,m4_lme)
## refitting model(s) with ML (instead of REML)
## Data: Exp1_SCJT_ANOVA_MEM_final
## Models:
## m4_lme: RT ~ Task * Condition + (1 | Subject) + (1 | ID)
## m3_lme: RT ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##        npar   AIC   BIC logLik deviance  Chisq Df Pr(>Chisq)
## m4_lme    7 39387 39429 -19686    39373                     
## m3_lme    9 39387 39441 -19685    39369 3.2869  2     0.1933
#選擇m4,只有隨機截距

##Exp1(friends) Naming_ACC

#隨機截距&隨機斜率
m5_lme = lmer(ACC~Task*Condition+(1+Condition|Subject)+(1|ID),data=Exp1_Naming_ANOVA_MEM_final)
summary(m5_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: ACC ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##    Data: Exp1_Naming_ANOVA_MEM_final
## 
## REML criterion at convergence: -2408.9
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -6.0478 -0.0092  0.0212  0.1637  2.2262 
## 
## Random effects:
##  Groups   Name               Variance  Std.Dev. Corr 
##  Subject  (Intercept)        0.0006313 0.02513       
##           Conditionunlearned 0.0016202 0.04025  -0.66
##  ID       (Intercept)        0.0053850 0.07338       
##  Residual                    0.0272253 0.16500       
## Number of obs: 3360, groups:  Subject, 28; ID, 24
## 
## Fixed effects:
##                           Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)              9.417e-01  1.833e-02  4.586e+01  51.368  < 2e-16 ***
## Task                     9.524e-03  2.847e-03  3.280e+03   3.346  0.00083 ***
## Conditionunlearned       2.173e-02  1.537e-02  1.693e+02   1.414  0.15923    
## Task:Conditionunlearned -1.042e-02  4.026e-03  3.280e+03  -2.588  0.00971 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.466              
## Condtnnlrnd -0.400  0.556       
## Tsk:Cndtnnl  0.329 -0.707 -0.786
#只有隨機截距
m6_lme = lmer(ACC~Task*Condition+(1|Subject)+(1|ID),data=Exp1_Naming_ANOVA_MEM_final)
summary(m6_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: ACC ~ Task * Condition + (1 | Subject) + (1 | ID)
##    Data: Exp1_Naming_ANOVA_MEM_final
## 
## REML criterion at convergence: -2389.6
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.9405 -0.0523  0.0623  0.1705  2.0129 
## 
## Random effects:
##  Groups   Name        Variance  Std.Dev.
##  Subject  (Intercept) 0.0003704 0.01925 
##  ID       (Intercept) 0.0055246 0.07433 
##  Residual             0.0275991 0.16613 
## Number of obs: 3360, groups:  Subject, 28; ID, 24
## 
## Fixed effects:
##                           Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)              9.417e-01  1.827e-02  4.474e+01  51.543   <2e-16 ***
## Task                     9.524e-03  2.866e-03  3.306e+03   3.323   0.0009 ***
## Conditionunlearned       2.173e-02  1.344e-02  3.306e+03   1.616   0.1061    
## Task:Conditionunlearned -1.042e-02  4.053e-03  3.306e+03  -2.570   0.0102 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.471              
## Condtnnlrnd -0.368  0.640       
## Tsk:Cndtnnl  0.333 -0.707 -0.905
anova(m5_lme,m6_lme)
## refitting model(s) with ML (instead of REML)
## Data: Exp1_Naming_ANOVA_MEM_final
## Models:
## m6_lme: ACC ~ Task * Condition + (1 | Subject) + (1 | ID)
## m5_lme: ACC ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##        npar     AIC     BIC logLik deviance  Chisq Df Pr(>Chisq)    
## m6_lme    7 -2410.3 -2367.4 1212.1  -2424.3                         
## m5_lme    9 -2424.7 -2369.6 1221.3  -2442.7 18.396  2  0.0001012 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#選擇m5,隨機截距&隨機斜率
#分割資料
fixA <- split(Exp1_Naming_ANOVA_MEM_final,Exp1_Naming_ANOVA_MEM_final$Task)
fixB <- split(Exp1_Naming_ANOVA_MEM_final,Exp1_Naming_ANOVA_MEM_final$Condition)
#在不同檢測階段的簡單主要效果
fixA1.aov <- aov(ACC ~ Condition, data = fixA$`1`)
fixA2.aov <- aov(ACC ~ Condition, data = fixA$`2`)
fixA3.aov <- aov(ACC ~ Condition, data = fixA$`3`)
fixA4.aov <- aov(ACC ~ Condition, data = fixA$`4`)
fixA5.aov <- aov(ACC ~ Condition, data = fixA$`5`)
summary(fixA1.aov)
##              Df Sum Sq Mean Sq F value Pr(>F)
## Condition     1   0.12 0.12054   2.443  0.119
## Residuals   670  33.06 0.04934
summary(fixA2.aov)
##              Df Sum Sq Mean Sq F value Pr(>F)
## Condition     1  0.054 0.05357   1.855  0.174
## Residuals   670 19.351 0.02888
summary(fixA3.aov)
##              Df Sum Sq Mean Sq F value Pr(>F)
## Condition     1  0.037 0.03720   1.227  0.268
## Residuals   670 20.307 0.03031
summary(fixA4.aov)
##              Df Sum Sq Mean Sq F value Pr(>F)
## Condition     1  0.037  0.0372   1.353  0.245
## Residuals   670 18.426  0.0275
summary(fixA5.aov)
##              Df Sum Sq Mean Sq F value Pr(>F)  
## Condition     1  0.121 0.12054   3.993 0.0461 *
## Residuals   670 20.223 0.03018                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#在不同同音鄰群類型的簡單主要效果
fixB1.aov <- aov(ACC ~ Task, data = fixB$`learned`)
fixB2.aov <- aov(ACC ~ Task, data = fixB$`unlearned`)
summary(fixB1.aov)
##               Df Sum Sq Mean Sq F value  Pr(>F)   
## Task           1   0.30 0.30476   10.61 0.00115 **
## Residuals   1678  48.21 0.02873                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(fixB2.aov)
##               Df Sum Sq Mean Sq F value Pr(>F)
## Task           1    0.0 0.00268   0.071   0.79
## Residuals   1678   63.4 0.03779

##Exp1(friends) Naming_RT

#隨機截距&隨機斜率用
m7_lme = lmer(RT~Task*Condition+(1+Condition|Subject)+(1|ID),data=Exp1_Naming_ANOVA_MEM_final)
summary(m7_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: RT ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##    Data: Exp1_Naming_ANOVA_MEM_final
## 
## REML criterion at convergence: 38502.4
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.6553 -0.4722 -0.0773  0.3395 15.1467 
## 
## Random effects:
##  Groups   Name               Variance Std.Dev. Corr 
##  Subject  (Intercept)        5115.1   71.52         
##           Conditionunlearned  835.9   28.91    -0.75
##  ID       (Intercept)        1964.0   44.32         
##  Residual                    9275.1   96.31         
## Number of obs: 3200, groups:  Subject, 28; ID, 24
## 
## Fixed effects:
##                          Estimate Std. Error        df t value Pr(>|t|)    
## (Intercept)              543.1939    17.2321   54.1818  31.522  < 2e-16 ***
## Task                      -5.8537     1.7046 3121.5655  -3.434 0.000603 ***
## Conditionunlearned         1.4730     9.7149  124.2674   0.152 0.879729    
## Task:Conditionunlearned   -0.8376     2.4123 3121.4607  -0.347 0.728457    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.299              
## Condtnnlrnd -0.526  0.531       
## Tsk:Cndtnnl  0.212 -0.707 -0.749
#只有隨機截距
m8_lme = lmer(RT~Task*Condition+(1|Subject)+(1|ID),data=Exp1_Naming_ANOVA_MEM_final)
summary(m8_lme)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: RT ~ Task * Condition + (1 | Subject) + (1 | ID)
##    Data: Exp1_Naming_ANOVA_MEM_final
## 
## REML criterion at convergence: 38542.4
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -4.5717 -0.4690 -0.0728  0.3318 15.5507 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Subject  (Intercept) 3753     61.26   
##  ID       (Intercept) 2157     46.44   
##  Residual             9460     97.26   
## Number of obs: 3200, groups:  Subject, 28; ID, 24
## 
## Fixed effects:
##                         Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)              543.082     16.030   61.541  33.879  < 2e-16 ***
## Task                      -5.811      1.721 3146.121  -3.376 0.000743 ***
## Conditionunlearned         1.522      8.111 3146.110   0.188 0.851171    
## Task:Conditionunlearned   -0.850      2.436 3145.994  -0.349 0.727151    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) Task   Cndtnn
## Task        -0.325              
## Condtnnlrnd -0.254  0.642       
## Tsk:Cndtnnl  0.230 -0.707 -0.905
anova(m7_lme,m8_lme)
## refitting model(s) with ML (instead of REML)
## Data: Exp1_Naming_ANOVA_MEM_final
## Models:
## m8_lme: RT ~ Task * Condition + (1 | Subject) + (1 | ID)
## m7_lme: RT ~ Task * Condition + (1 + Condition | Subject) + (1 | ID)
##        npar   AIC   BIC logLik deviance  Chisq Df Pr(>Chisq)    
## m8_lme    7 38574 38616 -19280    38560                         
## m7_lme    9 38539 38593 -19260    38521 39.085  2  3.256e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#選擇m7,隨機截距&隨機斜率