En el siguiente artículo desarrollaré lo aprendido en el día 3 de Introducción al lenguaje y entorno R para el análisis de datos.
library(readxl)
Peces <- read_excel("C:/WorkR/datos.xlsx", col_types = c("skip",
"numeric", "numeric", "text"))
Peces <- data.frame(Peces)
str(Peces)
## 'data.frame': 100 obs. of 3 variables:
## $ Talla: num 72.3 79.8 62.4 75.1 85.8 ...
## $ Peso : num 2821 5639 5263 7397 6742 ...
## $ Sexo : chr "Hembra" "Macho" "Hembra" "Macho" ...
Halterofilia <- read.csv2(file = "C:/WorkR/Halterofilia.csv", dec = ".")
str(Halterofilia)
## 'data.frame': 462 obs. of 6 variables:
## $ Peso : num 55.6 55.6 55.9 55.7 55.9 ...
## $ Arrancada : int 132 127 130 123 120 127 118 112 121 115 ...
## $ Dos.Tiempos: int 160 161 150 150 149 140 146 152 142 146 ...
## $ Total : int 292 288 280 273 269 267 264 264 263 261 ...
## $ Categoria : chr "menos 56" "menos 56" "menos 56" "menos 56" ...
## $ Sexo : chr "M" "M" "M" "M" ...
Trazamos un gráfico de dispersión para observar el comportamiento al relacionar talla de peces con peso de peces.
plot(x = Peces$Peso, y = Peces$Talla, pch = 20, col = "#370128",
main = "Relación entre talla y peso de peces",
xlab = "Peso", ylab = "Talla")
Creamos el primer modelo y lo trazamos en la gráfica de dispersión
#Creamos el modelo 1 y lo guardamos como un objeto
modelo1 <- lm(Talla ~ Peso, data = Peces)
#Imprimimos los resultados del modelo 1
summary(modelo1)
##
## Call:
## lm(formula = Talla ~ Peso, data = Peces)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.2664 -3.3973 -0.0335 3.2542 13.8990
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.421e+01 2.767e+00 26.820 <2e-16 ***
## Peso 4.809e-04 4.628e-04 1.039 0.301
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.393 on 98 degrees of freedom
## Multiple R-squared: 0.0109, Adjusted R-squared: 0.0008056
## F-statistic: 1.08 on 1 and 98 DF, p-value: 0.3013
#Trazamos la gráfica de dispersión
plot(x = Peces$Peso, y = Peces$Talla, pch = 20, col = "#370128",
main = "Relación entre talla y peso de peces",
xlab = "Peso", ylab = "Talla")
#Colocamos una línea punteada del modelo 1
abline(modelo1, col = "#e42355", lwd = 2, lty = 2)
Creamos el segundo modelo y lo trazamos en la gráfica de dispersión
#Creamos el modelo 2 y lo guardamos como un objeto
modelo2 <- lm(Talla ~ Peso - 1, data = Peces)
#Imprimimos los resultados del modelo 1
summary(modelo2)
##
## Call:
## lm(formula = Talla ~ Peso - 1, data = Peces)
##
## Residuals:
## Min 1Q Median 3Q Max
## -32.397 -7.216 3.633 12.965 39.819
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## Peso 0.0126548 0.0002592 48.83 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 15.5 on 99 degrees of freedom
## Multiple R-squared: 0.9601, Adjusted R-squared: 0.9597
## F-statistic: 2384 on 1 and 99 DF, p-value: < 2.2e-16
#Trazamos la gráfica de dispersión
plot(x = Peces$Peso, y = Peces$Talla, pch = 20, col = "#370128",
main = "Relación entre talla y peso de peces",
xlab = "Peso", ylab = "Talla")
#Colocamos una línea punteada del modelo 1
abline(modelo1, col = "#e42355", lwd = 2, lty = 2)
#Colocamos una línea punteada del modelo 2
abline(modelo2, col = "#fe7945", lwd = 2, lty = 2)
Para analizar estos resultados utilizamos las siguientes funciones:
library(report)
report(modelo1)
## We fitted a linear model (estimated using OLS) to predict Talla with Peso
## (formula: Talla ~ Peso). The model explains a statistically not significant and
## very weak proportion of variance (R2 = 0.01, F(1, 98) = 1.08, p = 0.301, adj.
## R2 = 8.06e-04). The model's intercept, corresponding to Peso = 0, is at 74.21
## (95% CI [68.72, 79.70], t(98) = 26.82, p < .001). Within this model:
##
## - The effect of Peso is statistically non-significant and positive (beta =
## 4.81e-04, 95% CI [-4.37e-04, 1.40e-03], t(98) = 1.04, p = 0.301; Std. beta =
## 0.10, 95% CI [-0.09, 0.30])
##
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
report(modelo2)
## We fitted a linear model (estimated using OLS) to predict Talla with Peso
## (formula: Talla ~ Peso - 1). The model explains a statistically significant and
## substantial proportion of variance (R2 = 0.96, F(1, 99) = 2383.86, p < .001,
## adj. R2 = 0.96). The model's intercept, corresponding to Peso = 0, is at (t() =
## , p ). Within this model:
##
## - The effect of Peso is statistically significant and positive (beta = 0.01,
## 95% CI [0.01, 0.01], t(99) = 48.82, p < .001; Std. beta = 0.10, 95% CI [-0.09,
## 0.30])
##
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
De estos reportes podemos deducir que el modelo 2 es mucho más favorable para la investigación ya que el modelo 1 no es estadisticamente significativo es muy débil la proporción de la varianza, además del R2 que es mucho mayor en el modelo 2.
Trazamos un gráfico de dispersión para observar el comportamiento al relacionar arrancada con peso.
#Cargamos el paquete
library(ggplot2)
#Realizamos una gráfica de dispersión
ggplot(Halterofilia) +
aes(x = Peso, y = Arrancada) +
labs(title = "Relación entre Arrancada y Peso") +
geom_point(shape = "circle", size = 1.5, colour = "#370128") +
theme_bw()
Creamos el primer modelo y lo trazamos en la gráfica de dispersión
#Creamos el modelo 1 y lo guardamos como un objeto
Hmodelo1 <- lm(Arrancada ~ Peso, data = Halterofilia)
#Imprimimos los resultados del modelo 1
summary(Hmodelo1)
##
## Call:
## lm(formula = Arrancada ~ Peso, data = Halterofilia)
##
## Residuals:
## Min 1Q Median 3Q Max
## -81.912 -17.415 0.731 20.343 50.135
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 36.68995 3.86035 9.504 <2e-16 ***
## Peso 1.12324 0.04803 23.386 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 23.86 on 460 degrees of freedom
## Multiple R-squared: 0.5432, Adjusted R-squared: 0.5422
## F-statistic: 546.9 on 1 and 460 DF, p-value: < 2.2e-16
#Trazamos la gráfica de dispersión
ggplot(Halterofilia) +
aes(x = Peso, y = Arrancada) +
labs(title = "Relación entre Arrancada y Peso") +
geom_point(shape = "circle", size = 1.5, colour = "#370128") +
geom_smooth(method = "lm" , formula = y ~ x, col = "#e42355",
linetype = "dashed") +
theme_bw()
Creamos el segundo modelo y lo trazamos en la gráfica de dispersión
#Creamos el modelo 2 y lo guardamos como un objeto
Hmodelo2 <- lm(Arrancada ~ Peso - 1, data = Halterofilia)
#Imprimimos los resultados del modelo 1
summary(Hmodelo2)
##
## Call:
## lm(formula = Arrancada ~ Peso - 1, data = Halterofilia)
##
## Residuals:
## Min 1Q Median 3Q Max
## -90.851 -10.919 6.273 21.293 53.281
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## Peso 1.56047 0.01509 103.4 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 26.06 on 461 degrees of freedom
## Multiple R-squared: 0.9587, Adjusted R-squared: 0.9586
## F-statistic: 1.07e+04 on 1 and 461 DF, p-value: < 2.2e-16
#Trazamos la gráfica de dispersión
ggplot(Halterofilia) +
aes(x = Peso, y = Arrancada) +
labs(title = "Relación entre Arrancada y Peso") +
geom_point(shape = "circle", size = 1.5, colour = "#370128") +
geom_smooth(method = "lm", formula = y ~ x, col = "#e42355",
linetype = "dashed") +
geom_smooth(method = "lm", formula = y ~ x - 1, col = "#fe7945",
linetype = "dashed") +
theme_bw()
Para analizar estos resultados utilizamos las siguientes funciones:
report(Hmodelo1)
## We fitted a linear model (estimated using OLS) to predict Arrancada with Peso
## (formula: Arrancada ~ Peso). The model explains a statistically significant and
## substantial proportion of variance (R2 = 0.54, F(1, 460) = 546.90, p < .001,
## adj. R2 = 0.54). The model's intercept, corresponding to Peso = 0, is at 36.69
## (95% CI [29.10, 44.28], t(460) = 9.50, p < .001). Within this model:
##
## - The effect of Peso is statistically significant and positive (beta = 1.12,
## 95% CI [1.03, 1.22], t(460) = 23.39, p < .001; Std. beta = 0.74, 95% CI [0.68,
## 0.80])
##
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
report(Hmodelo2)
## We fitted a linear model (estimated using OLS) to predict Arrancada with Peso
## (formula: Arrancada ~ Peso - 1). The model explains a statistically significant
## and substantial proportion of variance (R2 = 0.96, F(1, 461) = 10696.80, p <
## .001, adj. R2 = 0.96). The model's intercept, corresponding to Peso = 0, is at
## (t() = , p ). Within this model:
##
## - The effect of Peso is statistically significant and positive (beta = 1.56,
## 95% CI [1.53, 1.59], t(461) = 103.43, p < .001; Std. beta = 0.74, 95% CI [0.68,
## 0.80])
##
## Standardized parameters were obtained by fitting the model on a standardized
## version of the dataset. 95% Confidence Intervals (CIs) and p-values were
## computed using a Wald t-distribution approximation.
De estos reportes podemos deducir que el modelo 2 es mucho más favorable para la investigación que el modelo 1, a pesar de que ambos son estadisticamente significativo, el modelo 2 tiene una mejor R.