library(car)
## Warning: package 'car' was built under R version 4.2.3
## Loading required package: carData
library(faux)
## 
## ************
## Welcome to faux. For support and examples visit:
## https://debruine.github.io/faux/
## - Get and set global package options with: faux_options()
## ************
options(digits=4)
DFL1=rnorm_multi(n=24,vars = 3,mu = c(15,15.2,15.4),sd = c(1.2,1.2,1.25),r = c(0.7,0.8,0.81),
                    varnames=c("Lpre1","Lpre2","Lpre3"))
DFL2=rnorm_multi(n=24,vars = 3,mu = c(16,16.2,16.5),sd = c(1.1,1.05,1.15),r = c(0.72,0.82,0.84),
                 varnames=c("Lpost1","Lpost2","Lpost3"))
DFL3=rnorm_multi(n=24,vars = 3,mu = c(16.8,17.2,17.5),sd = c(1.12,1.15,1.08),r = c(0.74,0.83,0.86),
                 varnames=c("Lpost2_1","Lpost2_2","Lpost2_3"))
dfd1=data.frame(DFL1,DFL2,DFL3,trt=gl(3,8,48,labels=c("trt0","trt1","trt2")),pos=gl(2,12,48,labels=c("interno","externo")))
View(dfd1)


names(dfd1)
##  [1] "Lpre1"    "Lpre2"    "Lpre3"    "Lpost1"   "Lpost2"   "Lpost3"  
##  [7] "Lpost2_1" "Lpost2_2" "Lpost2_3" "trt"      "pos"
contrasts(dfd1$trt)
##      trt1 trt2
## trt0    0    0
## trt1    1    0
## trt2    0    1
xtabs(~ trt, data=dfd1)
## trt
## trt0 trt1 trt2 
##   16   16   16
xtabs(~ pos, data=dfd1)
## pos
## interno externo 
##      24      24
########################## formato largo
library(readxl)
dfl <- read_excel("G:/Mi unidad/MAESTRÍA CIENCIA Y TECNOLOGÍA ALIMENTOS/Métodos Multivariados/Entregables/1.3/L_033023.xlsx", 
                sheet = "h2F2")
some(dfl)
## # A tibble: 10 × 6
##        L trt   fase   hora    id pos    
##    <dbl> <chr> <chr> <dbl> <dbl> <chr>  
##  1  14.3 trt0  pre       1     7 interno
##  2  16.1 trt0  pre       2     6 externo
##  3  16.3 trt0  pre       2     8 interno
##  4  14.5 trt1  pre       2    16 externo
##  5  17.1 trt1  pre       3    15 interno
##  6  16.7 trt2  post1     1    22 interno
##  7  16.3 trt0  post1     2     6 externo
##  8  17.1 trt2  post1     3    23 interno
##  9  17.2 trt0  post2     2     8 externo
## 10  17.5 trt0  post2     3     5 externo
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dfmedias=dfl %>%
  group_by(trt,fase,hora,pos) %>%
  summarise(n = n(),Lmedia = mean(L, na.rm = TRUE))
## `summarise()` has grouped output by 'trt', 'fase', 'hora'. You can override
## using the `.groups` argument.
dfmedias
## # A tibble: 54 × 6
## # Groups:   trt, fase, hora [27]
##    trt   fase   hora pos         n Lmedia
##    <chr> <chr> <dbl> <chr>   <int>  <dbl>
##  1 trt0  post1     1 externo     8   16.3
##  2 trt0  post1     1 interno     8   16.1
##  3 trt0  post1     2 externo     8   16.5
##  4 trt0  post1     2 interno     8   16.0
##  5 trt0  post1     3 externo     8   16.6
##  6 trt0  post1     3 interno     8   16.5
##  7 trt0  post2     1 externo     8   16.6
##  8 trt0  post2     1 interno     8   16.7
##  9 trt0  post2     2 externo     8   17.1
## 10 trt0  post2     2 interno     8   17.2
## # ℹ 44 more rows
library(lattice)
xyplot(Lmedia ~ hora | fase*trt, groups=pos, type="b",data=dfmedias,auto.key = list(2, cex = 0.8, title = "Posición", text = c("Interno", "Externo"), points = TRUE))

#xyplot(Lmedia ~ hora | fase+trt, groups=trt, type="b",data=dfmedias)

mod.ok <- lm(cbind(Lpre1,Lpre2,Lpre3,Lpost1,Lpost2,Lpost3,Lpost2_1,Lpost2_2,Lpost2_3)~factor(pos)+factor(trt),data=dfd1)
mod.ok
## 
## Call:
## lm(formula = cbind(Lpre1, Lpre2, Lpre3, Lpost1, Lpost2, Lpost3, 
##     Lpost2_1, Lpost2_2, Lpost2_3) ~ factor(pos) + factor(trt), 
##     data = dfd1)
## 
## Coefficients:
##                     Lpre1    Lpre2    Lpre3    Lpost1   Lpost2   Lpost3 
## (Intercept)         14.4045  14.9209  14.8845  16.2165  16.1737  16.6193
## factor(pos)externo   0.9795   1.1547   1.2874   0.4670   0.9706   0.4432
## factor(trt)trt1     -0.1075  -0.5411  -0.2089  -0.7535  -0.3037  -0.6880
## factor(trt)trt2      0.0107  -0.2712  -0.2648  -0.9497  -1.1120  -0.6374
##                     Lpost2_1  Lpost2_2  Lpost2_3
## (Intercept)         16.3259   16.9886   17.2903 
## factor(pos)externo   0.2716   -0.6362   -0.1828 
## factor(trt)trt1      0.2154    0.3011    0.3023 
## factor(trt)trt2     -0.1810    0.3458    0.1124
summary(mod.ok)
## Response Lpre1 :
## 
## Call:
## lm(formula = Lpre1 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.5943 -0.6619  0.0534  0.3898  2.0023 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         14.4045     0.2343   61.48   <2e-16 ***
## factor(pos)externo   0.9795     0.4686    2.09    0.042 *  
## factor(trt)trt1     -0.1075     0.4058   -0.26    0.792    
## factor(trt)trt2      0.0107     0.5739    0.02    0.985    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.937 on 44 degrees of freedom
## Multiple R-squared:  0.234,  Adjusted R-squared:  0.182 
## F-statistic: 4.48 on 3 and 44 DF,  p-value: 0.00785
## 
## 
## Response Lpre2 :
## 
## Call:
## lm(formula = Lpre2 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -1.673 -0.417  0.173  0.554  1.553 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          14.921      0.198   75.46   <2e-16 ***
## factor(pos)externo    1.155      0.395    2.92   0.0055 ** 
## factor(trt)trt1      -0.541      0.342   -1.58   0.1213    
## factor(trt)trt2      -0.271      0.484   -0.56   0.5783    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.791 on 44 degrees of freedom
## Multiple R-squared:  0.326,  Adjusted R-squared:  0.28 
## F-statistic:  7.1 on 3 and 44 DF,  p-value: 0.00054
## 
## 
## Response Lpre3 :
## 
## Call:
## lm(formula = Lpre3 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -1.938 -0.601 -0.139  0.799  1.935 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          14.885      0.242   61.46   <2e-16 ***
## factor(pos)externo    1.287      0.484    2.66    0.011 *  
## factor(trt)trt1      -0.209      0.419   -0.50    0.621    
## factor(trt)trt2      -0.265      0.593   -0.45    0.657    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.969 on 44 degrees of freedom
## Multiple R-squared:  0.267,  Adjusted R-squared:  0.217 
## F-statistic: 5.35 on 3 and 44 DF,  p-value: 0.00315
## 
## 
## Response Lpost1 :
## 
## Call:
## lm(formula = Lpost1 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -1.398 -0.525 -0.173  0.464  1.567 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          16.216      0.201   80.67   <2e-16 ***
## factor(pos)externo    0.467      0.402    1.16    0.252    
## factor(trt)trt1      -0.754      0.348   -2.16    0.036 *  
## factor(trt)trt2      -0.950      0.492   -1.93    0.060 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.804 on 44 degrees of freedom
## Multiple R-squared:  0.111,  Adjusted R-squared:  0.0508 
## F-statistic: 1.84 on 3 and 44 DF,  p-value: 0.154
## 
## 
## Response Lpost2 :
## 
## Call:
## lm(formula = Lpost2 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -2.132 -0.758  0.227  0.704  1.829 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          16.174      0.260   62.28   <2e-16 ***
## factor(pos)externo    0.971      0.519    1.87    0.068 .  
## factor(trt)trt1      -0.304      0.450   -0.68    0.503    
## factor(trt)trt2      -1.112      0.636   -1.75    0.087 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.04 on 44 degrees of freedom
## Multiple R-squared:  0.0885, Adjusted R-squared:  0.0263 
## F-statistic: 1.42 on 3 and 44 DF,  p-value: 0.249
## 
## 
## Response Lpost3 :
## 
## Call:
## lm(formula = Lpost3 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -1.992 -0.590  0.144  0.687  1.690 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          16.619      0.262   63.35   <2e-16 ***
## factor(pos)externo    0.443      0.525    0.84     0.40    
## factor(trt)trt1      -0.688      0.454   -1.51     0.14    
## factor(trt)trt2      -0.637      0.643   -0.99     0.33    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.05 on 44 degrees of freedom
## Multiple R-squared:  0.0499, Adjusted R-squared:  -0.0149 
## F-statistic: 0.77 on 3 and 44 DF,  p-value: 0.517
## 
## 
## Response Lpost2_1 :
## 
## Call:
## lm(formula = Lpost2_1 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.9767 -0.4236  0.0626  0.5140  1.4865 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          16.326      0.232   70.30   <2e-16 ***
## factor(pos)externo    0.272      0.464    0.58     0.56    
## factor(trt)trt1       0.215      0.402    0.54     0.60    
## factor(trt)trt2      -0.181      0.569   -0.32     0.75    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.929 on 44 degrees of freedom
## Multiple R-squared:  0.0345, Adjusted R-squared:  -0.0313 
## F-statistic: 0.525 on 3 and 44 DF,  p-value: 0.668
## 
## 
## Response Lpost2_2 :
## 
## Call:
## lm(formula = Lpost2_2 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6141 -0.5539  0.0297  0.4593  1.1422 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          16.989      0.166  102.43   <2e-16 ***
## factor(pos)externo   -0.636      0.332   -1.92    0.062 .  
## factor(trt)trt1       0.301      0.287    1.05    0.300    
## factor(trt)trt2       0.346      0.406    0.85    0.399    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.663 on 44 degrees of freedom
## Multiple R-squared:  0.113,  Adjusted R-squared:  0.0526 
## F-statistic: 1.87 on 3 and 44 DF,  p-value: 0.149
## 
## 
## Response Lpost2_3 :
## 
## Call:
## lm(formula = Lpost2_3 ~ factor(pos) + factor(trt), data = dfd1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3417 -0.4983 -0.0184  0.5818  1.7904 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          17.290      0.210   82.53   <2e-16 ***
## factor(pos)externo   -0.183      0.419   -0.44     0.66    
## factor(trt)trt1       0.302      0.363    0.83     0.41    
## factor(trt)trt2       0.112      0.513    0.22     0.83    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.838 on 44 degrees of freedom
## Multiple R-squared:  0.0258, Adjusted R-squared:  -0.0406 
## F-statistic: 0.389 on 3 and 44 DF,  p-value: 0.762
#(av.ok <- Anova(mod.ok, idata=idata, idesign=~fase*hora, type=3))
av.ok <- Anova(mod.ok, idesign=~fase*hora, type=3)
summary(av.ok)
## 
## Type III MANOVA Tests:
## 
## Sum of squares and products for error:
##            Lpre1  Lpre2  Lpre3 Lpost1   Lpost2  Lpost3 Lpost2_1 Lpost2_2
## Lpre1     38.647 19.914 28.968 -1.925 -15.6761 -17.585   -4.517  -5.5771
## Lpre2     19.914 27.526 19.527  5.521   1.5091   4.839    1.126  -4.6449
## Lpre3     28.968 19.527 41.293  3.379  -6.7827  -6.727   11.480   2.7910
## Lpost1    -1.925  5.521  3.379 28.450  26.4608  28.503    8.063   4.4875
## Lpost2   -15.676  1.509 -6.783 26.461  47.4814  40.365    2.630   0.2527
## Lpost3   -17.585  4.839 -6.727 28.503  40.3645  48.452   13.713   4.0693
## Lpost2_1  -4.517  1.126 11.480  8.063   2.6297  13.713   37.969  19.0526
## Lpost2_2  -5.577 -4.645  2.791  4.487   0.2527   4.069   19.053  19.3647
## Lpost2_3  -4.348 -4.725  2.918  7.239  -4.2206   6.189   25.877  21.7270
##          Lpost2_3
## Lpre1      -4.348
## Lpre2      -4.725
## Lpre3       2.918
## Lpost1      7.239
## Lpost2     -4.221
## Lpost3      6.189
## Lpost2_1   25.877
## Lpost2_2   21.727
## Lpost2_3   30.901
## 
## ------------------------------------------
##  
## Term: (Intercept) 
## 
## Sum of squares and products for the hypothesis:
##          Lpre1 Lpre2 Lpre3 Lpost1 Lpost2 Lpost3 Lpost2_1 Lpost2_2 Lpost2_3
## Lpre1     3320  3439  3430   3737   3728   3830     3763     3915     3985
## Lpre2     3439  3562  3553   3871   3861   3968     3898     4056     4128
## Lpre3     3430  3553  3545   3862   3852   3958     3888     4046     4118
## Lpost1    3737  3871  3862   4208   4196   4312     4236     4408     4486
## Lpost2    3728  3861  3852   4196   4185   4301     4225     4396     4474
## Lpost3    3830  3968  3958   4312   4301   4419     4341     4517     4598
## Lpost2_1  3763  3898  3888   4236   4225   4341     4265     4438     4516
## Lpost2_2  3915  4056  4046   4408   4396   4517     4438     4618     4700
## Lpost2_3  3985  4128  4118   4486   4474   4598     4516     4700     4783
## 
## Multivariate Tests: (Intercept)
##                  Df test stat approx F num Df den Df Pr(>F)    
## Pillai            1       1.0     2909      9     36 <2e-16 ***
## Wilks             1       0.0     2909      9     36 <2e-16 ***
## Hotelling-Lawley  1     727.2     2909      9     36 <2e-16 ***
## Roy               1     727.2     2909      9     36 <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## ------------------------------------------
##  
## Term: factor(pos) 
## 
## Sum of squares and products for the hypothesis:
##            Lpre1   Lpre2   Lpre3  Lpost1  Lpost2  Lpost3 Lpost2_1 Lpost2_2
## Lpre1     3.8378  4.5243  5.0441  1.8297  3.8030  1.7366   1.0642  -2.4925
## Lpre2     4.5243  5.3336  5.9464  2.1570  4.4833  2.0473   1.2546  -2.9384
## Lpre3     5.0441  5.9464  6.6296  2.4048  4.9984  2.2825   1.3987  -3.2760
## Lpost1    1.8297  2.1570  2.4048  0.8723  1.8131  0.8279   0.5074  -1.1883
## Lpost2    3.8030  4.4833  4.9984  1.8131  3.7685  1.7209   1.0546  -2.4699
## Lpost3    1.7366  2.0473  2.2825  0.8279  1.7209  0.7858   0.4816  -1.1279
## Lpost2_1  1.0642  1.2546  1.3987  0.5074  1.0546  0.4816   0.2951  -0.6912
## Lpost2_2 -2.4925 -2.9384 -3.2760 -1.1883 -2.4699 -1.1279  -0.6912   1.6188
## Lpost2_3 -0.7162 -0.8444 -0.9414 -0.3415 -0.7097 -0.3241  -0.1986   0.4652
##          Lpost2_3
## Lpre1     -0.7162
## Lpre2     -0.8444
## Lpre3     -0.9414
## Lpost1    -0.3415
## Lpost2    -0.7097
## Lpost3    -0.3241
## Lpost2_1  -0.1986
## Lpost2_2   0.4652
## Lpost2_3   0.1337
## 
## Multivariate Tests: factor(pos)
##                  Df test stat approx F num Df den Df   Pr(>F)    
## Pillai            1    0.5652      5.2      9     36 0.000155 ***
## Wilks             1    0.4348      5.2      9     36 0.000155 ***
## Hotelling-Lawley  1    1.3000      5.2      9     36 0.000155 ***
## Roy               1    1.3000      5.2      9     36 0.000155 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## ------------------------------------------
##  
## Term: factor(trt) 
## 
## Sum of squares and products for the hypothesis:
##             Lpre1   Lpre2    Lpre3  Lpost1  Lpost2  Lpost3 Lpost2_1 Lpost2_2
## Lpre1     0.13599  0.4801  0.08445  0.3082 -0.3351  0.4262  -0.3731  -0.1444
## Lpre2     0.48012  1.9501  0.52225  1.8922 -0.2870  2.0583  -1.1919  -0.8045
## Lpre3     0.08445  0.5223  0.24939  0.8980  0.5796  0.7513  -0.1216  -0.3487
## Lpost1    0.30823  1.8922  0.89799  3.2336  2.0665  2.7119  -0.4507  -1.2567
## Lpost2   -0.33513 -0.2870  0.57959  2.0665  3.9766  0.8963   1.3597  -0.6805
## Lpost3    0.42616  2.0583  0.75133  2.7119  0.8963  2.5381  -0.8971  -1.0926
## Lpost2_1 -0.37307 -1.1919 -0.12161 -0.4507  1.3597 -0.8971   1.0850   0.2512
## Lpost2_2 -0.14437 -0.8045 -0.34874 -1.2567 -0.6805 -1.0926   0.2512   0.4941
## Lpost2_3 -0.29284 -1.1456 -0.28003 -1.0160  0.3290 -1.1603   0.7485   0.4400
##          Lpost2_3
## Lpre1     -0.2928
## Lpre2     -1.1456
## Lpre3     -0.2800
## Lpost1    -1.0160
## Lpost2     0.3290
## Lpost3    -1.1603
## Lpost2_1   0.7485
## Lpost2_2   0.4400
## Lpost2_3   0.6795
## 
## Multivariate Tests: factor(trt)
##                  Df test stat approx F num Df den Df   Pr(>F)    
## Pillai            2    0.7159    2.292     18     74 0.006791 ** 
## Wilks             2    0.3798    2.491     18     72 0.003336 ** 
## Hotelling-Lawley  2    1.3811    2.686     18     70 0.001684 ** 
## Roy               2    1.1648    4.789      9     37 0.000291 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Se observa en los datos que el factor de tratamiento tiene mayor influencia que la posición de la medición de color en los frutos de durazno.
#Se coloca # en la función Anova ya que aparece un error en el argumento idata que no pudimos resolver.