En el siguiente ejemplo vamos a trabajar con datos de las venta de una compañia para relacionar las ventas con la inversión en mercadeo
require(datarium)
require(ggplot2)
require(plotly)
data("marketing")
head(marketing)
| youtube | newspaper | sales | |
|---|---|---|---|
| 276.12 | 45.36 | 83.04 | 26.52 |
| 53.40 | 47.16 | 54.12 | 12.48 |
| 20.64 | 55.08 | 83.16 | 11.16 |
| 181.80 | 49.56 | 70.20 | 22.20 |
| 216.96 | 12.96 | 70.08 | 15.48 |
| 10.44 | 58.68 | 90.00 | 8.64 |
## Promedio y desviación
promedio = mean(marketing$sales)
desviacion = sd(marketing$sales)
data.frame(promedio,desviacion)
| promedio | desviacion |
|---|---|
| 16.827 | 6.260948 |
g1=ggplot(data = marketing, mapping = aes(x=sales))+geom_histogram(fill="blue")+theme_bw()
ggplotly(g1)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Análisis Bivariado
g2=ggplot(data = marketing, mapping = aes(x=newspaper, y=sales))+geom_point()+theme_bw()+geom_smooth()
ggplotly(g2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Correlación de Pearson
cor(marketing$newspaper,marketing$sales)
## [1] 0.228299
Al tener un 22% es muy bajo la correlacion.
g3=ggplot(data = marketing, mapping = aes(x=facebook, y=sales))+geom_point()+theme_bw()+geom_smooth()
ggplotly(g3)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Correlacion de Pearson
cor(marketing$facebook,marketing$sales)
## [1] 0.5762226
g4=ggplot(data = marketing, mapping = aes(x=youtube, y=sales))+geom_point()+theme_bw()+geom_smooth()
ggplotly(g4)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Correlacion de Pearson
cor(marketing$youtube,marketing$sales)
## [1] 0.7822244
mod_you = lm(sales~youtube, data = marketing)
mod_you
##
## Call:
## lm(formula = sales ~ youtube, data = marketing)
##
## Coefficients:
## (Intercept) youtube
## 8.43911 0.04754
sales = 8.43911 + (0.04754* youtube)
Las ventas promedio cuando no se inyecta capital a Youtube es de 8.43911 dolares y por cada 1000 dolares crece 0.04754 millones de dolares
summary(mod_you)
##
## Call:
## lm(formula = sales ~ youtube, data = marketing)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.0632 -2.3454 -0.2295 2.4805 8.6548
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.439112 0.549412 15.36 <2e-16 ***
## youtube 0.047537 0.002691 17.67 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.91 on 198 degrees of freedom
## Multiple R-squared: 0.6119, Adjusted R-squared: 0.6099
## F-statistic: 312.1 on 1 and 198 DF, p-value: < 2.2e-16
Variable youtube significativa del modelo <2e-16 *** y explica un 60% la variabilidad de las ventas con r cuadrado
Estimar las ventas para un mes en el cual se invierta en mercado en youtube 65 mil dolares.
predict(mod_you,list(youtube=65),interval = "confidence", level = 0.95)
## fit lwr upr
## 1 11.52899 10.72462 12.33337
11.52899 es el valor que se espera y se mueve entre 10.72462 y 12.33337 con un 95% de confianza
Sirve para evaluar el poder predictivo del modelo, 80 para modelar y estimar y 20 para validar
## segmentar los datos
id_modelar=sample(1:200, size = 160)
marketing_modelar=marketing[id_modelar,]
marketing_validar=marketing[-id_modelar,]
## estimar el modelo set de modelar
mod_you_modelar=lm(sales~youtube,data = marketing_modelar)
##precedir el set de validacion
sales_pred = predict(mod_you_modelar,list(youtube=marketing_validar$youtube))
## Comparar ventas del modelo y realea
sales_real=marketing_validar$sales
error=sales_real-sales_pred
res=data.frame(sales_real,sales_pred,error)
## calcularl el indicador de evaluacion de la predicción
MAE=mean(abs(error))
MAE
## [1] 2.749787
## class(marketing) para validar el tipo de datos
## el modelo no es perfecto puesto que su erroe es
MAE
## [1] 2.749787
head(marketing)
| youtube | newspaper | sales | |
|---|---|---|---|
| 276.12 | 45.36 | 83.04 | 26.52 |
| 53.40 | 47.16 | 54.12 | 12.48 |
| 20.64 | 55.08 | 83.16 | 11.16 |
| 181.80 | 49.56 | 70.20 | 22.20 |
| 216.96 | 12.96 | 70.08 | 15.48 |
| 10.44 | 58.68 | 90.00 | 8.64 |