Regresion lineal

#Usar FILE.CHOOSE() PARA ENCONTRAR LA DIRECCIÓN DEL DOC (de preferencia en la consola)
data <- read.csv("/Users/eduardojuniormedinahernandez/Downloads/HousePriceData.csv")

str(data)
## 'data.frame':    905 obs. of  10 variables:
##  $ Observation  : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Dist_Taxi    : int  9796 8294 11001 8301 10510 6665 13153 5882 7495 8233 ...
##  $ Dist_Market  : int  5250 8186 14399 11188 12629 5142 11869 9948 11589 7067 ...
##  $ Dist_Hospital: int  10703 12694 16991 12289 13921 9972 17811 13315 13370 11400 ...
##  $ Carpet       : int  1659 1461 1340 1451 1770 1442 1542 1261 1090 1030 ...
##  $ Builtup      : int  1961 1752 1609 1748 2111 1733 1858 1507 1321 1235 ...
##  $ Parking      : chr  "Open" "Not Provided" "Not Provided" "Covered" ...
##  $ City_Category: chr  "CAT B" "CAT B" "CAT A" "CAT B" ...
##  $ Rainfall     : int  530 210 720 620 450 760 1030 1020 680 1130 ...
##  $ House_Price  : int  6649000 3982000 5401000 5373000 4662000 4526000 7224000 3772000 4631000 4415000 ...
summary(data)
##   Observation      Dist_Taxi      Dist_Market    Dist_Hospital  
##  Min.   :  1.0   Min.   :  146   Min.   : 1666   Min.   : 3227  
##  1st Qu.:237.0   1st Qu.: 6477   1st Qu.: 9367   1st Qu.:11302  
##  Median :469.0   Median : 8228   Median :11149   Median :13189  
##  Mean   :468.4   Mean   : 8235   Mean   :11022   Mean   :13091  
##  3rd Qu.:700.0   3rd Qu.: 9939   3rd Qu.:12675   3rd Qu.:14855  
##  Max.   :932.0   Max.   :20662   Max.   :20945   Max.   :23294  
##                                                                 
##      Carpet         Builtup        Parking          City_Category     
##  Min.   :  775   Min.   :  932   Length:905         Length:905        
##  1st Qu.: 1317   1st Qu.: 1579   Class :character   Class :character  
##  Median : 1478   Median : 1774   Mode  :character   Mode  :character  
##  Mean   : 1511   Mean   : 1794                                        
##  3rd Qu.: 1654   3rd Qu.: 1985                                        
##  Max.   :24300   Max.   :12730                                        
##  NA's   :7                                                            
##     Rainfall       House_Price       
##  Min.   :-110.0   Min.   :  1492000  
##  1st Qu.: 600.0   1st Qu.:  4623000  
##  Median : 780.0   Median :  5860000  
##  Mean   : 786.9   Mean   :  6083992  
##  3rd Qu.: 970.0   3rd Qu.:  7200000  
##  Max.   :1560.0   Max.   :150000000  
## 

Limpieza

data <- na.omit(data)

Eliminamos un valor atipico

data <- data[-348, ]

Generación de modelo

#Nivel de confiabilidad = 1-Nivel de significancia 
#El nivel de significacnia sale del simbolo derecho de cada coeficiente y su valor esta en la descripcion
regresion2 <- lm(
  House_Price ~ Dist_Taxi + Dist_Market + Dist_Hospital +
  Carpet + Builtup +
  factor(Parking) + factor(City_Category) +
  Rainfall,
  data = data
)

summary(regresion2)
## 
## Call:
## lm(formula = House_Price ~ Dist_Taxi + Dist_Market + Dist_Hospital + 
##     Carpet + Builtup + factor(Parking) + factor(City_Category) + 
##     Rainfall, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3594178  -837392   -64560   783622  4587323 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                  5.558e+06  3.692e+05  15.052  < 2e-16 ***
## Dist_Taxi                    2.997e+01  2.707e+01   1.107   0.2685    
## Dist_Market                  1.221e+01  2.090e+01   0.584   0.5592    
## Dist_Hospital                5.005e+01  3.024e+01   1.655   0.0983 .  
## Carpet                       9.901e+03  1.431e+02  69.194  < 2e-16 ***
## Builtup                     -7.564e+03  2.418e+02 -31.276  < 2e-16 ***
## factor(Parking)No Parking   -6.219e+05  1.396e+05  -4.456 9.42e-06 ***
## factor(Parking)Not Provided -5.121e+05  1.242e+05  -4.124 4.07e-05 ***
## factor(Parking)Open         -2.641e+05  1.133e+05  -2.331   0.0200 *  
## factor(City_Category)CAT B  -1.883e+06  9.644e+04 -19.522  < 2e-16 ***
## factor(City_Category)CAT C  -2.899e+06  1.064e+05 -27.252  < 2e-16 ***
## Rainfall                    -9.909e+01  1.548e+02  -0.640   0.5224    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1228000 on 885 degrees of freedom
## Multiple R-squared:  0.9429, Adjusted R-squared:  0.9422 
## F-statistic:  1328 on 11 and 885 DF,  p-value: < 2.2e-16

Generar pronósticos

nuevos_datos <- data.frame(
  Dist_Taxi = 8000,
  Dist_Market = 10000,
  Dist_Hospital = 12000,
  Carpet = 1500,
  Builtup = 1800,
  Parking = "Covered",
  City_Category = "CAT A",
  Rainfall = 800
)

predict(regresion2, nuevos_datos)
##       1 
## 7676854

Conclusiones

El análisis de regresión muestra que el precio de la vivienda depende principalmente del tamaño del inmueble, la disponibilidad de estacionamiento y la categoría de la ciudad. En contraste, factores como la distancia a servicios y la lluvia tienen poca influencia en el valor de la propiedad. El alto R² indica que el modelo explica adecuadamente el comportamiento de los precios, por lo que puede considerarse confiable para predicción.