require (ggplot2)
## Loading required package: ggplot2
require(plotly)
## Loading required package: plotly
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
require(datarium)
## Loading required package: datarium
data("marketing")
head(marketing)
##   youtube facebook newspaper sales
## 1  276.12    45.36     83.04 26.52
## 2   53.40    47.16     54.12 12.48
## 3   20.64    55.08     83.16 11.16
## 4  181.80    49.56     70.20 22.20
## 5  216.96    12.96     70.08 15.48
## 6   10.44    58.68     90.00  8.64
promedio=mean(marketing$sales)
desviacion=sd(marketing$sales)

data.frame(promedio,desviacion)
##   promedio desviacion
## 1   16.827   6.260948
g1 = ggplot(data = marketing, mapping = aes(x = sales)) + 
  geom_histogram(fill = "purple") + 
  theme_bw()

ggplotly(g1)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
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'
cor(marketing$newspaper, marketing$sales)
## [1] 0.228299
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'
cor(marketing$facebook, marketing$sales)
## [1] 0.5762226
g4=ggplot(data=marketing,mapping = aes(x=youtube,y=sales))+geom_point()+theme_bw()+geom_smooth(method = "lm")
ggplotly(g4)
## `geom_smooth()` using formula = 'y ~ x'
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
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
predict(mod_you,list (youtube=65),interval = "confidence",level = 0.95)
##        fit      lwr      upr
## 1 11.52899 10.72462 12.33337
##Paso 1 ~ Segmentar Los Datos
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)

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

Interpretación

B0 = intercepto: 8,43 –> En promedio, las ventas serán 8,42 mill USD si no hay inversión publicidasd youtube

B1 = Pendiente = 0,04 –> Por un millón dolar adicional invirtiendo en publicidad youtube, las ventas aumentan 0,04 millones USD