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

library(ggplot2)   # Gráficos mas amigables

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

Visualizar la dispersión de los datos

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

#### Correlación

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

Recta de regresión

m= lm(data = datos, formula = ventas ~ poblacion)
a=summary(m)
a
## 
## 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
sqrt(a$r.squared)
## [1] 0.950123
ggplot(data = datos, mapping = aes(poblacion, ventas)) +
  geom_point() +
  geom_line(aes(poblacion,m$fitted.values, color = "red"))

25k estudiantes

x=25
p= a$coefficients[1,1]+ x*a$coefficients[2,1]
p
## [1] 185
v=data.frame(poblacion=c(25,30,28,15,10))
p1= predict(m,v)
p1
##   1   2   3   4   5 
## 185 210 200 135 110

8k,14k,20k,30k,28k,40k,50k

v=data.frame(poblacion=c(8,14,20,30,28,40,50))
p1= predict(m,v)
p1
##   1   2   3   4   5   6   7 
## 100 130 160 210 200 260 310