rm(list=ls())
library(readxl)
data2 <- read_excel("C:/Users/dyobr/OneDrive/Escritorio/R/Trabajo Sebas/data2.xlsx")
View(data2)
#0. Tratamiento de la DATA
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
alpaca = data2
str(alpaca)
## tibble [190 × 6] (S3: tbl_df/tbl/data.frame)
## $ N° : num [1:190] 1 2 3 4 5 6 7 8 9 10 ...
## $ TIPO : chr [1:190] "H" "H" "H" "H" ...
## $ COLOR: chr [1:190] "C" "B" "B" "B" ...
## $ SEXO : chr [1:190] "H" "M" "H" "H" ...
## $ PESO : num [1:190] 1.14 1.8 1.53 1.62 2.23 ...
## $ DPE : num [1:190] 313 313 313 311 310 310 310 309 309 308 ...
str(alpaca$PESO)
## num [1:190] 1.14 1.8 1.53 1.62 2.23 ...
summary(alpaca$COLOR)
## Length Class Mode
## 190 character character
alpaca$COLOR = as.factor(alpaca$COLOR)
alpaca$COLOR=factor(alpaca$COLOR, levels = levels(alpaca$COLOR),labels = c("C","B","LF"))
str(alpaca$COLOR)
## Factor w/ 3 levels "C","B","LF": 2 1 1 1 1 1 1 1 1 1 ...
library(dplyr)
#1. Prueba ANOVA
anova = aov(alpaca$PESO~alpaca$COLOR)
summary(anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## alpaca$COLOR 2 0.306 0.15282 1.691 0.187
## Residuals 187 16.898 0.09036
#No existe diferencia Significativa P.value 0.187
#Prueba Tukey para confirmar los promedios
TukeyHSD(anova)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = alpaca$PESO ~ alpaca$COLOR)
##
## $`alpaca$COLOR`
## diff lwr upr p adj
## B-C -0.01888497 -0.17415741 0.1363875 0.9555181
## LF-C 0.31536503 -0.09841794 0.7291480 0.1721828
## LF-B 0.33425000 -0.10064922 0.7691492 0.1672134
plot(TukeyHSD(anova),cex.axis=0.3)
#Ninguna de las comparaciones muestra una diferencia significativa en el
peso de vellón entre los colores (Blanco, Café, LF), ya que todos los
valores p ajustados son mayores que 0.05. Los intervalos de confianza
para todas las comparaciones incluyen el valor 0, lo cual también
sugiere que las diferencias observadas en las medias no son
estadísticamente significativas. Estos resultados indican que, basándose
en los datos disponibles, no hay evidencia suficiente para afirmar que
el color de las alpacas influye significativamente en el peso de su
vellón.
#2.-Regresión multiple para ver si las otras variables influyen en el tipo de Vellón
#Volver variables a factores
str(alpaca$TIPO)
## chr [1:190] "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "H" "S" "H" ...
str(alpaca$SEXO)
## chr [1:190] "H" "M" "H" "H" "M" "M" "H" "M" "H" "H" "M" "H" "H" "H" "M" ...
alpaca$TIPO = as.factor(alpaca$TIPO)
alpaca$SEXO = as.factor(alpaca$SEXO)
modelo1 = lm(alpaca$PESO ~ alpaca$DPE + alpaca$TIPO + alpaca$SEXO)
summary(modelo1)
##
## Call:
## lm(formula = alpaca$PESO ~ alpaca$DPE + alpaca$TIPO + alpaca$SEXO)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.62507 -0.20101 -0.01137 0.15324 1.06991
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.7943526 0.1617164 4.912 1.97e-06 ***
## alpaca$DPE 0.0022941 0.0005254 4.366 2.10e-05 ***
## alpaca$TIPOS 0.0397271 0.0715102 0.556 0.579
## alpaca$SEXOM 0.0556918 0.0418119 1.332 0.185
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.288 on 186 degrees of freedom
## Multiple R-squared: 0.1033, Adjusted R-squared: 0.08888
## F-statistic: 7.146 on 3 and 186 DF, p-value: 0.0001441
library(jtools)
## Warning: package 'jtools' was built under R version 4.3.3
plot_coefs(modelo1,scale=FALSE)
## Registered S3 methods overwritten by 'broom':
## method from
## tidy.glht jtools
## tidy.summary.glht jtools
## Loading required namespace: broom.mixed
#3.- Prueba CHI2 para correlación entre tipo de vellón y color
tabla1=table(alpaca$COLOR, alpaca$TIPO) #tabla simple
tabla1
##
## H S
## C 147 16
## B 23 1
## LF 2 1
tablapor1 = tabla1 %>%
prop.table(2) %>%
round(2) #redondear el resultado a 2 decimales
tablapor1
##
## H S
## C 0.85 0.89
## B 0.13 0.06
## LF 0.01 0.06
library(gtsummary)
## Warning: package 'gtsummary' was built under R version 4.3.3
alpaca %>%
select(TIPO,COLOR) %>%
tbl_summary(by=TIPO)
| Characteristic | H, N = 1721 | S, N = 181 |
|---|---|---|
| COLOR | ||
| C | 147 (85%) | 16 (89%) |
| B | 23 (13%) | 1 (5.6%) |
| LF | 2 (1.2%) | 1 (5.6%) |
| 1 n (%) | ||
#Diagrama de barras apiladas
toPlot1 = as.data.frame(tablapor1)
names(toPlot1) = c("Categoria", "Tipo", "Porcentaje")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
ggplot(toPlot1, aes(x=Tipo, y=Porcentaje*100, fill=Categoria)) +
geom_bar(position="stack", stat="identity")+ #Stack indica que son barras apiladas
geom_text(aes(label=paste0(Porcentaje*100,"%")),
position = position_stack(),
vjust=1, size = 3)+
labs(x="Tipo", y="Porcentaje", fill="Color")+
theme_bw()
#Prueba Chi2
chisq.test(tabla1)
## Warning in chisq.test(tabla1): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: tabla1
## X-squared = 2.8018, df = 2, p-value = 0.2464
chisq.test(tabla1)$expected
## Warning in chisq.test(tabla1): Chi-squared approximation may be incorrect
##
## H S
## C 147.557895 15.4421053
## B 21.726316 2.2736842
## LF 2.715789 0.2842105