require(faraway)
## Loading required package: faraway
#llamado a base de datos
data(rats)
rats
## time poison treat
## 1 0.31 I A
## 2 0.82 I B
## 3 0.43 I C
## 4 0.45 I D
## 5 0.45 I A
## 6 1.10 I B
## 7 0.45 I C
## 8 0.71 I D
## 9 0.46 I A
## 10 0.88 I B
## 11 0.63 I C
## 12 0.66 I D
## 13 0.43 I A
## 14 0.72 I B
## 15 0.76 I C
## 16 0.62 I D
## 17 0.36 II A
## 18 0.92 II B
## 19 0.44 II C
## 20 0.56 II D
## 21 0.29 II A
## 22 0.61 II B
## 23 0.35 II C
## 24 1.02 II D
## 25 0.40 II A
## 26 0.49 II B
## 27 0.31 II C
## 28 0.71 II D
## 29 0.23 II A
## 30 1.24 II B
## 31 0.40 II C
## 32 0.38 II D
## 33 0.22 III A
## 34 0.30 III B
## 35 0.23 III C
## 36 0.30 III D
## 37 0.21 III A
## 38 0.37 III B
## 39 0.25 III C
## 40 0.36 III D
## 41 0.18 III A
## 42 0.38 III B
## 43 0.24 III C
## 44 0.31 III D
## 45 0.23 III A
## 46 0.29 III B
## 47 0.22 III C
## 48 0.33 III D
require(ggplot2)
## Loading required package: ggplot2
ggplot(rats,aes(x=poison, y=time))+geom_point()
#se observo que el veneno 3 era mucho mas agresivo dandoles menos de una hora de vida, en contraste el veneno 2 permitia que los individuos sobrevivieran por los menos 1 hora con 15 minutos. en terminos de los tratamientos, el tratamiento A fue el menos efectivo pues ninguna rata sobrevivio por mas de una hora, seguodo por el tratamiento C, y como tratamiento mas efectivo fue el B siendo, ya que los individuos sobrevivian mas de una hora, cabe resaltar que los datos del tratamiento B se encuentran bastante dispersos.
##ajuste del modelo
mod=lm(time~poison+treat,data=rats)
summary(mod)
##
## Call:
## lm(formula = time ~ poison + treat, data = rats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.25167 -0.09625 -0.01490 0.06177 0.49833
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.45229 0.05592 8.088 4.22e-10 ***
## poisonII -0.07312 0.05592 -1.308 0.19813
## poisonIII -0.34125 0.05592 -6.102 2.83e-07 ***
## treatB 0.36250 0.06458 5.614 1.43e-06 ***
## treatC 0.07833 0.06458 1.213 0.23189
## treatD 0.22000 0.06458 3.407 0.00146 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1582 on 42 degrees of freedom
## Multiple R-squared: 0.6503, Adjusted R-squared: 0.6087
## F-statistic: 15.62 on 5 and 42 DF, p-value: 1.123e-08
table(rats$time)
##
## 0.18 0.21 0.22 0.23 0.24 0.25 0.29 0.3 0.31 0.33 0.35 0.36 0.37 0.38 0.4 0.43
## 1 1 2 3 1 1 2 2 3 1 1 2 1 2 2 2
## 0.44 0.45 0.46 0.49 0.56 0.61 0.62 0.63 0.66 0.71 0.72 0.76 0.82 0.88 0.92 1.02
## 1 3 1 1 1 1 1 1 1 2 1 1 1 1 1 1
## 1.1 1.24
## 1 1
table(rats$poison)
##
## I II III
## 16 16 16
table(rats$treat)
##
## A B C D
## 12 12 12 12
rats$time_=as.factor(rats$time)
rats$poison_=as.factor(rats$poison)
rats$treat_=as.factor(rats$treat)
rats$time_
## [1] 0.31 0.82 0.43 0.45 0.45 1.1 0.45 0.71 0.46 0.88 0.63 0.66 0.43 0.72 0.76
## [16] 0.62 0.36 0.92 0.44 0.56 0.29 0.61 0.35 1.02 0.4 0.49 0.31 0.71 0.23 1.24
## [31] 0.4 0.38 0.22 0.3 0.23 0.3 0.21 0.37 0.25 0.36 0.18 0.38 0.24 0.31 0.23
## [46] 0.29 0.22 0.33
## 34 Levels: 0.18 0.21 0.22 0.23 0.24 0.25 0.29 0.3 0.31 0.33 0.35 0.36 ... 1.24
rats$poison_
## [1] I I I I I I I I I I I I I I I I II II II
## [20] II II II II II II II II II II II II II III III III III III III
## [39] III III III III III III III III III III
## Levels: I II III
rats$treat_
## [1] A B C D A B C D A B C D A B C D A B C D A B C D A B C D A B C D A B C D A B
## [39] C D A B C D A B C D
## Levels: A B C D
mod_A=lm(time~poison+treat, data=rats)
summary(mod_A)
##
## Call:
## lm(formula = time ~ poison + treat, data = rats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.25167 -0.09625 -0.01490 0.06177 0.49833
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.45229 0.05592 8.088 4.22e-10 ***
## poisonII -0.07312 0.05592 -1.308 0.19813
## poisonIII -0.34125 0.05592 -6.102 2.83e-07 ***
## treatB 0.36250 0.06458 5.614 1.43e-06 ***
## treatC 0.07833 0.06458 1.213 0.23189
## treatD 0.22000 0.06458 3.407 0.00146 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1582 on 42 degrees of freedom
## Multiple R-squared: 0.6503, Adjusted R-squared: 0.6087
## F-statistic: 15.62 on 5 and 42 DF, p-value: 1.123e-08
ggplot(rats,aes(x=treat, y=time, fill=poison))+geom_boxplot()
ggplot(rats,aes(x=poison, y=time, fill=poison))+geom_boxplot()
ggplot(rats, aes(x=treat,y=time,fill=treat))+geom_boxplot()
x=as.numeric(rats$treat=="yes")
y=rats$time
mod_B=lm(y~x)
summary(mod_B)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.29938 -0.17938 -0.07938 0.14312 0.76062
##
## Coefficients: (1 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.4794 0.0365 13.13 <2e-16 ***
## x NA NA NA NA
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2529 on 47 degrees of freedom
#en la primer grafica se puede apreciar que los diferentes venenos tienen diferentes efectos en cada individuo segun el tratamiento, el veneno 3 es el mas letal para las ratas sin importar el tratamiento, por otro lado el veneno 2 es el menos letal para las ratas con el tratamiento B y el veneno 1 con el tratamiento C fue el menos letal. en la segunda grafica se observaque tanto demoran los venenos en hacer efecto; donde el veneno mas efectivo es el 3 por su rapida accion. en los otros dos venenos los individuos pueden permanecer vivos por mucho mas tiempo. En el tercer grafico se observa que el tratamiento con menor efecto fue el A gracias a la alta mortalidad en los individuos, por otro lado el trataiento B permite que los individuos sobrevivan por mucho mas tiempo, variando en cada animal.
#modelo de diseƱo anova
mod=lm(time~poison,data=rats)
summary(mod)
##
## Call:
## lm(formula = time ~ poison, data = rats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.31438 -0.15922 -0.03125 0.08594 0.69563
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.61750 0.05234 11.799 2.28e-15 ***
## poisonII -0.07313 0.07401 -0.988 0.328
## poisonIII -0.34125 0.07401 -4.611 3.32e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2093 on 45 degrees of freedom
## Multiple R-squared: 0.3438, Adjusted R-squared: 0.3146
## F-statistic: 11.79 on 2 and 45 DF, p-value: 7.656e-05
anova(mod)
## Analysis of Variance Table
##
## Response: time
## Df Sum Sq Mean Sq F value Pr(>F)
## poison 2 1.0330 0.51651 11.786 7.656e-05 ***
## Residuals 45 1.9721 0.04382
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
mod_C=lm(time~treat,data=rats)
summary(mod_C)
##
## Call:
## lm(formula = time ~ treat, data = rats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.38667 -0.15292 -0.01417 0.12833 0.56333
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.31417 0.06282 5.001 9.62e-06 ***
## treatB 0.36250 0.08885 4.080 0.000186 ***
## treatC 0.07833 0.08885 0.882 0.382739
## treatD 0.22000 0.08885 2.476 0.017196 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2176 on 44 degrees of freedom
## Multiple R-squared: 0.3065, Adjusted R-squared: 0.2593
## F-statistic: 6.484 on 3 and 44 DF, p-value: 0.0009921
anova(mod_C)
## Analysis of Variance Table
##
## Response: time
## Df Sum Sq Mean Sq F value Pr(>F)
## treat 3 0.92121 0.307069 6.4836 0.0009921 ***
## Residuals 44 2.08388 0.047361
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Prueba de comparacion multiple postanova
require (agricolae)
## Loading required package: agricolae
mod=lm(time~poison,data=rats)
summary(mod)
##
## Call:
## lm(formula = time ~ poison, data = rats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.31438 -0.15922 -0.03125 0.08594 0.69563
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.61750 0.05234 11.799 2.28e-15 ***
## poisonII -0.07313 0.07401 -0.988 0.328
## poisonIII -0.34125 0.07401 -4.611 3.32e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2093 on 45 degrees of freedom
## Multiple R-squared: 0.3438, Adjusted R-squared: 0.3146
## F-statistic: 11.79 on 2 and 45 DF, p-value: 7.656e-05
PANOVA=LSD.test(mod,"poison_")
PANOVA
## NULL
mod_C=lm(time~treat,data=rats)
summary(mod_C)
##
## Call:
## lm(formula = time ~ treat, data = rats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.38667 -0.15292 -0.01417 0.12833 0.56333
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.31417 0.06282 5.001 9.62e-06 ***
## treatB 0.36250 0.08885 4.080 0.000186 ***
## treatC 0.07833 0.08885 0.882 0.382739
## treatD 0.22000 0.08885 2.476 0.017196 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2176 on 44 degrees of freedom
## Multiple R-squared: 0.3065, Adjusted R-squared: 0.2593
## F-statistic: 6.484 on 3 and 44 DF, p-value: 0.0009921
PANOVA_A=LSD.test(mod,"treat_")
PANOVA_A
## NULL
table(rats$poison)
##
## I II III
## 16 16 16
rats$poison_=as.factor(rats$poison)
rats$poison_
## [1] I I I I I I I I I I I I I I I I II II II
## [20] II II II II II II II II II II II II II III III III III III III
## [39] III III III III III III III III III III
## Levels: I II III
mod_D=lm(time~poison_, data=rats)
summary(mod_D)
##
## Call:
## lm(formula = time ~ poison_, data = rats)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.31438 -0.15922 -0.03125 0.08594 0.69563
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.61750 0.05234 11.799 2.28e-15 ***
## poison_II -0.07313 0.07401 -0.988 0.328
## poison_III -0.34125 0.07401 -4.611 3.32e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2093 on 45 degrees of freedom
## Multiple R-squared: 0.3438, Adjusted R-squared: 0.3146
## F-statistic: 11.79 on 2 and 45 DF, p-value: 7.656e-05