El proyecto final “House Prices - Advanced Regression Techniques” realizado como parte de la competencia en Kaggle. Los temas a tratar son: Predicción, Clasificación, Limpieza y preprocesamiento de datos, Visualización de datos y Clustering.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ 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
library(dplyr)
df <- read.csv("../project/train.csv", stringsAsFactors = TRUE)
Diccionario de datos
SalePrice: Precio de venta de la propiedad en dólares. Esta es la variable objetivo que estás intentando predecir.
MSSubClass: la clase de construcción
MSZoning: La clasificación general de zonificación.
LotFrontage: Pies lineales de calle conectados a la propiedad
LotArea: Tamaño del lote en pies cuadrados
Street: Tipo de acceso por carretera
Alley: Tipo de acceso al callejón
LotShape: Forma general de la propiedad
LandContour: Planitud de la propiedad
Utilities: Tipo de utilidades disponibles
LotConfig: Configuración del lote
LandSlope: pendiente de la propiedad
Neighborhood: Ubicaciones físicas dentro de los límites de la ciudad de Ames
Condition1: Proximidad a la carretera principal o al ferrocarril.
Condition2: Proximidad a la carretera principal o al ferrocarril (si hay una segunda)
BldgType: tipo de vivienda
HouseStyle: estilo de vivienda
OverallQual: Calidad general del material y acabado.
OverallCond: Calificación de condición general
YearBuilt: Fecha de construcción original
YearRemodAdd: Fecha de remodelación
RoofStyle: tipo de techo
RoofMatl: Material del techo
Exterior1st: Revestimiento exterior de la casa
Exterior2nd: Revestimiento exterior de la casa (si hay más de un material)
MasVnrType: Tipo de revestimiento de mampostería
MasVnrArea: Área de revestimiento de mampostería en pies cuadrados
ExterQual: Calidad de los materiales exteriores
ExterCond: PEstado actual del material en el exterior.
Foundation: Tipo de cimentación
BsmtQual: Altura del sótano
BsmtCond: Estado general del sótano.
BsmtExposure: Paredes del sótano a nivel de jardín o de salida
BsmtFinType1: Calidad del área terminada del sótano
BsmtFinSF1: Pies cuadrados terminados tipo 1
BsmtFinType2: Calidad del segundo área terminada (si está presente)
BsmtFinSF2: Pies cuadrados terminados tipo 2
BsmtUnfSF: Pies cuadrados sin terminar de área del sótano
TotalBsmtSF: Pies cuadrados totales de área del sótano
Heating: Tipo de calefacción
HeatingQC: Calidad y estado de la calefacción.
CentralAir: aire acondicionado centralizado
Electrical: Sistema eléctrico
1stFlrSF: Pies cuadrados del primer piso
2ndFlrSF: Pies cuadrados del segundo piso
LowQualFinSF: Pies cuadrados con acabados de baja calidad (todos los pisos)
GrLivArea: Superficie habitable sobre el nivel del suelo (suelo) en pies cuadrados
BsmtFullBath: Baños completos en sótano
BsmtHalfBath: Medios baños del sótano
FullBath: Baños completos sobre el nivel del suelo.
HalfBath: Medios baños sobre el nivel del suelo
Bedroom: Número de dormitorios por encima del nivel del sótano
Kitchen: Número de cocinas
KitchenQual: Calidad de la cocina
TotRmsAbvGrd: Total de habitaciones sobre rasante (no incluye baños)
Functional: Calificación de funcionalidad del hogar
Fireplaces: Número de chimeneas
FireplaceQu: Calidad de la chimenea
GarageType: Ubicación del garaje
GarageYrBlt: Año de construcción del garaje.
GarageFinish: Acabado interior del garaje.
GarageCars: Tamaño del garaje en capacidad de coche.
GarageArea: Tamaño del garaje en pies cuadrados.
GarageQual: Calidad del garaje
GarageCond: Estado del garaje
PavedDrive: Camino pavimentado
WoodDeckSF: Área de cubierta de madera en pies cuadrados
OpenPorchSF: Área abierta del porche en pies cuadrados
EnclosedPorch: Área de porche cerrado en pies cuadrados
3SsnPorch: Área de porche de tres estaciones en pies cuadrados
ScreenPorch: Área del porche cubierto en pies cuadrados
PoolArea: Área de piscina en pies cuadrados
PoolQC: Calidad de la piscina
Fence: Calidad de la valla
MiscFeature: Característica miscelánea no cubierta en otras categorías
MiscVal: $Valor de característica miscelánea
MoSold: Meses de edad
YrSold: Año vendido
SaleType: Tipo de venta
SaleCondition: condición de venta
glimpse(df)
## Rows: 1,460
## Columns: 81
## $ Id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
## $ MSSubClass <int> 60, 20, 60, 70, 60, 50, 20, 60, 50, 190, 20, 60, 20, 20,…
## $ MSZoning <fct> RL, RL, RL, RL, RL, RL, RL, RL, RM, RL, RL, RL, RL, RL, …
## $ LotFrontage <int> 65, 80, 68, 60, 84, 85, 75, NA, 51, 50, 70, 85, NA, 91, …
## $ LotArea <int> 8450, 9600, 11250, 9550, 14260, 14115, 10084, 10382, 612…
## $ Street <fct> Pave, Pave, Pave, Pave, Pave, Pave, Pave, Pave, Pave, Pa…
## $ Alley <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
## $ LotShape <fct> Reg, Reg, IR1, IR1, IR1, IR1, Reg, IR1, Reg, Reg, Reg, I…
## $ LandContour <fct> Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, L…
## $ Utilities <fct> AllPub, AllPub, AllPub, AllPub, AllPub, AllPub, AllPub, …
## $ LotConfig <fct> Inside, FR2, Inside, Corner, FR2, Inside, Inside, Corner…
## $ LandSlope <fct> Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, G…
## $ Neighborhood <fct> CollgCr, Veenker, CollgCr, Crawfor, NoRidge, Mitchel, So…
## $ Condition1 <fct> Norm, Feedr, Norm, Norm, Norm, Norm, Norm, PosN, Artery,…
## $ Condition2 <fct> Norm, Norm, Norm, Norm, Norm, Norm, Norm, Norm, Norm, Ar…
## $ BldgType <fct> 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 2f…
## $ HouseStyle <fct> 2Story, 1Story, 2Story, 2Story, 2Story, 1.5Fin, 1Story, …
## $ OverallQual <int> 7, 6, 7, 7, 8, 5, 8, 7, 7, 5, 5, 9, 5, 7, 6, 7, 6, 4, 5,…
## $ OverallCond <int> 5, 8, 5, 5, 5, 5, 5, 6, 5, 6, 5, 5, 6, 5, 5, 8, 7, 5, 5,…
## $ YearBuilt <int> 2003, 1976, 2001, 1915, 2000, 1993, 2004, 1973, 1931, 19…
## $ YearRemodAdd <int> 2003, 1976, 2002, 1970, 2000, 1995, 2005, 1973, 1950, 19…
## $ RoofStyle <fct> Gable, Gable, Gable, Gable, Gable, Gable, Gable, Gable, …
## $ RoofMatl <fct> CompShg, CompShg, CompShg, CompShg, CompShg, CompShg, Co…
## $ Exterior1st <fct> VinylSd, MetalSd, VinylSd, Wd Sdng, VinylSd, VinylSd, Vi…
## $ Exterior2nd <fct> VinylSd, MetalSd, VinylSd, Wd Shng, VinylSd, VinylSd, Vi…
## $ MasVnrType <fct> BrkFace, None, BrkFace, None, BrkFace, None, Stone, Ston…
## $ MasVnrArea <int> 196, 0, 162, 0, 350, 0, 186, 240, 0, 0, 0, 286, 0, 306, …
## $ ExterQual <fct> Gd, TA, Gd, TA, Gd, TA, Gd, TA, TA, TA, TA, Ex, TA, Gd, …
## $ ExterCond <fct> TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, …
## $ Foundation <fct> PConc, CBlock, PConc, BrkTil, PConc, Wood, PConc, CBlock…
## $ BsmtQual <fct> Gd, Gd, Gd, TA, Gd, Gd, Ex, Gd, TA, TA, TA, Ex, TA, Gd, …
## $ BsmtCond <fct> TA, TA, TA, Gd, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, …
## $ BsmtExposure <fct> No, Gd, Mn, No, Av, No, Av, Mn, No, No, No, No, No, Av, …
## $ BsmtFinType1 <fct> GLQ, ALQ, GLQ, ALQ, GLQ, GLQ, GLQ, ALQ, Unf, GLQ, Rec, G…
## $ BsmtFinSF1 <int> 706, 978, 486, 216, 655, 732, 1369, 859, 0, 851, 906, 99…
## $ BsmtFinType2 <fct> Unf, Unf, Unf, Unf, Unf, Unf, Unf, BLQ, Unf, Unf, Unf, U…
## $ BsmtFinSF2 <int> 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ BsmtUnfSF <int> 150, 284, 434, 540, 490, 64, 317, 216, 952, 140, 134, 17…
## $ TotalBsmtSF <int> 856, 1262, 920, 756, 1145, 796, 1686, 1107, 952, 991, 10…
## $ Heating <fct> GasA, GasA, GasA, GasA, GasA, GasA, GasA, GasA, GasA, Ga…
## $ HeatingQC <fct> Ex, Ex, Ex, Gd, Ex, Ex, Ex, Ex, Gd, Ex, Ex, Ex, TA, Ex, …
## $ CentralAir <fct> Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y,…
## $ Electrical <fct> SBrkr, SBrkr, SBrkr, SBrkr, SBrkr, SBrkr, SBrkr, SBrkr, …
## $ X1stFlrSF <int> 856, 1262, 920, 961, 1145, 796, 1694, 1107, 1022, 1077, …
## $ X2ndFlrSF <int> 854, 0, 866, 756, 1053, 566, 0, 983, 752, 0, 0, 1142, 0,…
## $ LowQualFinSF <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ GrLivArea <int> 1710, 1262, 1786, 1717, 2198, 1362, 1694, 2090, 1774, 10…
## $ BsmtFullBath <int> 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1,…
## $ BsmtHalfBath <int> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ FullBath <int> 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 3, 1, 2, 1, 1, 1, 2, 1,…
## $ HalfBath <int> 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,…
## $ BedroomAbvGr <int> 3, 3, 3, 3, 4, 1, 3, 3, 2, 2, 3, 4, 2, 3, 2, 2, 2, 2, 3,…
## $ KitchenAbvGr <int> 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1,…
## $ KitchenQual <fct> Gd, TA, Gd, Gd, Gd, TA, Gd, TA, TA, TA, TA, Ex, TA, Gd, …
## $ TotRmsAbvGrd <int> 8, 6, 6, 7, 9, 5, 7, 7, 8, 5, 5, 11, 4, 7, 5, 5, 5, 6, 6…
## $ Functional <fct> Typ, Typ, Typ, Typ, Typ, Typ, Typ, Typ, Min1, Typ, Typ, …
## $ Fireplaces <int> 0, 1, 1, 1, 1, 0, 1, 2, 2, 2, 0, 2, 0, 1, 1, 0, 1, 0, 0,…
## $ FireplaceQu <fct> NA, TA, TA, Gd, TA, NA, Gd, TA, TA, TA, NA, Gd, NA, Gd, …
## $ GarageType <fct> Attchd, Attchd, Attchd, Detchd, Attchd, Attchd, Attchd, …
## $ GarageYrBlt <int> 2003, 1976, 2001, 1998, 2000, 1993, 2004, 1973, 1931, 19…
## $ GarageFinish <fct> RFn, RFn, RFn, Unf, RFn, Unf, RFn, RFn, Unf, RFn, Unf, F…
## $ GarageCars <int> 2, 2, 2, 3, 3, 2, 2, 2, 2, 1, 1, 3, 1, 3, 1, 2, 2, 2, 2,…
## $ GarageArea <int> 548, 460, 608, 642, 836, 480, 636, 484, 468, 205, 384, 7…
## $ GarageQual <fct> TA, TA, TA, TA, TA, TA, TA, TA, Fa, Gd, TA, TA, TA, TA, …
## $ GarageCond <fct> TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, …
## $ PavedDrive <fct> Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y,…
## $ WoodDeckSF <int> 0, 298, 0, 0, 192, 40, 255, 235, 90, 0, 0, 147, 140, 160…
## $ OpenPorchSF <int> 61, 0, 42, 35, 84, 30, 57, 204, 0, 4, 0, 21, 0, 33, 213,…
## $ EnclosedPorch <int> 0, 0, 0, 272, 0, 0, 0, 228, 205, 0, 0, 0, 0, 0, 176, 0, …
## $ X3SsnPorch <int> 0, 0, 0, 0, 0, 320, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ ScreenPorch <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 0, 0, 0, 0, …
## $ PoolArea <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ PoolQC <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
## $ Fence <fct> NA, NA, NA, NA, NA, MnPrv, NA, NA, NA, NA, NA, NA, NA, N…
## $ MiscFeature <fct> NA, NA, NA, NA, NA, Shed, NA, Shed, NA, NA, NA, NA, NA, …
## $ MiscVal <int> 0, 0, 0, 0, 0, 700, 0, 350, 0, 0, 0, 0, 0, 0, 0, 0, 700,…
## $ MoSold <int> 2, 5, 9, 2, 12, 10, 8, 11, 4, 1, 2, 7, 9, 8, 5, 7, 3, 10…
## $ YrSold <int> 2008, 2007, 2008, 2006, 2008, 2009, 2007, 2009, 2008, 20…
## $ SaleType <fct> WD, WD, WD, WD, WD, WD, WD, WD, WD, WD, WD, New, WD, New…
## $ SaleCondition <fct> Normal, Normal, Normal, Abnorml, Normal, Normal, Normal,…
## $ SalePrice <int> 208500, 181500, 223500, 140000, 250000, 143000, 307000, …
str(df)
## 'data.frame': 1460 obs. of 81 variables:
## $ Id : int 1 2 3 4 5 6 7 8 9 10 ...
## $ MSSubClass : int 60 20 60 70 60 50 20 60 50 190 ...
## $ MSZoning : Factor w/ 5 levels "C (all)","FV",..: 4 4 4 4 4 4 4 4 5 4 ...
## $ LotFrontage : int 65 80 68 60 84 85 75 NA 51 50 ...
## $ LotArea : int 8450 9600 11250 9550 14260 14115 10084 10382 6120 7420 ...
## $ Street : Factor w/ 2 levels "Grvl","Pave": 2 2 2 2 2 2 2 2 2 2 ...
## $ Alley : Factor w/ 2 levels "Grvl","Pave": NA NA NA NA NA NA NA NA NA NA ...
## $ LotShape : Factor w/ 4 levels "IR1","IR2","IR3",..: 4 4 1 1 1 1 4 1 4 4 ...
## $ LandContour : Factor w/ 4 levels "Bnk","HLS","Low",..: 4 4 4 4 4 4 4 4 4 4 ...
## $ Utilities : Factor w/ 2 levels "AllPub","NoSeWa": 1 1 1 1 1 1 1 1 1 1 ...
## $ LotConfig : Factor w/ 5 levels "Corner","CulDSac",..: 5 3 5 1 3 5 5 1 5 1 ...
## $ LandSlope : Factor w/ 3 levels "Gtl","Mod","Sev": 1 1 1 1 1 1 1 1 1 1 ...
## $ Neighborhood : Factor w/ 25 levels "Blmngtn","Blueste",..: 6 25 6 7 14 12 21 17 18 4 ...
## $ Condition1 : Factor w/ 9 levels "Artery","Feedr",..: 3 2 3 3 3 3 3 5 1 1 ...
## $ Condition2 : Factor w/ 8 levels "Artery","Feedr",..: 3 3 3 3 3 3 3 3 3 1 ...
## $ BldgType : Factor w/ 5 levels "1Fam","2fmCon",..: 1 1 1 1 1 1 1 1 1 2 ...
## $ HouseStyle : Factor w/ 8 levels "1.5Fin","1.5Unf",..: 6 3 6 6 6 1 3 6 1 2 ...
## $ OverallQual : int 7 6 7 7 8 5 8 7 7 5 ...
## $ OverallCond : int 5 8 5 5 5 5 5 6 5 6 ...
## $ YearBuilt : int 2003 1976 2001 1915 2000 1993 2004 1973 1931 1939 ...
## $ YearRemodAdd : int 2003 1976 2002 1970 2000 1995 2005 1973 1950 1950 ...
## $ RoofStyle : Factor w/ 6 levels "Flat","Gable",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ RoofMatl : Factor w/ 8 levels "ClyTile","CompShg",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ Exterior1st : Factor w/ 15 levels "AsbShng","AsphShn",..: 13 9 13 14 13 13 13 7 4 9 ...
## $ Exterior2nd : Factor w/ 16 levels "AsbShng","AsphShn",..: 14 9 14 16 14 14 14 7 16 9 ...
## $ MasVnrType : Factor w/ 4 levels "BrkCmn","BrkFace",..: 2 3 2 3 2 3 4 4 3 3 ...
## $ MasVnrArea : int 196 0 162 0 350 0 186 240 0 0 ...
## $ ExterQual : Factor w/ 4 levels "Ex","Fa","Gd",..: 3 4 3 4 3 4 3 4 4 4 ...
## $ ExterCond : Factor w/ 5 levels "Ex","Fa","Gd",..: 5 5 5 5 5 5 5 5 5 5 ...
## $ Foundation : Factor w/ 6 levels "BrkTil","CBlock",..: 3 2 3 1 3 6 3 2 1 1 ...
## $ BsmtQual : Factor w/ 4 levels "Ex","Fa","Gd",..: 3 3 3 4 3 3 1 3 4 4 ...
## $ BsmtCond : Factor w/ 4 levels "Fa","Gd","Po",..: 4 4 4 2 4 4 4 4 4 4 ...
## $ BsmtExposure : Factor w/ 4 levels "Av","Gd","Mn",..: 4 2 3 4 1 4 1 3 4 4 ...
## $ BsmtFinType1 : Factor w/ 6 levels "ALQ","BLQ","GLQ",..: 3 1 3 1 3 3 3 1 6 3 ...
## $ BsmtFinSF1 : int 706 978 486 216 655 732 1369 859 0 851 ...
## $ BsmtFinType2 : Factor w/ 6 levels "ALQ","BLQ","GLQ",..: 6 6 6 6 6 6 6 2 6 6 ...
## $ BsmtFinSF2 : int 0 0 0 0 0 0 0 32 0 0 ...
## $ BsmtUnfSF : int 150 284 434 540 490 64 317 216 952 140 ...
## $ TotalBsmtSF : int 856 1262 920 756 1145 796 1686 1107 952 991 ...
## $ Heating : Factor w/ 6 levels "Floor","GasA",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ HeatingQC : Factor w/ 5 levels "Ex","Fa","Gd",..: 1 1 1 3 1 1 1 1 3 1 ...
## $ CentralAir : Factor w/ 2 levels "N","Y": 2 2 2 2 2 2 2 2 2 2 ...
## $ Electrical : Factor w/ 5 levels "FuseA","FuseF",..: 5 5 5 5 5 5 5 5 2 5 ...
## $ X1stFlrSF : int 856 1262 920 961 1145 796 1694 1107 1022 1077 ...
## $ X2ndFlrSF : int 854 0 866 756 1053 566 0 983 752 0 ...
## $ LowQualFinSF : int 0 0 0 0 0 0 0 0 0 0 ...
## $ GrLivArea : int 1710 1262 1786 1717 2198 1362 1694 2090 1774 1077 ...
## $ BsmtFullBath : int 1 0 1 1 1 1 1 1 0 1 ...
## $ BsmtHalfBath : int 0 1 0 0 0 0 0 0 0 0 ...
## $ FullBath : int 2 2 2 1 2 1 2 2 2 1 ...
## $ HalfBath : int 1 0 1 0 1 1 0 1 0 0 ...
## $ BedroomAbvGr : int 3 3 3 3 4 1 3 3 2 2 ...
## $ KitchenAbvGr : int 1 1 1 1 1 1 1 1 2 2 ...
## $ KitchenQual : Factor w/ 4 levels "Ex","Fa","Gd",..: 3 4 3 3 3 4 3 4 4 4 ...
## $ TotRmsAbvGrd : int 8 6 6 7 9 5 7 7 8 5 ...
## $ Functional : Factor w/ 7 levels "Maj1","Maj2",..: 7 7 7 7 7 7 7 7 3 7 ...
## $ Fireplaces : int 0 1 1 1 1 0 1 2 2 2 ...
## $ FireplaceQu : Factor w/ 5 levels "Ex","Fa","Gd",..: NA 5 5 3 5 NA 3 5 5 5 ...
## $ GarageType : Factor w/ 6 levels "2Types","Attchd",..: 2 2 2 6 2 2 2 2 6 2 ...
## $ GarageYrBlt : int 2003 1976 2001 1998 2000 1993 2004 1973 1931 1939 ...
## $ GarageFinish : Factor w/ 3 levels "Fin","RFn","Unf": 2 2 2 3 2 3 2 2 3 2 ...
## $ GarageCars : int 2 2 2 3 3 2 2 2 2 1 ...
## $ GarageArea : int 548 460 608 642 836 480 636 484 468 205 ...
## $ GarageQual : Factor w/ 5 levels "Ex","Fa","Gd",..: 5 5 5 5 5 5 5 5 2 3 ...
## $ GarageCond : Factor w/ 5 levels "Ex","Fa","Gd",..: 5 5 5 5 5 5 5 5 5 5 ...
## $ PavedDrive : Factor w/ 3 levels "N","P","Y": 3 3 3 3 3 3 3 3 3 3 ...
## $ WoodDeckSF : int 0 298 0 0 192 40 255 235 90 0 ...
## $ OpenPorchSF : int 61 0 42 35 84 30 57 204 0 4 ...
## $ EnclosedPorch: int 0 0 0 272 0 0 0 228 205 0 ...
## $ X3SsnPorch : int 0 0 0 0 0 320 0 0 0 0 ...
## $ ScreenPorch : int 0 0 0 0 0 0 0 0 0 0 ...
## $ PoolArea : int 0 0 0 0 0 0 0 0 0 0 ...
## $ PoolQC : Factor w/ 3 levels "Ex","Fa","Gd": NA NA NA NA NA NA NA NA NA NA ...
## $ Fence : Factor w/ 4 levels "GdPrv","GdWo",..: NA NA NA NA NA 3 NA NA NA NA ...
## $ MiscFeature : Factor w/ 4 levels "Gar2","Othr",..: NA NA NA NA NA 3 NA 3 NA NA ...
## $ MiscVal : int 0 0 0 0 0 700 0 350 0 0 ...
## $ MoSold : int 2 5 9 2 12 10 8 11 4 1 ...
## $ YrSold : int 2008 2007 2008 2006 2008 2009 2007 2009 2008 2008 ...
## $ SaleType : Factor w/ 9 levels "COD","Con","ConLD",..: 9 9 9 9 9 9 9 9 9 9 ...
## $ SaleCondition: Factor w/ 6 levels "Abnorml","AdjLand",..: 5 5 5 1 5 5 5 5 1 5 ...
## $ SalePrice : int 208500 181500 223500 140000 250000 143000 307000 200000 129900 118000 ...
sum(is.na(df))
## [1] 6965
colSums(is.na(df))
## Id MSSubClass MSZoning LotFrontage LotArea
## 0 0 0 259 0
## Street Alley LotShape LandContour Utilities
## 0 1369 0 0 0
## LotConfig LandSlope Neighborhood Condition1 Condition2
## 0 0 0 0 0
## BldgType HouseStyle OverallQual OverallCond YearBuilt
## 0 0 0 0 0
## YearRemodAdd RoofStyle RoofMatl Exterior1st Exterior2nd
## 0 0 0 0 0
## MasVnrType MasVnrArea ExterQual ExterCond Foundation
## 8 8 0 0 0
## BsmtQual BsmtCond BsmtExposure BsmtFinType1 BsmtFinSF1
## 37 37 38 37 0
## BsmtFinType2 BsmtFinSF2 BsmtUnfSF TotalBsmtSF Heating
## 38 0 0 0 0
## HeatingQC CentralAir Electrical X1stFlrSF X2ndFlrSF
## 0 0 1 0 0
## LowQualFinSF GrLivArea BsmtFullBath BsmtHalfBath FullBath
## 0 0 0 0 0
## HalfBath BedroomAbvGr KitchenAbvGr KitchenQual TotRmsAbvGrd
## 0 0 0 0 0
## Functional Fireplaces FireplaceQu GarageType GarageYrBlt
## 0 0 690 81 81
## GarageFinish GarageCars GarageArea GarageQual GarageCond
## 81 0 0 81 81
## PavedDrive WoodDeckSF OpenPorchSF EnclosedPorch X3SsnPorch
## 0 0 0 0 0
## ScreenPorch PoolArea PoolQC Fence MiscFeature
## 0 0 1453 1179 1406
## MiscVal MoSold YrSold SaleType SaleCondition
## 0 0 0 0 0
## SalePrice
## 0
Crear un nuevo dataset para trabajar los valores faltantes
df2 <- df
Para el tratamiento de los datos faltantes, se aplicarán las siguientes acciones:
1. Se eliminaran las columnas cuyos NA sean mayores a 100: - Fence - MiscFeature - PoolQC - Alley - FireplaceQu - LotFrontage
Eliminar Columnas
df2$Fence <- NULL
df2$MiscFeature <- NULL
df2$PoolQC <- NULL
df2$Alley <- NULL
df2$FireplaceQu <- NULL
df2$LotFrontage <- NULL
Luego de la eliminación de las columnas, quedan 609 datos faltantes en 11 columnas
sum(is.na(df2))
## [1] 609
colSums(is.na(df2))
## Id MSSubClass MSZoning LotArea Street
## 0 0 0 0 0
## LotShape LandContour Utilities LotConfig LandSlope
## 0 0 0 0 0
## Neighborhood Condition1 Condition2 BldgType HouseStyle
## 0 0 0 0 0
## OverallQual OverallCond YearBuilt YearRemodAdd RoofStyle
## 0 0 0 0 0
## RoofMatl Exterior1st Exterior2nd MasVnrType MasVnrArea
## 0 0 0 8 8
## ExterQual ExterCond Foundation BsmtQual BsmtCond
## 0 0 0 37 37
## BsmtExposure BsmtFinType1 BsmtFinSF1 BsmtFinType2 BsmtFinSF2
## 38 37 0 38 0
## BsmtUnfSF TotalBsmtSF Heating HeatingQC CentralAir
## 0 0 0 0 0
## Electrical X1stFlrSF X2ndFlrSF LowQualFinSF GrLivArea
## 1 0 0 0 0
## BsmtFullBath BsmtHalfBath FullBath HalfBath BedroomAbvGr
## 0 0 0 0 0
## KitchenAbvGr KitchenQual TotRmsAbvGrd Functional Fireplaces
## 0 0 0 0 0
## GarageType GarageYrBlt GarageFinish GarageCars GarageArea
## 81 81 81 0 0
## GarageQual GarageCond PavedDrive WoodDeckSF OpenPorchSF
## 81 81 0 0 0
## EnclosedPorch X3SsnPorch ScreenPorch PoolArea MiscVal
## 0 0 0 0 0
## MoSold YrSold SaleType SaleCondition SalePrice
## 0 0 0 0 0
Crear un nuevo dataset para la imputacion de datos
df3 <- df2
2. Se imputaran los valores perdidos (NA)
Instalar libreria
install.packages("missForest")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
Abrir libreria
library(missForest)
df3 <- missForest(df2, verbose = TRUE, variablewise = FALSE)
## missForest iteration 1 in progress...done!
## estimated error(s): 0.0005868666 0.04229095
## difference(s): 4.042076e-09 0.001423937
## time: 16.096 seconds
##
## missForest iteration 2 in progress...done!
## estimated error(s): 0.0005884321 0.04256124
## difference(s): 1.609288e-10 0.0001982696
## time: 15.969 seconds
##
## missForest iteration 3 in progress...done!
## estimated error(s): 0.000595264 0.04254093
## difference(s): 1.005098e-10 0.0001802451
## time: 16.194 seconds
##
## missForest iteration 4 in progress...done!
## estimated error(s): 0.0005911489 0.04217784
## difference(s): 4.118802e-11 0.0001081471
## time: 16.139 seconds
##
## missForest iteration 5 in progress...done!
## estimated error(s): 0.0005928387 0.04197662
## difference(s): 1.083788e-10 0.0001802451
## time: 16.015 seconds
df3$OOBerror
## NRMSE PFC
## 0.0005911489 0.0421778402
convertir la lista a dataframe
df3 <- as.data.frame(df3$ximp)
El nuevo dataset ya no tiene valores perdidos
sum(is.na(df3))
## [1] 0
Escalas de precios
summary(df$SalePrice)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 34900 129975 163000 180921 214000 755000
Para el análisis de la variable dependiente se realizó etiquetas de precios, dependiendo de la escala de precios. Esto ayudará a un análsisi bivariado.
df <- df %>%
mutate(rango_Precio = case_when(
df$SalePrice < 80000 ~ "1. Economica",
df$SalePrice < 129000 ~ "2. Popular",
df$SalePrice < 163000 ~ "3. Tradicional",
df$SalePrice < 180000 ~ "4. Media",
df$SalePrice < 214000 ~ "5. Residencial",
TRUE ~ "6. Residencial plus"
))
table(df$rango_Precio)
##
## 1. Economica 2. Popular 3. Tradicional 4. Media
## 35 314 379 158
## 5. Residencial 6. Residencial plus
## 207 367
rango de precio y Tipo de acceso por carretera
tb_1 <- table(df$rango_Precio, df$Street)
tb_1 <- prop.table(tb_1, 1)
tb_1 <- round(tb_1, digits = 2)
tb_1
##
## Grvl Pave
## 1. Economica 0.03 0.97
## 2. Popular 0.01 0.99
## 3. Tradicional 0.00 1.00
## 4. Media 0.00 1.00
## 5. Residencial 0.00 1.00
## 6. Residencial plus 0.00 1.00
rango de precio y Configuración del lote
tb_2 <- table(df$rango_Precio, df$LotConfig)
tb_2 <- prop.table(tb_2, 1)
tb_2 <- round(tb_2, digits = 2)
tb_2
##
## Corner CulDSac FR2 FR3 Inside
## 1. Economica 0.20 0.00 0.00 0.00 0.80
## 2. Popular 0.18 0.01 0.03 0.00 0.77
## 3. Tradicional 0.18 0.07 0.03 0.00 0.72
## 4. Media 0.18 0.01 0.04 0.00 0.77
## 5. Residencial 0.15 0.11 0.04 0.01 0.70
## 6. Residencial plus 0.19 0.11 0.03 0.00 0.66
rango de precio y Tipo de vivienda
tb_3 <- table(df$rango_Precio, df$BldgType)
tb_3 <- prop.table(tb_3, 1)
tb_3 <- round(tb_3, digits = 2)
tb_3
##
## 1Fam 2fmCon Duplex Twnhs TwnhsE
## 1. Economica 0.91 0.03 0.00 0.03 0.03
## 2. Popular 0.77 0.05 0.07 0.06 0.05
## 3. Tradicional 0.79 0.03 0.06 0.03 0.08
## 4. Media 0.82 0.01 0.01 0.06 0.10
## 5. Residencial 0.84 0.00 0.02 0.01 0.12
## 6. Residencial plus 0.93 0.00 0.00 0.00 0.07
rango de precio y La clasificación general de zonificación
tb_4 <- table(df$rango_Precio, df$MSZoning)
tb_4 <- prop.table(tb_4, 1)
tb_4 <- round(tb_4, digits = 2)
tb_4
##
## C (all) FV RH RL RM
## 1. Economica 0.14 0.00 0.06 0.37 0.43
## 2. Popular 0.01 0.00 0.01 0.59 0.38
## 3. Tradicional 0.00 0.02 0.02 0.79 0.17
## 4. Media 0.00 0.09 0.00 0.86 0.05
## 5. Residencial 0.00 0.07 0.01 0.90 0.01
## 6. Residencial plus 0.00 0.08 0.00 0.90 0.02
rango de precio y estilo de vivienda
tb_5 <- table(df$rango_Precio, df$HouseStyle)
tb_5 <- prop.table(tb_5, 1)
tb_5 <- round(tb_5, digits = 2)
tb_5
##
## 1.5Fin 1.5Unf 1Story 2.5Fin 2.5Unf 2Story SFoyer SLvl
## 1. Economica 0.17 0.03 0.66 0.00 0.00 0.11 0.03 0.00
## 2. Popular 0.21 0.03 0.55 0.01 0.01 0.15 0.03 0.01
## 3. Tradicional 0.13 0.01 0.54 0.00 0.01 0.18 0.06 0.07
## 4. Media 0.06 0.00 0.37 0.01 0.01 0.41 0.03 0.12
## 5. Residencial 0.05 0.00 0.46 0.01 0.00 0.42 0.01 0.05
## 6. Residencial plus 0.04 0.00 0.47 0.01 0.01 0.47 0.00 0.01
tb_6 <- table(df$rango_Precio, df$SaleCondition)
tb_6 <- prop.table(tb_6, 1)
tb_6 <- round(tb_6, digits = 2)
tb_6
##
## Abnorml AdjLand Alloca Family Normal Partial
## 1. Economica 0.23 0.00 0.03 0.00 0.74 0.00
## 2. Popular 0.12 0.01 0.01 0.02 0.82 0.01
## 3. Tradicional 0.07 0.00 0.01 0.02 0.88 0.02
## 4. Media 0.04 0.00 0.01 0.01 0.85 0.09
## 5. Residencial 0.04 0.00 0.01 0.00 0.85 0.10
## 6. Residencial plus 0.04 0.00 0.01 0.01 0.73 0.22