MODELO LINEALES MIXTOS
EJEMPLO CO2
Variable 1= Type Variable 2= Treatment Variable 3= Plant (Aleatoria) Variable 4= Conc *Variable5= Uptake (Co2)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(broom.mixed)
## Warning: package 'broom.mixed' was built under R version 4.3.1
library(lme4)
## Warning: package 'lme4' was built under R version 4.3.1
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
library(ggplot2)
1. BASE DE DATOS
data2023= "co2"
print(CO2)
## Grouped Data: uptake ~ conc | Plant
## Plant Type Treatment conc uptake
## 1 Qn1 Quebec nonchilled 95 16.0
## 2 Qn1 Quebec nonchilled 175 30.4
## 3 Qn1 Quebec nonchilled 250 34.8
## 4 Qn1 Quebec nonchilled 350 37.2
## 5 Qn1 Quebec nonchilled 500 35.3
## 6 Qn1 Quebec nonchilled 675 39.2
## 7 Qn1 Quebec nonchilled 1000 39.7
## 8 Qn2 Quebec nonchilled 95 13.6
## 9 Qn2 Quebec nonchilled 175 27.3
## 10 Qn2 Quebec nonchilled 250 37.1
## 11 Qn2 Quebec nonchilled 350 41.8
## 12 Qn2 Quebec nonchilled 500 40.6
## 13 Qn2 Quebec nonchilled 675 41.4
## 14 Qn2 Quebec nonchilled 1000 44.3
## 15 Qn3 Quebec nonchilled 95 16.2
## 16 Qn3 Quebec nonchilled 175 32.4
## 17 Qn3 Quebec nonchilled 250 40.3
## 18 Qn3 Quebec nonchilled 350 42.1
## 19 Qn3 Quebec nonchilled 500 42.9
## 20 Qn3 Quebec nonchilled 675 43.9
## 21 Qn3 Quebec nonchilled 1000 45.5
## 22 Qc1 Quebec chilled 95 14.2
## 23 Qc1 Quebec chilled 175 24.1
## 24 Qc1 Quebec chilled 250 30.3
## 25 Qc1 Quebec chilled 350 34.6
## 26 Qc1 Quebec chilled 500 32.5
## 27 Qc1 Quebec chilled 675 35.4
## 28 Qc1 Quebec chilled 1000 38.7
## 29 Qc2 Quebec chilled 95 9.3
## 30 Qc2 Quebec chilled 175 27.3
## 31 Qc2 Quebec chilled 250 35.0
## 32 Qc2 Quebec chilled 350 38.8
## 33 Qc2 Quebec chilled 500 38.6
## 34 Qc2 Quebec chilled 675 37.5
## 35 Qc2 Quebec chilled 1000 42.4
## 36 Qc3 Quebec chilled 95 15.1
## 37 Qc3 Quebec chilled 175 21.0
## 38 Qc3 Quebec chilled 250 38.1
## 39 Qc3 Quebec chilled 350 34.0
## 40 Qc3 Quebec chilled 500 38.9
## 41 Qc3 Quebec chilled 675 39.6
## 42 Qc3 Quebec chilled 1000 41.4
## 43 Mn1 Mississippi nonchilled 95 10.6
## 44 Mn1 Mississippi nonchilled 175 19.2
## 45 Mn1 Mississippi nonchilled 250 26.2
## 46 Mn1 Mississippi nonchilled 350 30.0
## 47 Mn1 Mississippi nonchilled 500 30.9
## 48 Mn1 Mississippi nonchilled 675 32.4
## 49 Mn1 Mississippi nonchilled 1000 35.5
## 50 Mn2 Mississippi nonchilled 95 12.0
## 51 Mn2 Mississippi nonchilled 175 22.0
## 52 Mn2 Mississippi nonchilled 250 30.6
## 53 Mn2 Mississippi nonchilled 350 31.8
## 54 Mn2 Mississippi nonchilled 500 32.4
## 55 Mn2 Mississippi nonchilled 675 31.1
## 56 Mn2 Mississippi nonchilled 1000 31.5
## 57 Mn3 Mississippi nonchilled 95 11.3
## 58 Mn3 Mississippi nonchilled 175 19.4
## 59 Mn3 Mississippi nonchilled 250 25.8
## 60 Mn3 Mississippi nonchilled 350 27.9
## 61 Mn3 Mississippi nonchilled 500 28.5
## 62 Mn3 Mississippi nonchilled 675 28.1
## 63 Mn3 Mississippi nonchilled 1000 27.8
## 64 Mc1 Mississippi chilled 95 10.5
## 65 Mc1 Mississippi chilled 175 14.9
## 66 Mc1 Mississippi chilled 250 18.1
## 67 Mc1 Mississippi chilled 350 18.9
## 68 Mc1 Mississippi chilled 500 19.5
## 69 Mc1 Mississippi chilled 675 22.2
## 70 Mc1 Mississippi chilled 1000 21.9
## 71 Mc2 Mississippi chilled 95 7.7
## 72 Mc2 Mississippi chilled 175 11.4
## 73 Mc2 Mississippi chilled 250 12.3
## 74 Mc2 Mississippi chilled 350 13.0
## 75 Mc2 Mississippi chilled 500 12.5
## 76 Mc2 Mississippi chilled 675 13.7
## 77 Mc2 Mississippi chilled 1000 14.4
## 78 Mc3 Mississippi chilled 95 10.6
## 79 Mc3 Mississippi chilled 175 18.0
## 80 Mc3 Mississippi chilled 250 17.9
## 81 Mc3 Mississippi chilled 350 17.9
## 82 Mc3 Mississippi chilled 500 17.9
## 83 Mc3 Mississippi chilled 675 18.9
## 84 Mc3 Mississippi chilled 1000 19.9
Ruido=runif(84,0.02,0.04)
Uptake2= Ruido+CO2$uptake
print(Uptake2)
## [1] 16.024269 30.430803 34.830751 37.236099 35.338709 39.233665 39.739403
## [8] 13.625062 27.332062 37.133075 41.836500 40.629038 41.426623 44.336706
## [15] 16.226614 32.435378 40.331728 42.123874 42.931842 43.923327 45.522295
## [22] 14.234168 24.120403 30.338876 34.625652 32.520859 35.427634 38.735680
## [29] 9.323210 27.335237 35.026381 38.826496 38.626286 37.520760 42.439008
## [36] 15.134291 21.028970 38.120407 34.020015 38.923176 39.620840 41.429767
## [43] 10.629146 19.232918 26.235948 30.022028 30.924584 32.427455 35.525549
## [50] 12.025922 22.025837 30.634936 31.821229 32.427029 31.130845 31.533873
## [57] 11.327390 19.427482 25.824640 27.939488 28.521386 28.137217 27.830469
## [64] 10.539310 14.929518 18.121689 18.933059 19.532601 22.222825 21.926568
## [71] 7.738803 11.426874 12.331619 13.031225 12.534495 13.738229 14.421754
## [78] 10.633734 18.029090 17.921885 17.931126 17.931070 18.938221 19.934600
hist(Uptake2)
2.ANÁLISIS DESCRIPTIVO
ggplot(CO2, aes(x= conc, y= Uptake2, color=Type))+ geom_point()
*Podemos ver en el primer grafico que a mayor conc las mejores plantas
que captan CO2 son las del type= Quebec
ggplot(CO2, aes(x= conc, y= Uptake2, color=Type))+ geom_point(aes(shape=Treatment))+theme_bw()
*En la grafica anterior podemos ver la interaccion del primer grafico
con el Treatment o enfriamiento -Nonchilled= No Resfriado; Chilled=
Resfriado
ggplot(CO2, aes(x= conc, y= Uptake2, color=Type))+ geom_point(aes(shape=Treatment))+
geom_path(aes(group=Plant, lty=Treatment)) +theme_bw()
Podemos apreciar tres plantas del tipo Mississippi que encuentra resfriadas Podemos apreciar tres plantas del tipo Mississippi que encuentra No resfriadas * Adiferencia del type Quebec no se puede ver una tendecia clara
*Ya que la variable Plant es una variable aleatoria no podriamos interpretar adecaudamente solo con la grafica anterior
*Aunque podemos ver una interaccion entre Type y treatment
Para analizar los modelos aleatorios se usa:
MODELO LINEAL VS MODELO LINEAL MIXTO
#NOTA: Modelo exponencial se usa la función (Log).
#Modelo lineal
Fit1 <- lm(Uptake2 ~ I(log(conc)) + Type:Treatment, data = CO2)
summary(Fit1)
##
## Call:
## lm(formula = Uptake2 ~ I(log(conc)) + Type:Treatment, data = CO2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.721 -2.890 0.583 2.765 8.867
##
## Coefficients: (1 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -33.5256 4.0764 -8.224 3.19e-12 ***
## I(log(conc)) 8.4840 0.6783 12.508 < 2e-16 ***
## TypeQuebec:Treatmentnonchilled 19.5190 1.4401 13.554 < 2e-16 ***
## TypeMississippi:Treatmentnonchilled 10.1361 1.4401 7.038 6.31e-10 ***
## TypeQuebec:Treatmentchilled 15.9348 1.4401 11.065 < 2e-16 ***
## TypeMississippi:Treatmentchilled NA NA NA NA
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.667 on 79 degrees of freedom
## Multiple R-squared: 0.8228, Adjusted R-squared: 0.8138
## F-statistic: 91.68 on 4 and 79 DF, p-value: < 2.2e-16
##se utiliza unicamente para variables de efecto fijo.
#siendo uptake 2 la variable respuesta que depende de la concentración de CO2, y la interacción entre el tratamiento y el lugar.
#Planteamos interacción en el ejemplo al observar la gráfica.
#Modelo lineal mixto
#Aleatorizar los datos, factor aleatorio (1| Plant).
#Esto hace que cada una de las plantas tenga un intercepto distinto.
Fit2 <- lmer(Uptake2 ~ I(log(conc)) + Type : Treatment + (1 | Plant), data = CO2)
## fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
summary(Fit2)
## Linear mixed model fit by REML ['lmerMod']
## Formula: Uptake2 ~ I(log(conc)) + Type:Treatment + (1 | Plant)
## Data: CO2
##
## REML criterion at convergence: 482.3
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.6944 -0.5386 0.1038 0.6899 1.8917
##
## Random effects:
## Groups Name Variance Std.Dev.
## Plant (Intercept) 2.151 1.467
## Residual 20.252 4.500
## Number of obs: 84, groups: Plant, 12
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) -33.5256 4.0213 -8.337
## I(log(conc)) 8.4840 0.6541 12.970
## TypeQuebec:Treatmentnonchilled 19.5190 1.8338 10.644
## TypeMississippi:Treatmentnonchilled 10.1361 1.8338 5.527
## TypeQuebec:Treatmentchilled 15.9348 1.8338 8.689
##
## Correlation of Fixed Effects:
## (Intr) I(l()) TypQbc:Trtmntn TypM:T
## I(log(cnc)) -0.947
## TypQbc:Trtmntn -0.228 0.000
## TypMssssp:T -0.228 0.000 0.500
## TypQbc:Trtmntc -0.228 0.000 0.500 0.500
## fit warnings:
## fixed-effect model matrix is rank deficient so dropping 1 column / coefficient
#Se tiene en cuenta errores aleatorios por individuo.
*Se observa que los interceptos y pendientes de cada modelos son muy similares
*En este caso para las variables aleatorias la varianza es de 2.166
##MODELO LINEAL.
broom::glance(Fit1)
## # A tibble: 1 × 12
## r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.823 0.814 4.67 91.7 6.98e-29 4 -246. 504. 519.
## # ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
broom::tidy(Fit1)
## # A tibble: 6 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -33.5 4.08 -8.22 3.19e-12
## 2 I(log(conc)) 8.48 0.678 12.5 2.02e-20
## 3 TypeQuebec:Treatmentnonchilled 19.5 1.44 13.6 2.60e-22
## 4 TypeMississippi:Treatmentnonchilled 10.1 1.44 7.04 6.31e-10
## 5 TypeQuebec:Treatmentchilled 15.9 1.44 11.1 1.00e-17
## 6 TypeMississippi:Treatmentchilled NA NA NA NA
#El tidy nos indica cual sera el estimador de cada uno de los factores.
Modelo lineal mixto
broom.mixed::glance(Fit2)
## # A tibble: 1 × 7
## nobs sigma logLik AIC BIC REMLcrit df.residual
## <int> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
## 1 84 4.50 -241. 496. 513. 482. 77
#En este modelo no se tienen R cuadrados.
*En este caso se observa la columna logLik, donde, valores negativos indican una mejor calidad del ajsute del modelo, por tanto, como el nuestor es -241.1621 es un buen ajuste.
broom.mixed::tidy(Fit2)
## # A tibble: 7 × 6
## effect group term estimate std.error statistic
## <chr> <chr> <chr> <dbl> <dbl> <dbl>
## 1 fixed <NA> (Intercept) -33.5 4.02 -8.34
## 2 fixed <NA> I(log(conc)) 8.48 0.654 13.0
## 3 fixed <NA> TypeQuebec:Treatmentnonchilled 19.5 1.83 10.6
## 4 fixed <NA> TypeMississippi:Treatmentnonch… 10.1 1.83 5.53
## 5 fixed <NA> TypeQuebec:Treatmentchilled 15.9 1.83 8.69
## 6 ran_pars Plant sd__(Intercept) 1.47 NA NA
## 7 ran_pars Residual sd__Observation 4.50 NA NA
*Se observa los valores de las interacciones para la columna estimate y se puede decir que se cumple con el análisis descriptivo, debido a que Quebec-nonchilled presenta mayor valor (19.519) y en la grafica concuerda con que son las plantas que más captan CO2. Por otro lado, Mississipi - nonchilled presentan menor valor (10.139) y en la grafica concuerda que captan menos CO2 en comparación con las de Quebec.
EJEMPLO POLLO
data("chickWeight")
## Warning in data("chickWeight"): data set 'chickWeight' not found
print(ChickWeight)
## Grouped Data: weight ~ Time | Chick
## weight Time Chick Diet
## 1 42 0 1 1
## 2 51 2 1 1
## 3 59 4 1 1
## 4 64 6 1 1
## 5 76 8 1 1
## 6 93 10 1 1
## 7 106 12 1 1
## 8 125 14 1 1
## 9 149 16 1 1
## 10 171 18 1 1
## 11 199 20 1 1
## 12 205 21 1 1
## 13 40 0 2 1
## 14 49 2 2 1
## 15 58 4 2 1
## 16 72 6 2 1
## 17 84 8 2 1
## 18 103 10 2 1
## 19 122 12 2 1
## 20 138 14 2 1
## 21 162 16 2 1
## 22 187 18 2 1
## 23 209 20 2 1
## 24 215 21 2 1
## 25 43 0 3 1
## 26 39 2 3 1
## 27 55 4 3 1
## 28 67 6 3 1
## 29 84 8 3 1
## 30 99 10 3 1
## 31 115 12 3 1
## 32 138 14 3 1
## 33 163 16 3 1
## 34 187 18 3 1
## 35 198 20 3 1
## 36 202 21 3 1
## 37 42 0 4 1
## 38 49 2 4 1
## 39 56 4 4 1
## 40 67 6 4 1
## 41 74 8 4 1
## 42 87 10 4 1
## 43 102 12 4 1
## 44 108 14 4 1
## 45 136 16 4 1
## 46 154 18 4 1
## 47 160 20 4 1
## 48 157 21 4 1
## 49 41 0 5 1
## 50 42 2 5 1
## 51 48 4 5 1
## 52 60 6 5 1
## 53 79 8 5 1
## 54 106 10 5 1
## 55 141 12 5 1
## 56 164 14 5 1
## 57 197 16 5 1
## 58 199 18 5 1
## 59 220 20 5 1
## 60 223 21 5 1
## 61 41 0 6 1
## 62 49 2 6 1
## 63 59 4 6 1
## 64 74 6 6 1
## 65 97 8 6 1
## 66 124 10 6 1
## 67 141 12 6 1
## 68 148 14 6 1
## 69 155 16 6 1
## 70 160 18 6 1
## 71 160 20 6 1
## 72 157 21 6 1
## 73 41 0 7 1
## 74 49 2 7 1
## 75 57 4 7 1
## 76 71 6 7 1
## 77 89 8 7 1
## 78 112 10 7 1
## 79 146 12 7 1
## 80 174 14 7 1
## 81 218 16 7 1
## 82 250 18 7 1
## 83 288 20 7 1
## 84 305 21 7 1
## 85 42 0 8 1
## 86 50 2 8 1
## 87 61 4 8 1
## 88 71 6 8 1
## 89 84 8 8 1
## 90 93 10 8 1
## 91 110 12 8 1
## 92 116 14 8 1
## 93 126 16 8 1
## 94 134 18 8 1
## 95 125 20 8 1
## 96 42 0 9 1
## 97 51 2 9 1
## 98 59 4 9 1
## 99 68 6 9 1
## 100 85 8 9 1
## 101 96 10 9 1
## 102 90 12 9 1
## 103 92 14 9 1
## 104 93 16 9 1
## 105 100 18 9 1
## 106 100 20 9 1
## 107 98 21 9 1
## 108 41 0 10 1
## 109 44 2 10 1
## 110 52 4 10 1
## 111 63 6 10 1
## 112 74 8 10 1
## 113 81 10 10 1
## 114 89 12 10 1
## 115 96 14 10 1
## 116 101 16 10 1
## 117 112 18 10 1
## 118 120 20 10 1
## 119 124 21 10 1
## 120 43 0 11 1
## 121 51 2 11 1
## 122 63 4 11 1
## 123 84 6 11 1
## 124 112 8 11 1
## 125 139 10 11 1
## 126 168 12 11 1
## 127 177 14 11 1
## 128 182 16 11 1
## 129 184 18 11 1
## 130 181 20 11 1
## 131 175 21 11 1
## 132 41 0 12 1
## 133 49 2 12 1
## 134 56 4 12 1
## 135 62 6 12 1
## 136 72 8 12 1
## 137 88 10 12 1
## 138 119 12 12 1
## 139 135 14 12 1
## 140 162 16 12 1
## 141 185 18 12 1
## 142 195 20 12 1
## 143 205 21 12 1
## 144 41 0 13 1
## 145 48 2 13 1
## 146 53 4 13 1
## 147 60 6 13 1
## 148 65 8 13 1
## 149 67 10 13 1
## 150 71 12 13 1
## 151 70 14 13 1
## 152 71 16 13 1
## 153 81 18 13 1
## 154 91 20 13 1
## 155 96 21 13 1
## 156 41 0 14 1
## 157 49 2 14 1
## 158 62 4 14 1
## 159 79 6 14 1
## 160 101 8 14 1
## 161 128 10 14 1
## 162 164 12 14 1
## 163 192 14 14 1
## 164 227 16 14 1
## 165 248 18 14 1
## 166 259 20 14 1
## 167 266 21 14 1
## 168 41 0 15 1
## 169 49 2 15 1
## 170 56 4 15 1
## 171 64 6 15 1
## 172 68 8 15 1
## 173 68 10 15 1
## 174 67 12 15 1
## 175 68 14 15 1
## 176 41 0 16 1
## 177 45 2 16 1
## 178 49 4 16 1
## 179 51 6 16 1
## 180 57 8 16 1
## 181 51 10 16 1
## 182 54 12 16 1
## 183 42 0 17 1
## 184 51 2 17 1
## 185 61 4 17 1
## 186 72 6 17 1
## 187 83 8 17 1
## 188 89 10 17 1
## 189 98 12 17 1
## 190 103 14 17 1
## 191 113 16 17 1
## 192 123 18 17 1
## 193 133 20 17 1
## 194 142 21 17 1
## 195 39 0 18 1
## 196 35 2 18 1
## 197 43 0 19 1
## 198 48 2 19 1
## 199 55 4 19 1
## 200 62 6 19 1
## 201 65 8 19 1
## 202 71 10 19 1
## 203 82 12 19 1
## 204 88 14 19 1
## 205 106 16 19 1
## 206 120 18 19 1
## 207 144 20 19 1
## 208 157 21 19 1
## 209 41 0 20 1
## 210 47 2 20 1
## 211 54 4 20 1
## 212 58 6 20 1
## 213 65 8 20 1
## 214 73 10 20 1
## 215 77 12 20 1
## 216 89 14 20 1
## 217 98 16 20 1
## 218 107 18 20 1
## 219 115 20 20 1
## 220 117 21 20 1
## 221 40 0 21 2
## 222 50 2 21 2
## 223 62 4 21 2
## 224 86 6 21 2
## 225 125 8 21 2
## 226 163 10 21 2
## 227 217 12 21 2
## 228 240 14 21 2
## 229 275 16 21 2
## 230 307 18 21 2
## 231 318 20 21 2
## 232 331 21 21 2
## 233 41 0 22 2
## 234 55 2 22 2
## 235 64 4 22 2
## 236 77 6 22 2
## 237 90 8 22 2
## 238 95 10 22 2
## 239 108 12 22 2
## 240 111 14 22 2
## 241 131 16 22 2
## 242 148 18 22 2
## 243 164 20 22 2
## 244 167 21 22 2
## 245 43 0 23 2
## 246 52 2 23 2
## 247 61 4 23 2
## 248 73 6 23 2
## 249 90 8 23 2
## 250 103 10 23 2
## 251 127 12 23 2
## 252 135 14 23 2
## 253 145 16 23 2
## 254 163 18 23 2
## 255 170 20 23 2
## 256 175 21 23 2
## 257 42 0 24 2
## 258 52 2 24 2
## 259 58 4 24 2
## 260 74 6 24 2
## 261 66 8 24 2
## 262 68 10 24 2
## 263 70 12 24 2
## 264 71 14 24 2
## 265 72 16 24 2
## 266 72 18 24 2
## 267 76 20 24 2
## 268 74 21 24 2
## 269 40 0 25 2
## 270 49 2 25 2
## 271 62 4 25 2
## 272 78 6 25 2
## 273 102 8 25 2
## 274 124 10 25 2
## 275 146 12 25 2
## 276 164 14 25 2
## 277 197 16 25 2
## 278 231 18 25 2
## 279 259 20 25 2
## 280 265 21 25 2
## 281 42 0 26 2
## 282 48 2 26 2
## 283 57 4 26 2
## 284 74 6 26 2
## 285 93 8 26 2
## 286 114 10 26 2
## 287 136 12 26 2
## 288 147 14 26 2
## 289 169 16 26 2
## 290 205 18 26 2
## 291 236 20 26 2
## 292 251 21 26 2
## 293 39 0 27 2
## 294 46 2 27 2
## 295 58 4 27 2
## 296 73 6 27 2
## 297 87 8 27 2
## 298 100 10 27 2
## 299 115 12 27 2
## 300 123 14 27 2
## 301 144 16 27 2
## 302 163 18 27 2
## 303 185 20 27 2
## 304 192 21 27 2
## 305 39 0 28 2
## 306 46 2 28 2
## 307 58 4 28 2
## 308 73 6 28 2
## 309 92 8 28 2
## 310 114 10 28 2
## 311 145 12 28 2
## 312 156 14 28 2
## 313 184 16 28 2
## 314 207 18 28 2
## 315 212 20 28 2
## 316 233 21 28 2
## 317 39 0 29 2
## 318 48 2 29 2
## 319 59 4 29 2
## 320 74 6 29 2
## 321 87 8 29 2
## 322 106 10 29 2
## 323 134 12 29 2
## 324 150 14 29 2
## 325 187 16 29 2
## 326 230 18 29 2
## 327 279 20 29 2
## 328 309 21 29 2
## 329 42 0 30 2
## 330 48 2 30 2
## 331 59 4 30 2
## 332 72 6 30 2
## 333 85 8 30 2
## 334 98 10 30 2
## 335 115 12 30 2
## 336 122 14 30 2
## 337 143 16 30 2
## 338 151 18 30 2
## 339 157 20 30 2
## 340 150 21 30 2
## 341 42 0 31 3
## 342 53 2 31 3
## 343 62 4 31 3
## 344 73 6 31 3
## 345 85 8 31 3
## 346 102 10 31 3
## 347 123 12 31 3
## 348 138 14 31 3
## 349 170 16 31 3
## 350 204 18 31 3
## 351 235 20 31 3
## 352 256 21 31 3
## 353 41 0 32 3
## 354 49 2 32 3
## 355 65 4 32 3
## 356 82 6 32 3
## 357 107 8 32 3
## 358 129 10 32 3
## 359 159 12 32 3
## 360 179 14 32 3
## 361 221 16 32 3
## 362 263 18 32 3
## 363 291 20 32 3
## 364 305 21 32 3
## 365 39 0 33 3
## 366 50 2 33 3
## 367 63 4 33 3
## 368 77 6 33 3
## 369 96 8 33 3
## 370 111 10 33 3
## 371 137 12 33 3
## 372 144 14 33 3
## 373 151 16 33 3
## 374 146 18 33 3
## 375 156 20 33 3
## 376 147 21 33 3
## 377 41 0 34 3
## 378 49 2 34 3
## 379 63 4 34 3
## 380 85 6 34 3
## 381 107 8 34 3
## 382 134 10 34 3
## 383 164 12 34 3
## 384 186 14 34 3
## 385 235 16 34 3
## 386 294 18 34 3
## 387 327 20 34 3
## 388 341 21 34 3
## 389 41 0 35 3
## 390 53 2 35 3
## 391 64 4 35 3
## 392 87 6 35 3
## 393 123 8 35 3
## 394 158 10 35 3
## 395 201 12 35 3
## 396 238 14 35 3
## 397 287 16 35 3
## 398 332 18 35 3
## 399 361 20 35 3
## 400 373 21 35 3
## 401 39 0 36 3
## 402 48 2 36 3
## 403 61 4 36 3
## 404 76 6 36 3
## 405 98 8 36 3
## 406 116 10 36 3
## 407 145 12 36 3
## 408 166 14 36 3
## 409 198 16 36 3
## 410 227 18 36 3
## 411 225 20 36 3
## 412 220 21 36 3
## 413 41 0 37 3
## 414 48 2 37 3
## 415 56 4 37 3
## 416 68 6 37 3
## 417 80 8 37 3
## 418 83 10 37 3
## 419 103 12 37 3
## 420 112 14 37 3
## 421 135 16 37 3
## 422 157 18 37 3
## 423 169 20 37 3
## 424 178 21 37 3
## 425 41 0 38 3
## 426 49 2 38 3
## 427 61 4 38 3
## 428 74 6 38 3
## 429 98 8 38 3
## 430 109 10 38 3
## 431 128 12 38 3
## 432 154 14 38 3
## 433 192 16 38 3
## 434 232 18 38 3
## 435 280 20 38 3
## 436 290 21 38 3
## 437 42 0 39 3
## 438 50 2 39 3
## 439 61 4 39 3
## 440 78 6 39 3
## 441 89 8 39 3
## 442 109 10 39 3
## 443 130 12 39 3
## 444 146 14 39 3
## 445 170 16 39 3
## 446 214 18 39 3
## 447 250 20 39 3
## 448 272 21 39 3
## 449 41 0 40 3
## 450 55 2 40 3
## 451 66 4 40 3
## 452 79 6 40 3
## 453 101 8 40 3
## 454 120 10 40 3
## 455 154 12 40 3
## 456 182 14 40 3
## 457 215 16 40 3
## 458 262 18 40 3
## 459 295 20 40 3
## 460 321 21 40 3
## 461 42 0 41 4
## 462 51 2 41 4
## 463 66 4 41 4
## 464 85 6 41 4
## 465 103 8 41 4
## 466 124 10 41 4
## 467 155 12 41 4
## 468 153 14 41 4
## 469 175 16 41 4
## 470 184 18 41 4
## 471 199 20 41 4
## 472 204 21 41 4
## 473 42 0 42 4
## 474 49 2 42 4
## 475 63 4 42 4
## 476 84 6 42 4
## 477 103 8 42 4
## 478 126 10 42 4
## 479 160 12 42 4
## 480 174 14 42 4
## 481 204 16 42 4
## 482 234 18 42 4
## 483 269 20 42 4
## 484 281 21 42 4
## 485 42 0 43 4
## 486 55 2 43 4
## 487 69 4 43 4
## 488 96 6 43 4
## 489 131 8 43 4
## 490 157 10 43 4
## 491 184 12 43 4
## 492 188 14 43 4
## 493 197 16 43 4
## 494 198 18 43 4
## 495 199 20 43 4
## 496 200 21 43 4
## 497 42 0 44 4
## 498 51 2 44 4
## 499 65 4 44 4
## 500 86 6 44 4
## 501 103 8 44 4
## 502 118 10 44 4
## 503 127 12 44 4
## 504 138 14 44 4
## 505 145 16 44 4
## 506 146 18 44 4
## 507 41 0 45 4
## 508 50 2 45 4
## 509 61 4 45 4
## 510 78 6 45 4
## 511 98 8 45 4
## 512 117 10 45 4
## 513 135 12 45 4
## 514 141 14 45 4
## 515 147 16 45 4
## 516 174 18 45 4
## 517 197 20 45 4
## 518 196 21 45 4
## 519 40 0 46 4
## 520 52 2 46 4
## 521 62 4 46 4
## 522 82 6 46 4
## 523 101 8 46 4
## 524 120 10 46 4
## 525 144 12 46 4
## 526 156 14 46 4
## 527 173 16 46 4
## 528 210 18 46 4
## 529 231 20 46 4
## 530 238 21 46 4
## 531 41 0 47 4
## 532 53 2 47 4
## 533 66 4 47 4
## 534 79 6 47 4
## 535 100 8 47 4
## 536 123 10 47 4
## 537 148 12 47 4
## 538 157 14 47 4
## 539 168 16 47 4
## 540 185 18 47 4
## 541 210 20 47 4
## 542 205 21 47 4
## 543 39 0 48 4
## 544 50 2 48 4
## 545 62 4 48 4
## 546 80 6 48 4
## 547 104 8 48 4
## 548 125 10 48 4
## 549 154 12 48 4
## 550 170 14 48 4
## 551 222 16 48 4
## 552 261 18 48 4
## 553 303 20 48 4
## 554 322 21 48 4
## 555 40 0 49 4
## 556 53 2 49 4
## 557 64 4 49 4
## 558 85 6 49 4
## 559 108 8 49 4
## 560 128 10 49 4
## 561 152 12 49 4
## 562 166 14 49 4
## 563 184 16 49 4
## 564 203 18 49 4
## 565 233 20 49 4
## 566 237 21 49 4
## 567 41 0 50 4
## 568 54 2 50 4
## 569 67 4 50 4
## 570 84 6 50 4
## 571 105 8 50 4
## 572 122 10 50 4
## 573 155 12 50 4
## 574 175 14 50 4
## 575 205 16 50 4
## 576 234 18 50 4
## 577 264 20 50 4
## 578 264 21 50 4
ChickWeight$Chick%>%length()
## [1] 578
ChickWeight$Chick%>%unique()%>%length()
## [1] 50
ggplot(ChickWeight, aes(x= Time, y= weight))+geom_point(aes(color=Diet))+ geom_path(aes(color=Diet, group=Chick))
Podemos ver que aun lo pollos tengan una dia establecidad su
pendiente de crecimiento va ser variable Podemos observar que con
la dieta 3 los pollos tienen una pendiente de crecimiento relevante * La
identidad (Chick) tambien es una variable importante, ya que algunos con
la dieta 3 no tuvieron buenos rendimientos
Filt1_poisson= glm(weight~Diet:Time, data = ChickWeight, family =poisson())
tidy(Filt1_poisson)
## # A tibble: 5 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.86 0.00931 415. 0
## 2 Diet1:Time 0.0656 0.000722 90.8 0
## 3 Diet2:Time 0.0754 0.000768 98.1 0
## 4 Diet3:Time 0.0862 0.000729 118. 0
## 5 Diet4:Time 0.0823 0.000756 109. 0
*En la tabla tenemos distintas dietas en relacion con el tiempo, donde su pendiente de crecimiento es mas alta para cada pollo en la Diet3:time con 0,086g/dia
Filt2_poisson= glmer(weight~Diet:Time+(1|Chick), data = ChickWeight, family =poisson())
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
## - Rescale variables?
tidy(Filt2_poisson)
## # A tibble: 6 × 7
## effect group term estimate std.error statistic p.value
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 fixed <NA> (Intercept) 3.84 0.0315 122. 0
## 2 fixed <NA> Diet1:Time 0.0666 0.00105 63.7 0
## 3 fixed <NA> Diet2:Time 0.0749 0.00129 57.9 0
## 4 fixed <NA> Diet3:Time 0.0868 0.00123 70.5 0
## 5 fixed <NA> Diet4:Time 0.0763 0.00126 60.4 0
## 6 ran_pars Chick sd__(Intercept) 0.213 NA NA NA
broom::tidy(Filt1_poisson)
## # A tibble: 5 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.86 0.00931 415. 0
## 2 Diet1:Time 0.0656 0.000722 90.8 0
## 3 Diet2:Time 0.0754 0.000768 98.1 0
## 4 Diet3:Time 0.0862 0.000729 118. 0
## 5 Diet4:Time 0.0823 0.000756 109. 0
broom.mixed::tidy(Filt1_poisson)
## # A tibble: 5 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 3.86 0.00931 415. 0
## 2 Diet1:Time 0.0656 0.000722 90.8 0
## 3 Diet2:Time 0.0754 0.000768 98.1 0
## 4 Diet3:Time 0.0862 0.000729 118. 0
## 5 Diet4:Time 0.0823 0.000756 109. 0
*Se observan diferencias entre los modelos, porque, en el modelo lineal da un estimate del intercepto de 3.86 y cuando se aleatorizan la variable individuo en el modelo mixtose observaun ligero cambio de ese intercepto a 3.84.
*Por otro lado, se observa que la identidad del pollo capta alguna variabilidad y esto genera que no sean iguales los modelos.
broom.mixed::tidy(Filt2_poisson)
## # A tibble: 6 × 7
## effect group term estimate std.error statistic p.value
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 fixed <NA> (Intercept) 3.84 0.0315 122. 0
## 2 fixed <NA> Diet1:Time 0.0666 0.00105 63.7 0
## 3 fixed <NA> Diet2:Time 0.0749 0.00129 57.9 0
## 4 fixed <NA> Diet3:Time 0.0868 0.00123 70.5 0
## 5 fixed <NA> Diet4:Time 0.0763 0.00126 60.4 0
## 6 ran_pars Chick sd__(Intercept) 0.213 NA NA NA
Conclucion: Estos modelos tienen la caracteristica de que nos ayudan a visualizar la variables como interactuan para cada uno de los modelos