Problema 1

En el ejemplo los instructores se centran en el efecto de sus diferentes programas de educación nutricional, que son los tratamientos; no están preocupados por el efecto de una ciudad específica u otra per se, pero quieren tener en cuenta las diferencias debidas a las diferentes ciudades.

Instructor: bloque fijo Town: factor aleatorio

df<-read.csv("https://raw.githubusercontent.com/PedroGonzalezBeermann2020/DExperimental2021/main/Problema1.csv")
df$Instructor=factor(df$Instructor)
df$Town=factor(df$Town)
df$Sodium=as.numeric(df$Sodium)
df
##       Instructor          Town Sodium
## 1   BrendonSmall Squiggleville   1200
## 2   BrendonSmall Squiggleville   1400
## 3   BrendonSmall Squiggleville   1350
## 4   BrendonSmall Metalocalypse    950
## 5   BrendonSmall Squiggleville   1400
## 6   BrendonSmall Squiggleville   1150
## 7   BrendonSmall Squiggleville   1300
## 8   BrendonSmall Metalocalypse   1325
## 9   BrendonSmall Metalocalypse   1425
## 10  BrendonSmall Squiggleville   1500
## 11  BrendonSmall Squiggleville   1250
## 12  BrendonSmall Metalocalypse   1150
## 13  BrendonSmall Metalocalypse    950
## 14  BrendonSmall Squiggleville   1150
## 15  BrendonSmall Metalocalypse   1600
## 16  BrendonSmall Metalocalypse   1300
## 17  BrendonSmall Metalocalypse   1050
## 18  BrendonSmall Metalocalypse   1300
## 19  BrendonSmall Squiggleville   1700
## 20  BrendonSmall Squiggleville   1300
## 21  CoachMcGuirk Squiggleville   1100
## 22  CoachMcGuirk Squiggleville   1200
## 23  CoachMcGuirk Squiggleville   1250
## 24  CoachMcGuirk Metalocalypse   1050
## 25  CoachMcGuirk Metalocalypse   1200
## 26  CoachMcGuirk Metalocalypse   1250
## 27  CoachMcGuirk Squiggleville   1350
## 28  CoachMcGuirk Squiggleville   1350
## 29  CoachMcGuirk Squiggleville   1325
## 30  CoachMcGuirk Squiggleville   1525
## 31  CoachMcGuirk Squiggleville   1225
## 32  CoachMcGuirk Squiggleville   1125
## 33  CoachMcGuirk Metalocalypse   1000
## 34  CoachMcGuirk Metalocalypse   1125
## 35  CoachMcGuirk Squiggleville   1400
## 36  CoachMcGuirk Metalocalypse   1200
## 37  CoachMcGuirk Squiggleville   1150
## 38  CoachMcGuirk Squiggleville   1400
## 39  CoachMcGuirk Squiggleville   1500
## 40  CoachMcGuirk Squiggleville   1200
## 41 MelissaRobins Metalocalypse    900
## 42 MelissaRobins Metalocalypse   1100
## 43 MelissaRobins Metalocalypse   1150
## 44 MelissaRobins Metalocalypse    950
## 45 MelissaRobins Metalocalypse   1100
## 46 MelissaRobins Metalocalypse   1150
## 47 MelissaRobins Squiggleville   1250
## 48 MelissaRobins Squiggleville   1250
## 49 MelissaRobins Squiggleville   1225
## 50 MelissaRobins Squiggleville   1325
## 51 MelissaRobins Metalocalypse   1125
## 52 MelissaRobins Metalocalypse   1025
## 53 MelissaRobins Metalocalypse    950
## 54 MelissaRobins Metalocalypse    925
## 55 MelissaRobins Squiggleville   1200
## 56 MelissaRobins Metalocalypse   1100
## 57 MelissaRobins Metalocalypse    950
## 58 MelissaRobins Metalocalypse   1300
## 59 MelissaRobins Squiggleville   1400
## 60 MelissaRobins Metalocalypse   1100

Hipótesis

Hipotesis para factor aleatorio-Factor Town Ho: variabilidad del factor Town=0 variabilidad del factor Town x Instructor=0

Ha: variabilidad del factor Town no es igual a 0 variabilidad del factor Town x Instructor no es igual a 0

Hipotesis para el factor fijo-Factor Instructor Ho= Las medias son iguales Ha = Las medias son diferentes

Modelo estadístico

library(lme4)
## Loading required package: Matrix
modelo=lmer(Sodium~(1|Instructor)+(1|Town),data=df)
summary(modelo)
## Linear mixed model fit by REML ['lmerMod']
## Formula: Sodium ~ (1 | Instructor) + (1 | Town)
##    Data: df
## 
## REML criterion at convergence: 763.3
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.64670 -0.72097  0.03089  0.45329  2.87607 
## 
## Random effects:
##  Groups     Name        Variance Std.Dev.
##  Instructor (Intercept)  2954     54.35  
##  Town       (Intercept) 12500    111.81  
##  Residual               20655    143.72  
## Number of obs: 60, groups:  Instructor, 3; Town, 2
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  1216.61      87.06   13.97
modelo2=lm(Sodium~Instructor*Town,data=df)
anova=aov(modelo2)
summary(anova)
##                 Df  Sum Sq Mean Sq F value   Pr(>F)    
## Instructor       2  290146  145073   6.920 0.002110 ** 
## Town             1  329551  329551  15.721 0.000218 ***
## Instructor:Town  2   26269   13135   0.627 0.538263    
## Residuals       54 1131992   20963                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R/. Existe diferencia significativa en el factor Instructor y Town; pero en la interacción no hay diferencia significativa. Por tanto, los efectos activos son Instructor y Town

with(df, interaction.plot(x.factor = Instructor, trace.factor = Town,response = Sodium))

with(df, interaction.plot(x.factor = Town, trace.factor = Instructor,response = Sodium))

R/. Debido a las pendientes en algún punto hay interacción.

PROBLEMA 2

Se miden la mano izquierda y la derecha de varios individuos, las medidas se emparejan dentro de cada individuo. Es decir, queremos hacer coincidir estadísticamente la mano izquierda del individuo A con la mano derecha del individuo A, ya que suponemos que alguien con una mano izquierda grande tendrá una mano derecha grande. Por tanto, la variable Individuo se incluirá en el modelo como variable aleatoria. Se podría pensar que cada individuo tiene un bloque que incluye una medida para la mano izquierda y una medida para la mano derecha.

Datos

df2<-read.csv("https://raw.githubusercontent.com/PedroGonzalezBeermann2020/DExperimental2021/main/Problema2.csv")
df2$IndividuO=factor(df2$IndividuO)
df2$Largo=as.numeric(df2$Largo)
df2$Mano=factor(df2$Mano)
df2
##    IndividuO  Mano Largo
## 1          A  Left  17.5
## 2          B  Left  18.4
## 3          C  Left  16.2
## 4          D  Left  14.5
## 5          E  Left  13.5
## 6          F  Left  18.9
## 7          G  Left  19.5
## 8          H  Left  21.1
## 9          I  Left  17.8
## 10         J  Left  16.8
## 11         K  Left  18.4
## 12         L  Left  17.3
## 13         M  Left  18.9
## 14         N  Left  16.4
## 15         O  Left  17.5
## 16         P  Left  15.0
## 17         A Right  17.6
## 18         B Right  18.5
## 19         C Right  15.9
## 20         D Right  14.9
## 21         E Right  13.7
## 22         F Right  18.9
## 23         G Right  19.5
## 24         H Right  21.5
## 25         I Right  18.5
## 26         J Right  17.1
## 27         K Right  18.9
## 28         L Right  17.5
## 29         M Right  19.5
## 30         N Right  16.5
## 31         O Right  17.4
## 32         P Right  15.6
library(lme4)
modelo2<-lmer(Largo~(1|IndividuO),data=df2)
summary(modelo2)
## Linear mixed model fit by REML ['lmerMod']
## Formula: Largo ~ (1 | IndividuO)
##    Data: df2
## 
## REML criterion at convergence: 78.2
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.35030 -0.54255  0.05752  0.42751  1.39532 
## 
## Random effects:
##  Groups    Name        Variance Std.Dev.
##  IndividuO (Intercept) 3.790    1.947   
##  Residual              0.065    0.255   
## Number of obs: 32, groups:  IndividuO, 16
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  17.4750     0.4888   35.75
modelo3<-lm(Largo~IndividuO,data =df2)
anova2<-aov(modelo3)
summary(anova2)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## IndividuO   15 114.68   7.645   117.6 1.79e-13 ***
## Residuals   16   1.04   0.065                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

R/. Existe diferencia significativa respecto al factor aleatorio individuo, porque el valor p es inferior a 0.05, por tanto la variabilidad no es igual a cero y afecta la medición.

Normalidad

qqnorm(anova2$residuals)
qqline(anova2$residuals)

shapiro.test(modelo3$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  modelo3$residuals
## W = 0.97949, p-value = 0.7847

R/. Los datos presentan una distribución normal debido al valor p mayor a 0.05.