Instalar paquetes y llamar librerías

#install.packages("tidyverse")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── 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

Importar la base de datos

df <- read.csv("C:\\Users\\Usuario\\Documents\\IAConcentración\\M2\\Walmart\\walmart.csv")

Entender y limpiar la base de datos

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 ...

Agregar variables a la base de datos

df$Year <- format(df$Date, "%Y")
df$Year <- as.integer(df$Year)
df$Month <- format(df$Date, "%m")
df$Month <- as.integer(df$Month)
# df$WeekYear <- format(df$Date, "%W") # Iniciando en lunes
# df$WeekYear <- as.integer(df$WeekYear)
# df$WeekDay <- format(df$Date, "%u") # Iniciando en lunes
# df$WeekDay <- as.integer(df$WeekDay)
df$Day <- format(df$Date, "%d")
df$Day <- as.integer(df$Day)
str(df)
## 'data.frame':    6435 obs. of  11 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  2 2 2 2 3 3 3 3 4 4 ...
##  $ Day         : int  5 12 19 26 5 12 19 26 2 9 ...
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  
##       Year          Month             Day       
##  Min.   :2010   Min.   : 1.000   Min.   : 1.00  
##  1st Qu.:2010   1st Qu.: 4.000   1st Qu.: 8.00  
##  Median :2011   Median : 6.000   Median :16.00  
##  Mean   :2011   Mean   : 6.448   Mean   :15.68  
##  3rd Qu.:2012   3rd Qu.: 9.000   3rd Qu.:23.00  
##  Max.   :2012   Max.   :12.000   Max.   :31.00

Generar regresión lineal

regresion <- lm(Weekly_Sales ~., data=df)
summary(regresion)
## 
## Call:
## lm(formula = Weekly_Sales ~ ., data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1094800  -382464   -42860   375406  2587123 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -2.384e+09  9.127e+09  -0.261   0.7940    
## Store        -1.538e+04  5.202e+02 -29.576  < 2e-16 ***
## Date         -3.399e+03  1.266e+04  -0.268   0.7883    
## Holiday_Flag  4.773e+04  2.706e+04   1.763   0.0779 .  
## Temperature  -1.817e+03  4.053e+02  -4.484 7.47e-06 ***
## Fuel_Price    6.124e+04  2.876e+04   2.130   0.0332 *  
## CPI          -2.109e+03  1.928e+02 -10.941  < 2e-16 ***
## Unemployment -2.209e+04  3.967e+03  -5.569 2.67e-08 ***
## Year          1.212e+06  4.633e+06   0.262   0.7937    
## Month         1.177e+05  3.858e+05   0.305   0.7604    
## Day           2.171e+03  1.269e+04   0.171   0.8642    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 520900 on 6424 degrees of freedom
## Multiple R-squared:  0.1495, Adjusted R-squared:  0.1482 
## F-statistic:   113 on 10 and 6424 DF,  p-value: < 2.2e-16

Ajustar regresión lineal

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