install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages("plotly")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
install.packages("datarium")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
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
##Exploración de las ventas (sales)
promedio=mean(marketing$sales) #promedio / media
desviación=sd(marketing$sales) #desviación estandar
data.frame(promedio,desviación)
## promedio desviación
## 1 16.827 6.260948
g1=ggplot(data = marketing,mapping = aes(x=sales))+geom_histogram(fill="skyblue")+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
g2=ggplot(data=marketing,mapping = aes(x=facebook,y=sales))+geom_point()+theme_bw()+
geom_smooth()
ggplotly(g2)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
correlacionF = cor(marketing$facebook,marketing$sales)
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
##Estimación del Modelo
mod_face=lm(sales~facebook,data=marketing)
mod_face
##
## Call:
## lm(formula = sales ~ facebook, data = marketing)
##
## Coefficients:
## (Intercept) facebook
## 11.1740 0.2025
#sales=11.1740 +(0.2025*facebook)
##Interpretación del summary del modelo
summary(mod_face)
##
## 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
##Predecir con el Modelo #####Estimar las ventas para un mes en el cual se invierta en mercadeo en facebook 65 mil dólares.
predict(mod_face,list(facebook=65),interval = "confidence",level = 0.95)
## fit lwr upr
## 1 24.33619 22.68098 25.9914
id_modelar=sample(1:200,size = 160)
marketing_modelar=marketing[id_modelar,]
marketing_validar=marketing[-id_modelar,]
mod_face_modelar=lm(sales~facebook,data=marketing_modelar)
sales_pred=predict(mod_face_modelar,list(facebook=marketing_validar$facebook))
sales_real=marketing_validar$sales
error=sales_real-sales_pred
res=data.frame(sales_real,sales_pred,error)
MAE=mean(abs(error)) #Mean Absolut Error (Error Medio Absoluto)
MAE
## [1] 3.832691