U1A14

Marian Gutiérrez

03/10/2020

Regresión lineal simple parte 2

  • Para este ejercicio se utilizará los datos de búsquedas a nivel mundial de Reborn Live y Auron PLay.

Reborn_Live

Importar datos

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
library(readr)
reb <- read_csv("reb.csv", col_types = cols(RebornLive = col_integer(), 
    AuronPlay = col_integer()))
head(reb)
## # A tibble: 6 x 2
##   RebornLive AuronPlay
##        <int>     <int>
## 1          1        24
## 2          0        26
## 3          0        27
## 4          0        24
## 5          1        26
## 6          0        27

Conociendo los datos

glimpse(reb)
## Rows: 52
## Columns: 2
## $ RebornLive <int> 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0,...
## $ AuronPlay  <int> 24, 26, 27, 24, 26, 27, 27, 40, 51, 43, 47, 56, 50, 45, ...

Resumen estadístico

summary(reb)
##    RebornLive      AuronPlay     
##  Min.   :0.000   Min.   : 24.00  
##  1st Qu.:0.000   1st Qu.: 48.00  
##  Median :1.000   Median : 55.50  
##  Mean   :2.058   Mean   : 55.87  
##  3rd Qu.:4.000   3rd Qu.: 65.00  
##  Max.   :7.000   Max.   :100.00

Matriz de diagramas de dispersión

pairs(reb)

Matriz de diagramas de coeficiente de correlación

cor(reb)
##            RebornLive AuronPlay
## RebornLive  1.0000000 0.6145259
## AuronPlay   0.6145259 1.0000000

Prueba de correlación de pearson

cor.test(x=reb$AuronPlay, y=reb$RebornLive, method = "pearson", digits=3)
## 
##  Pearson's product-moment correlation
## 
## data:  reb$AuronPlay and reb$RebornLive
## t = 5.5081, df = 50, p-value = 1.259e-06
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.4104617 0.7599751
## sample estimates:
##       cor 
## 0.6145259
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
ggpairs(reb, lower=list(continuous="smooth"), diag=list(continuous="bar"), axisLabels = "none")
## Warning in check_and_set_ggpairs_defaults("diag", diag, continuous =
## "densityDiag", : Changing diag$continuous from 'bar' to 'barDiag'
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

De lo analizado hasta aquí, podemos concluír que:

  1. La variable “RebornLive” esta relacionada con la variable “AuronPlay”, por lo cual la usaremos como respuesta en este modelo.

  2. El coeficiente de correlación de pearson no es tan alto (0.6145259) y el valor de P es significativo (p-value < 1.259e-06), esto indica una correlación no muy intensa.

Modelo de regresión lineal simple

modelo.lineal <- lm(RebornLive~AuronPlay, data=reb)
summary(modelo.lineal)
## 
## Call:
## lm(formula = RebornLive ~ AuronPlay, data = reb)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.0570 -1.4266 -0.2581  1.3424  3.7038 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2.19281    0.80787  -2.714  0.00909 ** 
## AuronPlay    0.07608    0.01381   5.508 1.26e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.724 on 50 degrees of freedom
## Multiple R-squared:  0.3776, Adjusted R-squared:  0.3652 
## F-statistic: 30.34 on 1 and 50 DF,  p-value: 1.259e-06

Ecuación de la recta de mínimos cuadrados

\[ y=-2.19281 + 0.07608x \]

Intervalos de confianza

confint(modelo.lineal)
##                   2.5 %     97.5 %
## (Intercept) -3.81545984 -0.5701564
## AuronPlay    0.04834019  0.1038292

Representación gráfica del modelo

library(ggplot2)
ggplot(data = reb, mapping = aes(x = AuronPlay, y = RebornLive)) +
geom_point(color = "firebrick", size = 2) +
geom_smooth(method = "lm", se = TRUE, color = "black") +
labs(title = "RebornLive ~ AuronPlay", x = "AuronPlay", y = "RebornLive") +
theme_bw() + theme(plot.title = element_text(hjust = 0.5)) 
## `geom_smooth()` using formula 'y ~ x'

Verificar condiciones para aceptar o no el modelo

  • Para evaluar las condiciones que perminten decir que el modelo es válido, se hará un análisis de residuos.
par(mfrow=c(1,2))
plot(modelo.lineal)

Contraste de hipótesis (normalidad de los residuos)

Según el método de prueba de Shapiro-wilk

El test de Shapiro-Wilks plantea la hipótesis nula que una muestra proviene de una distribución normal. Eligimos un nivel de significanza, por ejemplo 0,05, y tenemos una hipótesis alternativa que sostiene que la distribución no es normal.

shapiro.test(modelo.lineal$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  modelo.lineal$residuals
## W = 0.89459, p-value = 0.0002433

Conclusión

Como ya se mencionó en una asignación anterior, la variable RebornLive no depende en su totalidad de la variable AuronPlay, como lo podemos observar con el coeficiente de correlación (0.6145259).