La regresión lineal es un modelo que permite analizar la relación lineal entre una variable dependiente y múltiples variables independientes o explicativas. En esta actividad se utilizará para predecir las ventas semanales de Walmart.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
df <- read.csv("/Users/lishdz/Downloads/Walmart_Store_sales.csv")
df$Date <- as.Date(df$Date, format="%d-%m-%Y")
summary(df)
## Store Date Weekly_Sales Holiday_Flag
## Min. : 1 Min. :2010-02-05 Min. : 209986 Min. :0.00000
## 1st Qu.:12 1st Qu.:2010-10-08 1st Qu.: 553350 1st Qu.:0.00000
## Median :23 Median :2011-06-17 Median : 960746 Median :0.00000
## Mean :23 Mean :2011-06-17 Mean :1046965 Mean :0.06993
## 3rd Qu.:34 3rd Qu.:2012-02-24 3rd Qu.:1420159 3rd Qu.:0.00000
## Max. :45 Max. :2012-10-26 Max. :3818686 Max. :1.00000
## Temperature Fuel_Price CPI Unemployment
## Min. : -2.06 Min. :2.472 Min. :126.1 Min. : 3.879
## 1st Qu.: 47.46 1st Qu.:2.933 1st Qu.:131.7 1st Qu.: 6.891
## Median : 62.67 Median :3.445 Median :182.6 Median : 7.874
## Mean : 60.66 Mean :3.359 Mean :171.6 Mean : 7.999
## 3rd Qu.: 74.94 3rd Qu.:3.735 3rd Qu.:212.7 3rd Qu.: 8.622
## Max. :100.14 Max. :4.468 Max. :227.2 Max. :14.313
str(df)
## 'data.frame': 6435 obs. of 8 variables:
## $ Store : int 1 1 1 1 1 1 1 1 1 1 ...
## $ Date : Date, format: "2010-02-05" "2010-02-12" ...
## $ Weekly_Sales: num 1643691 1641957 1611968 1409728 1554807 ...
## $ Holiday_Flag: int 0 1 0 0 0 0 0 0 0 0 ...
## $ Temperature : num 42.3 38.5 39.9 46.6 46.5 ...
## $ Fuel_Price : num 2.57 2.55 2.51 2.56 2.62 ...
## $ CPI : num 211 211 211 211 211 ...
## $ Unemployment: num 8.11 8.11 8.11 8.11 8.11 ...
df$Year <- as.integer(format(df$Date, "%Y"))
df$Month <- as.integer(format(df$Date, "%m"))
df$Month <- as.integer(format(df$Date, "%d"))
df$WeekYear <- as.integer(format(df$Date, "%W"))
df$WeekDay <- as.integer(format(df$Date, "%u"))
df$Day <- as.integer(format(df$Date, "%d"))
str(df)
## 'data.frame': 6435 obs. of 13 variables:
## $ Store : int 1 1 1 1 1 1 1 1 1 1 ...
## $ Date : Date, format: "2010-02-05" "2010-02-12" ...
## $ Weekly_Sales: num 1643691 1641957 1611968 1409728 1554807 ...
## $ Holiday_Flag: int 0 1 0 0 0 0 0 0 0 0 ...
## $ Temperature : num 42.3 38.5 39.9 46.6 46.5 ...
## $ Fuel_Price : num 2.57 2.55 2.51 2.56 2.62 ...
## $ CPI : num 211 211 211 211 211 ...
## $ Unemployment: num 8.11 8.11 8.11 8.11 8.11 ...
## $ Year : int 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 ...
## $ Month : int 5 12 19 26 5 12 19 26 2 9 ...
## $ WeekYear : int 5 6 7 8 9 10 11 12 13 14 ...
## $ WeekDay : int 5 5 5 5 5 5 5 5 5 5 ...
## $ Day : int 5 12 19 26 5 12 19 26 2 9 ...
regresion <- lm(Weekly_Sales ~., data=df)
summary(regresion)
##
## Call:
## lm(formula = Weekly_Sales ~ ., data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1093737 -382386 -42671 375664 2586331
##
## Coefficients: (3 not defined because of singularities)
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.998e+08 5.234e+07 7.638 2.53e-14 ***
## Store -1.538e+04 5.201e+02 -29.576 < 2e-16 ***
## Date 4.621e+02 7.186e+01 6.430 1.37e-10 ***
## Holiday_Flag 4.573e+04 2.626e+04 1.742 0.0816 .
## Temperature -1.784e+03 3.903e+02 -4.571 4.95e-06 ***
## Fuel_Price 6.525e+04 2.556e+04 2.553 0.0107 *
## CPI -2.103e+03 1.917e+02 -10.972 < 2e-16 ***
## Unemployment -2.225e+04 3.930e+03 -5.662 1.56e-08 ***
## Year -2.014e+05 2.649e+04 -7.601 3.36e-14 ***
## Month -1.693e+03 7.464e+02 -2.268 0.0234 *
## WeekYear NA NA NA NA
## WeekDay NA NA NA NA
## Day NA NA NA NA
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 520800 on 6425 degrees of freedom
## Multiple R-squared: 0.1495, Adjusted R-squared: 0.1483
## F-statistic: 125.5 on 9 and 6425 DF, p-value: < 2.2e-16
df_ajustada <- df %>% select(-Store, -Date, -Fuel_Price, -Year:-Day)
regresion_ajustada <- lm(Weekly_Sales ~., data=df_ajustada)
summary(regresion_ajustada)
##
## Call:
## lm(formula = Weekly_Sales ~ ., data = df_ajustada)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1020421 -477999 -115859 396128 2800875
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1687798.2 52515.7 32.139 < 2e-16 ***
## Holiday_Flag 75760.1 27605.3 2.744 0.00608 **
## Temperature -773.1 393.2 -1.966 0.04930 *
## CPI -1570.0 189.9 -8.267 < 2e-16 ***
## Unemployment -41235.7 3942.0 -10.460 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 557300 on 6430 degrees of freedom
## Multiple R-squared: 0.02538, Adjusted R-squared: 0.02477
## F-statistic: 41.86 on 4 and 6430 DF, p-value: < 2.2e-16
Las regresiones lineales son herramientas sencillas que se pueden utilizar para muchos procesos de negocios, como la predicción de ventas.