library(nlme)
library(multcomp)
## Loading required package: mvtnorm
## Loading required package: survival
## Loading required package: TH.data
## Loading required package: MASS
## 
## Attaching package: 'TH.data'
## The following object is masked from 'package:MASS':
## 
##     geyser
setwd("/home/michael/Dropbox/BGU/1_Other/Ilan_Stavi/")

# Read
dat = read.csv("data/Sde Zin_all data_170703+correlations.csv", stringsAsFactors = FALSE, check.names = TRUE)

# Factors

# Variables
vars = setdiff(colnames(dat), c("X.", "Block", "Treatment", "Spot", "Depth"))

# Fit model
for(i in vars) {
  
  print("#############################################")
  print(i)
  print("#############################################")
  
  tmp = dat[, c(i, c("Block", "Treatment", "Depth"))]
  tmp = tmp[complete.cases(tmp), ]
  tmp$Treatment = factor(tmp$Treatment)
  tmp$Depth = factor(tmp$Depth)
  
  if(length(unique(tmp$Depth)) == 1) {
    f = as.formula(paste0(i, "  ~ Treatment"))
  } else {
    f = as.formula(paste0(i, "  ~ Treatment * Depth"))
  }
  fit = lme(f, random = ~ 1|Block, tmp)
  s = summary(fit)
  print(s)
  
}
## [1] "#############################################"
## [1] "PR..penetration.resistance..MPa."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   143.8841 161.4014 -63.94207
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept)  Residual
## StdDev:   0.1079614 0.5629206
## 
## Fixed effects: list(f) 
##                                             Value Std.Error DF   t-value
## (Intercept)                             1.2571058 0.1712324 63  7.341518
## TreatmentDeep tillage                  -0.8246471 0.2298114 63 -3.588365
## TreatmentShallow tillage               -0.8167271 0.2298114 63 -3.553902
## Depth15 -- 20                           0.7394740 0.2298114 63  3.217744
## TreatmentDeep tillage:Depth15 -- 20    -0.8437114 0.3250023 63 -2.596016
## TreatmentShallow tillage:Depth15 -- 20 -0.8354279 0.3250023 63 -2.570529
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.0007
## TreatmentShallow tillage                0.0007
## Depth15 -- 20                           0.0020
## TreatmentDeep tillage:Depth15 -- 20     0.0117
## TreatmentShallow tillage:Depth15 -- 20  0.0125
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.671                            
## TreatmentShallow tillage               -0.671  0.500                     
## Depth15 -- 20                          -0.671  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.475 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.475 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.28813618 -0.49865679 -0.08929228  0.33124468  2.57232568 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "VS..kPa."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##       AIC      BIC   logLik
##   681.448 698.9652 -332.724
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept) Residual
## StdDev:     10.5627 32.63209
## 
## Fixed effects: list(f) 
##                                            Value Std.Error DF   t-value
## (Intercept)                            184.54500  10.79956 63 17.088202
## TreatmentDeep tillage                   69.48917  13.32199 63  5.216123
## TreatmentShallow tillage                35.31417  13.32199 63  2.650817
## Depth15 -- 20                            5.69583  13.32199 63  0.427551
## TreatmentDeep tillage:Depth15 -- 20    -15.94833  18.84015 63 -0.846508
## TreatmentShallow tillage:Depth15 -- 20  -5.69583  18.84015 63 -0.302324
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.0000
## TreatmentShallow tillage                0.0101
## Depth15 -- 20                           0.6704
## TreatmentDeep tillage:Depth15 -- 20     0.4005
## TreatmentShallow tillage:Depth15 -- 20  0.7634
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.617                            
## TreatmentShallow tillage               -0.617  0.500                     
## Depth15 -- 20                          -0.617  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.436 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.436 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.84562898 -0.63157868  0.07206476  0.76004888  1.48865434 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "CM...."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   116.8527 124.3353 -53.42636
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept)  Residual
## StdDev:    0.866263 0.9934974
## 
## Fixed effects: list(f) 
##                             Value Std.Error DF   t-value p-value
## (Intercept)              0.500000 0.5194766 30  0.962507  0.3435
## TreatmentDeep tillage    5.583333 0.4055936 30 13.765832  0.0000
## TreatmentShallow tillage 4.083333 0.4055936 30 10.067549  0.0000
##  Correlation: 
##                          (Intr) TrtmDt
## TreatmentDeep tillage    -0.39        
## TreatmentShallow tillage -0.39   0.50 
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -1.6254153 -0.6231376 -0.1326676  0.3748725  3.2481952 
## 
## Number of Observations: 36
## Number of Groups: 4 
## [1] "#############################################"
## [1] "SM...."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   255.1291 272.6463 -119.5646
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept) Residual
## StdDev:   0.1339641 1.317607
## 
## Fixed effects: list(f) 
##                                           Value Std.Error DF  t-value
## (Intercept)                            2.878200 0.3862131 63 7.452364
## TreatmentDeep tillage                  0.238302 0.5379107 63 0.443015
## TreatmentShallow tillage               0.386106 0.5379107 63 0.717788
## Depth15 -- 20                          0.767345 0.5379107 63 1.426528
## TreatmentDeep tillage:Depth15 -- 20    4.635556 0.7607206 63 6.093638
## TreatmentShallow tillage:Depth15 -- 20 3.204241 0.7607206 63 4.212113
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.6593
## TreatmentShallow tillage                0.4755
## Depth15 -- 20                           0.1587
## TreatmentDeep tillage:Depth15 -- 20     0.0000
## TreatmentShallow tillage:Depth15 -- 20  0.0001
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.696                            
## TreatmentShallow tillage               -0.696  0.500                     
## Depth15 -- 20                          -0.696  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.492 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.492 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -2.3283014 -0.4749579 -0.1177505  0.2796501  2.7874175 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "BD..g.cm.2."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##         AIC       BIC   logLik
##   -70.17455 -52.65731 43.08727
## 
## Random effects:
##  Formula: ~1 | Block
##          (Intercept)  Residual
## StdDev: 3.973082e-07 0.1125085
## 
## Fixed effects: list(f) 
##                                             Value  Std.Error DF  t-value
## (Intercept)                             1.5285661 0.03247841 63 47.06406
## TreatmentDeep tillage                  -0.2368510 0.04593141 63 -5.15662
## TreatmentShallow tillage               -0.1605383 0.04593141 63 -3.49517
## Depth15 -- 20                           0.0511512 0.04593141 63  1.11364
## TreatmentDeep tillage:Depth15 -- 20    -0.0178484 0.06495683 63 -0.27477
## TreatmentShallow tillage:Depth15 -- 20 -0.0756794 0.06495683 63 -1.16507
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.0000
## TreatmentShallow tillage                0.0009
## Depth15 -- 20                           0.2697
## TreatmentDeep tillage:Depth15 -- 20     0.7844
## TreatmentShallow tillage:Depth15 -- 20  0.2484
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.707                            
## TreatmentShallow tillage               -0.707  0.500                     
## Depth15 -- 20                          -0.707  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.500 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.500 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -2.2323815 -0.6588525 -0.1249656  0.5886175  2.9914301 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "Total.porosity..clculated."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##       AIC      BIC   logLik
##   409.066 426.5833 -196.533
## 
## Random effects:
##  Formula: ~1 | Block
##          (Intercept) Residual
## StdDev: 0.0002380539 4.245605
## 
## Fixed effects: list(f) 
##                                           Value Std.Error DF  t-value
## (Intercept)                            42.31826  1.225601 63 34.52859
## TreatmentDeep tillage                   8.93777  1.733261 63  5.15662
## TreatmentShallow tillage                6.05805  1.733261 63  3.49517
## Depth15 -- 20                          -1.93023  1.733261 63 -1.11364
## TreatmentDeep tillage:Depth15 -- 20     0.67352  2.451201 63  0.27477
## TreatmentShallow tillage:Depth15 -- 20  2.85583  2.451201 63  1.16507
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.0000
## TreatmentShallow tillage                0.0009
## Depth15 -- 20                           0.2697
## TreatmentDeep tillage:Depth15 -- 20     0.7844
## TreatmentShallow tillage:Depth15 -- 20  0.2484
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.707                            
## TreatmentShallow tillage               -0.707  0.500                     
## Depth15 -- 20                          -0.707  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.500 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.500 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -2.9914301 -0.5886175  0.1249656  0.6588525  2.2323815 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "CaCO3...."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   321.5559 339.0731 -152.7779
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept) Residual
## StdDev:   0.9858441 2.109969
## 
## Fixed effects: list(f) 
##                                            Value Std.Error DF  t-value
## (Intercept)                            26.564270 0.7835620 63 33.90194
## TreatmentDeep tillage                   1.039062 0.8613911 63  1.20626
## TreatmentShallow tillage                0.491859 0.8613911 63  0.57101
## Depth15 -- 20                          -0.567979 0.8613911 63 -0.65937
## TreatmentDeep tillage:Depth15 -- 20     0.271861 1.2181910 63  0.22317
## TreatmentShallow tillage:Depth15 -- 20  0.068304 1.2181910 63  0.05607
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.2322
## TreatmentShallow tillage                0.5700
## Depth15 -- 20                           0.5121
## TreatmentDeep tillage:Depth15 -- 20     0.8241
## TreatmentShallow tillage:Depth15 -- 20  0.9555
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.550                            
## TreatmentShallow tillage               -0.550  0.500                     
## Depth15 -- 20                          -0.550  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.389 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.389 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.48796431 -0.44236567 -0.06589552  0.59687593  3.41969223 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "TOC..g.kg."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   468.2615 485.7787 -226.1307
## 
## Random effects:
##  Formula: ~1 | Block
##          (Intercept) Residual
## StdDev: 0.0002834363 6.648121
## 
## Fixed effects: list(f) 
##                                            Value Std.Error DF   t-value
## (Intercept)                            22.961903  1.919147 63 11.964638
## TreatmentDeep tillage                  -0.211858  2.714084 63 -0.078059
## TreatmentShallow tillage               -5.427593  2.714084 63 -1.999788
## Depth15 -- 20                           2.501102  2.714084 63  0.921527
## TreatmentDeep tillage:Depth15 -- 20    -2.780835  3.838295 63 -0.724497
## TreatmentShallow tillage:Depth15 -- 20  1.815336  3.838295 63  0.472954
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.9380
## TreatmentShallow tillage                0.0498
## Depth15 -- 20                           0.3603
## TreatmentDeep tillage:Depth15 -- 20     0.4714
## TreatmentShallow tillage:Depth15 -- 20  0.6379
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.707                            
## TreatmentShallow tillage               -0.707  0.500                     
## Depth15 -- 20                          -0.707  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.500 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.500 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -1.4377439 -0.6213261 -0.2051760  0.3996879  2.6823258 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "SOC.tratification.ratio"
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   323.9971 331.4796 -156.9985
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept) Residual
## StdDev:    4.994289 24.81667
## 
## Fixed effects: list(f) 
##                              Value Std.Error DF   t-value p-value
## (Intercept)               92.50687  7.586699 30 12.193296  0.0000
## TreatmentDeep tillage      8.89277 10.131365 30  0.877747  0.3871
## TreatmentShallow tillage -10.58191 10.131365 30 -1.044471  0.3046
##  Correlation: 
##                          (Intr) TrtmDt
## TreatmentDeep tillage    -0.668       
## TreatmentShallow tillage -0.668  0.500
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -1.5169047 -0.6893916 -0.1666192  0.5708039  2.4222068 
## 
## Number of Observations: 36
## Number of Groups: 4 
## [1] "#############################################"
## [1] "FC...."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   343.9277 361.4449 -163.9638
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept) Residual
## StdDev:   0.2890523 2.579981
## 
## Fixed effects: list(f) 
##                                            Value Std.Error DF   t-value
## (Intercept)                            20.921933 0.7586696 63 27.577135
## TreatmentDeep tillage                   0.527964 1.0532727 63  0.501260
## TreatmentShallow tillage               -0.669531 1.0532727 63 -0.635667
## Depth15 -- 20                           0.059034 1.0532727 63  0.056048
## TreatmentDeep tillage:Depth15 -- 20     4.990373 1.4895525 63  3.350250
## TreatmentShallow tillage:Depth15 -- 20  5.302252 1.4895525 63  3.559627
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.6179
## TreatmentShallow tillage                0.5273
## Depth15 -- 20                           0.9555
## TreatmentDeep tillage:Depth15 -- 20     0.0014
## TreatmentShallow tillage:Depth15 -- 20  0.0007
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.694                            
## TreatmentShallow tillage               -0.694  0.500                     
## Depth15 -- 20                          -0.694  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.491 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.491 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -2.9413890 -0.5768363  0.2127127  0.6516217  2.2130066 
## 
## Number of Observations: 72
## Number of Groups: 4 
## [1] "#############################################"
## [1] "BRA"
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   1749.903 1764.872 -866.9513
## 
## Random effects:
##  Formula: ~1 | Block
##         (Intercept) Residual
## StdDev:    1917.968 14727394
## 
## Fixed effects: list(f) 
##                                            Value Std.Error DF   t-value
## (Intercept)                             27601769   4909131 46  5.622536
## TreatmentDeep tillage                   79641886   6942560 46 11.471544
## TreatmentShallow tillage                34868962   6942560 46  5.022493
## Depth15 -- 20                          -20631569   6942560 46 -2.971752
## TreatmentDeep tillage:Depth15 -- 20    -73168977   9818263 46 -7.452334
## TreatmentShallow tillage:Depth15 -- 20 -33004061   9818263 46 -3.361497
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.0000
## TreatmentShallow tillage                0.0000
## Depth15 -- 20                           0.0047
## TreatmentDeep tillage:Depth15 -- 20     0.0000
## TreatmentShallow tillage:Depth15 -- 20  0.0016
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.707                            
## TreatmentShallow tillage               -0.707  0.500                     
## Depth15 -- 20                          -0.707  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.500 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.500 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.13683357 -0.40945870 -0.05567042  0.25889227  3.69758298 
## 
## Number of Observations: 54
## Number of Groups: 3 
## [1] "#############################################"
## [1] "AWC...."
## [1] "#############################################"
## Linear mixed-effects model fit by REML
##  Data: tmp 
##        AIC      BIC    logLik
##   278.1948 293.1644 -131.0974
## 
## Random effects:
##  Formula: ~1 | Block
##          (Intercept) Residual
## StdDev: 0.0001249048  3.23792
## 
## Fixed effects: list(f) 
##                                            Value Std.Error DF   t-value
## (Intercept)                            11.305650  1.079306 46 10.474921
## TreatmentDeep tillage                   1.014722  1.526370 46  0.664794
## TreatmentShallow tillage               -0.304959  1.526370 46 -0.199794
## Depth15 -- 20                          -1.469440  1.526370 46 -0.962702
## TreatmentDeep tillage:Depth15 -- 20     5.783623  2.158613 46  2.679324
## TreatmentShallow tillage:Depth15 -- 20  6.272876  2.158613 46  2.905975
##                                        p-value
## (Intercept)                             0.0000
## TreatmentDeep tillage                   0.5095
## TreatmentShallow tillage                0.8425
## Depth15 -- 20                           0.3407
## TreatmentDeep tillage:Depth15 -- 20     0.0102
## TreatmentShallow tillage:Depth15 -- 20  0.0056
##  Correlation: 
##                                        (Intr) TrtmDt TrtmSt D15--2 TDt:-2
## TreatmentDeep tillage                  -0.707                            
## TreatmentShallow tillage               -0.707  0.500                     
## Depth15 -- 20                          -0.707  0.500  0.500              
## TreatmentDeep tillage:Depth15 -- 20     0.500 -0.707 -0.354 -0.707       
## TreatmentShallow tillage:Depth15 -- 20  0.500 -0.354 -0.707 -0.707  0.500
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -2.5436478 -0.5259908  0.0234113  0.7045633  1.9428862 
## 
## Number of Observations: 54
## Number of Groups: 3