Objetivo:

Realizar prediciones con Regresión lineal Simple. Caso de restaurante pizzas y estudiantes. Ventas de

Librerias

library(ggplot2) 

Datos

poblacion <- c( 2,   6,  8,  8,  12,  16,  20,   20,  22, 26) 
ventas <-    c(58, 105, 88, 118, 117, 137, 157, 169, 149, 202)

datos <- data.frame(poblacion, ventas)

datos
##    poblacion ventas
## 1          2     58
## 2          6    105
## 3          8     88
## 4          8    118
## 5         12    117
## 6         16    137
## 7         20    157
## 8         20    169
## 9         22    149
## 10        26    202

Visualizacion de dispersion

ggplot(data = datos, mapping = aes(poblacion, ventas)) +
geom_point()

Correlación

cor(datos$poblacion, datos$ventas)
## [1] 0.950123

Recta de regresión

modelo <- lm(data = datos, formula = ventas ~ poblacion)


summary(modelo)
## 
## Call:
## lm(formula = ventas ~ poblacion, data = datos)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -21.00  -9.75  -3.00  11.25  18.00 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  60.0000     9.2260   6.503 0.000187 ***
## poblacion     5.0000     0.5803   8.617 2.55e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 13.83 on 8 degrees of freedom
## Multiple R-squared:  0.9027, Adjusted R-squared:  0.8906 
## F-statistic: 74.25 on 1 and 8 DF,  p-value: 2.549e-05
a <- modelo$coefficients[1]
b <- modelo$coefficients[2]

Multiple R-squared: 0.9027

resumen <- summary(modelo)

sqrt(resumen$r.squared) 
## [1] 0.950123

Linea de tendencia

\[ y = a + bx \] \[ y = 60 + 5x \]

ggplot(data = datos, mapping = aes(poblacion, ventas)) +
  geom_point() +
  geom_line(aes(poblacion, modelo$fitted.values, color = "red"))

Predecir 25 mil estudiantes

x <- 25

prediccion.Y <- a + b * x 

prediccion.Y
## (Intercept) 
##         185

Predecir 25, 30, 28, 15 y 10 mil estudiantes

valornuevo <- data.frame(poblacion=c(25, 30, 28, 15, 10))
prediccion.Y <- predict(object = modelo, newdata =  valornuevo)
prediccion.Y
##   1   2   3   4   5 
## 185 210 200 135 110

Predecir 8, 14, 20, 30, 28, 40, 50 mil estudiantes

valornuevo <- data.frame(poblacion=c(8, 14, 20, 30, 28, 40, 50))
prediccion.Y <- predict(object = modelo, newdata =  valornuevo)
prediccion.Y
##   1   2   3   4   5   6   7 
## 100 130 160 210 200 260 310