# Exploracion de las Ventas 

proemdio <- mean(marketing$sales)
Desviacion <- sd(marketing$sales)

data.frame(proemdio,Desviacion)
proemdio Desviacion
16.827 6.260948
gl <- ggplot(data = marketing,mapping = aes(x=sales))+ geom_histogram(fill="green")+ theme_bw()
ggplotly(gl)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
gl2 <- ggplot(data = marketing,mapping = aes(x=youtube,y=sales))+ geom_point(color="red")+ theme_bw()+geom_smooth()
ggplotly(gl2)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
gl3 <- ggplot(data = marketing,mapping = aes(x=newspaper,y=sales))+ geom_point(color="grey")+ theme_bw()+geom_smooth()
ggplotly(gl3)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
gl4 <- ggplot(data = marketing,mapping = aes(x=facebook,y=sales))+ geom_point(color="blue")+ theme_bw()+geom_smooth()
ggplotly(gl4)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
cor(marketing$youtube,marketing$sales)
## [1] 0.7822244
cor(marketing$facebook,marketing$sales)
## [1] 0.5762226
cor(marketing$newspaper,marketing$sales)
## [1] 0.228299
Mo1 <- lm(sales~facebook, data = marketing)
summary(Mo1)
## 
## Call:
## lm(formula = sales ~ facebook, data = marketing)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -18.8766  -2.5589   0.9248   3.3330   9.8173 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 11.17397    0.67548  16.542   <2e-16 ***
## facebook     0.20250    0.02041   9.921   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.13 on 198 degrees of freedom
## Multiple R-squared:  0.332,  Adjusted R-squared:  0.3287 
## F-statistic: 98.42 on 1 and 198 DF,  p-value: < 2.2e-16
Mo2 <- lm(sales~youtube, data = marketing)
# encuntra las ventas promedio es 12.45
Mo3 <- lm(sales~newspaper, data = marketing)

sum
## function (..., na.rm = FALSE)  .Primitive("sum")
predict(Mo1,list(facebook=65),interval = "confidence",level = 0.95)
##        fit      lwr     upr
## 1 24.33619 22.68098 25.9914
predict(Mo2,list(youtube=65),interval = "confidence",level = 0.95)
##        fit      lwr      upr
## 1 11.52899 10.72462 12.33337
predict(Mo3,list(newspaper=65),interval = "confidence",level = 0.95)
##        fit      lwr      upr
## 1 18.37674 17.11817 19.63531
id_modelar=sample(1:200,size = 160)
marketing_modelar=marketing[id_modelar,]
marketing_validar=marketing[-id_modelar,]

##Paso 2 - Estimar el Modelo Set de Modelar
mod_you_modelar=lm(sales~youtube,data=marketing_modelar)

##Paso 3 - Predeccir Set de Validación
sales_pred=predict(mod_you_modelar,list(youtube=marketing_validar$youtube))

##Paso 4 - Comparar Ventas del Modelo y Reales
sales_real=marketing_validar$sales
error=sales_real-sales_pred
res=data.frame(sales_real,sales_pred,error)

##Paso 5 - Calcular Indicador de Evaluación de la Predicción
MAE=mean(abs(error)) #Mean Absolut Error (Error Medio Absoluto)
MAE
## [1] 2.353328