El siguiente conjunto de datos contiene informacion acerca de las viviendas de Boston y columnas en español, el objetivo es predecir si la construccion de la casa es nueva o no, en función de sus atributos. Incluyen el área total en pies cuadrados, el año de construcción de la casa, el precio promedio del terreno, el área habitable en pies cuadrados, entre otros.
Se pretende utilizar estos atributos para construir un modelo predictivo que pueda estimar de manera efectiva el precio final de una casa, aprovechando las relaciones y patrones presentes en los datos existentes.
library(neuralnet)
library(openxlsx)
library("stats")
library("psych")
## Warning: package 'psych' was built under R version 4.3.2
library("MASS")
library("ISLR")
library("fRegression")
library("vcd")
## Loading required package: grid
##
## Attaching package: 'vcd'
## The following object is masked from 'package:ISLR':
##
## Hitters
library("dplyr")
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
##
## select
## The following object is masked from 'package:neuralnet':
##
## compute
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library("openxlsx")
library("mlbench")
## Warning: package 'mlbench' was built under R version 4.3.2
library("magrittr")
library("neuralnet")
library("keras")
## Warning: package 'keras' was built under R version 4.3.2
library("caret")
## Warning: package 'caret' was built under R version 4.3.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.2
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
## Loading required package: lattice
#file.choose()
data <- read.csv("C:\\Users\\danbr\\OneDrive\\Desktop\\Mineria de Datos\\Actividad Constante - El Diario\\boston_housing_esp.csv")
View(data)
colSums(is.na(data))
## precio metros_totales antiguedad precio_terreno
## 0 0 0 0
## metros_habitables universitarios dormitorios chimenea
## 0 0 0 0
## banyos habitaciones calefaccion consumo_calefacion
## 0 0 0 0
## desague vistas_lago nueva_construccion aire_acondicionado
## 0 0 0 0
str(data)
## 'data.frame': 1728 obs. of 16 variables:
## $ precio : int 132500 181115 109000 155000 86060 120000 153000 170000 90000 122900 ...
## $ metros_totales : num 0.09 0.92 0.19 0.41 0.11 0.68 0.4 1.21 0.83 1.94 ...
## $ antiguedad : int 42 0 133 13 0 31 33 23 36 4 ...
## $ precio_terreno : int 50000 22300 7300 18700 15000 14000 23300 14600 22200 21200 ...
## $ metros_habitables : int 906 1953 1944 1944 840 1152 2752 1662 1632 1416 ...
## $ universitarios : int 35 51 51 51 51 22 51 35 51 44 ...
## $ dormitorios : int 2 3 4 3 2 4 4 4 3 3 ...
## $ chimenea : int 1 0 1 1 0 1 1 1 0 0 ...
## $ banyos : num 1 2.5 1 1.5 1 1 1.5 1.5 1.5 1.5 ...
## $ habitaciones : int 5 6 8 5 3 8 8 9 8 6 ...
## $ calefaccion : chr "electric" "hot water/steam" "hot water/steam" "hot air" ...
## $ consumo_calefacion: chr "electric" "gas" "gas" "gas" ...
## $ desague : chr "septic" "septic" "public/commercial" "septic" ...
## $ vistas_lago : chr "No" "No" "No" "No" ...
## $ nueva_construccion: chr "No" "No" "No" "No" ...
## $ aire_acondicionado: chr "No" "No" "No" "No" ...
Dada la composición de la base de datos, que abarcaba datos numéricos, decimales y de tipo cadena de texto (“chr”), era esencial adecuar los datos de manera apropiada para llevar a cabo el algoritmo de forma precisa. La primera etapa consistió en transformar los datos de tipo “chr” a variables numéricas, con el objetivo de retener la información de estas variables sin perder su contexto. En particular, se aplicó esta transformación a las variables “calefacción, consumo_calefacción, desagüe, vistas_lago y aire_acondicionado”. Este proceso posibilitará una comprensión completa de todas las variables al realizar predicciones. A continuación de la transformación de cada variable, se proporciona una explicación detallada de la interpretación asociada a cada valor asignado.
# Crear un dataframe y Convertir la variable categórica a factor (Asignar valores numéricos (0 y 1))
data2 <- data
data2$calefaccion <- factor(data2$calefaccion)
data2$calefaccion <- as.numeric(data2$calefaccion) - 1
#0 es electric, 1 es hot water/steam y 2 es gas
data2$consumo_calefacion <- factor(data2$consumo_calefacion)
data2$consumo_calefacion <- as.numeric(data2$consumo_calefacion) - 1
#0 es electric, 1 es gas y 2 es oil
data2$desague <- factor(data2$desague)
data2$desague <- as.numeric(data2$desague) - 1
#0 es none, 1 es public/commercial y 2 es septic
data2$vistas_lago <- factor(data2$vistas_lago)
data2$vistas_lago <- as.numeric(data2$vistas_lago) - 1
#0 es No y 1 es Si
data2$nueva_construccion <- factor(data2$nueva_construccion)
data2$nueva_construccion <- as.numeric(data2$nueva_construccion) - 1
#0 es No y 1 es Si
data2$aire_acondicionado <- factor(data2$aire_acondicionado)
data2$aire_acondicionado <- as.numeric(data2$aire_acondicionado) - 1
#0 es No y 1 es Si
# Mostrar el resultado
View(data2)
Se lleva a cabo una nueva verificación para asegurarse de que no haya valores faltantes (NA) y para confirmar que la transformación de las variables se haya realizado correctamente.
colSums(is.na(data2))
## precio metros_totales antiguedad precio_terreno
## 0 0 0 0
## metros_habitables universitarios dormitorios chimenea
## 0 0 0 0
## banyos habitaciones calefaccion consumo_calefacion
## 0 0 0 0
## desague vistas_lago nueva_construccion aire_acondicionado
## 0 0 0 0
str(data2)
## 'data.frame': 1728 obs. of 16 variables:
## $ precio : int 132500 181115 109000 155000 86060 120000 153000 170000 90000 122900 ...
## $ metros_totales : num 0.09 0.92 0.19 0.41 0.11 0.68 0.4 1.21 0.83 1.94 ...
## $ antiguedad : int 42 0 133 13 0 31 33 23 36 4 ...
## $ precio_terreno : int 50000 22300 7300 18700 15000 14000 23300 14600 22200 21200 ...
## $ metros_habitables : int 906 1953 1944 1944 840 1152 2752 1662 1632 1416 ...
## $ universitarios : int 35 51 51 51 51 22 51 35 51 44 ...
## $ dormitorios : int 2 3 4 3 2 4 4 4 3 3 ...
## $ chimenea : int 1 0 1 1 0 1 1 1 0 0 ...
## $ banyos : num 1 2.5 1 1.5 1 1 1.5 1.5 1.5 1.5 ...
## $ habitaciones : int 5 6 8 5 3 8 8 9 8 6 ...
## $ calefaccion : num 0 2 2 1 1 1 2 1 0 1 ...
## $ consumo_calefacion: num 0 1 1 1 1 1 2 2 0 1 ...
## $ desague : num 2 2 1 2 1 2 2 2 2 0 ...
## $ vistas_lago : num 0 0 0 0 0 0 0 0 0 0 ...
## $ nueva_construccion: num 0 0 0 0 1 0 0 0 0 0 ...
## $ aire_acondicionado: num 0 0 0 0 1 0 0 0 0 0 ...
Posteriormente se normalizan los datos de 0 a1 para obtener una mejor convergencia del modelo (evitar atributos con diferentes escalas) y facilitar la comparacion entre atributos. Esto contribuye para que entrenamiento de datos sea más eficiente y con un mejor rendimiento. Se excluye la variable, nueva_construccion, dado que es la variable de interes para realizar las predicciones.
normalize_column <- function(column) {
return((column - min(column)) / (max(column) - min(column)))
}
#Columnas normalizadas
columns_to_normalize <- c("precio", "metros_totales", "antiguedad", "precio_terreno", "metros_habitables","universitarios", "dormitorios", "chimenea", "banyos", "habitaciones", "calefaccion", "consumo_calefacion", "desague", "vistas_lago","aire_acondicionado")
for (column_name in columns_to_normalize) {
data2[[column_name]] <- normalize_column(data2[[column_name]])
}
# Bucle para generar histogramas después de la normalización
for (column_name in columns_to_normalize) {
hist(data2[[column_name]], main = paste("Histograma de ", column_name))
}
#Con estas líneas hacemos que el escoger datos de training sea aleatorio (Dividir datos)
set.seed(50)
inp <- sample(2, nrow(data2), replace = TRUE, prob = c(0.7, 0.3))
training_data <- data2[inp==1, ]
test_data <- data2[inp==2, ]
#Aquí creamos la red neuronal
set.seed(350)
n <- neuralnet(nueva_construccion~.,
data = training_data,
hidden = c(5,3),
err.fct = "ce",
linear.output = TRUE,
lifesign = 'full',
rep = 2,
algorithm = "rprop+",
stepmax = 800000)
## hidden: 5, 3 thresh: 0.01 rep: 1/2 steps: 1000 min thresh: 3.07892732718788
## 2000 min thresh: 1.54222676821163
## 3000 min thresh: 1.02876670053533
## 4000 min thresh: 0.771806057355355
## 5000 min thresh: 0.617555795376303
## 6000 min thresh: 0.514691486902009
## 7000 min thresh: 0.441201888974324
## 8000 min thresh: 0.386076434891923
## 9000 min thresh: 0.343196188491823
## 10000 min thresh: 0.308888908155995
## 11000 min thresh: 0.280817276409596
## 12000 min thresh: 0.257422848174847
## 13000 min thresh: 0.237626567314953
## 14000 min thresh: 0.220657600268084
## 15000 min thresh: 0.205950619816343
## 16000 min thresh: 0.193081598785317
## 17000 min thresh: 0.181726259997772
## 18000 min thresh: 0.171632373476241
## 19000 min thresh: 0.162600800241683
## 20000 min thresh: 0.154472221950248
## 21000 min thresh: 0.147117661413045
## 22000 min thresh: 0.140431588006242
## 23000 min thresh: 0.134326821388559
## 24000 min thresh: 0.12873070909858
## 25000 min thresh: 0.123582221396671
## 26000 min thresh: 0.118829716435317
## 27000 min thresh: 0.114429201992136
## 28000 min thresh: 0.110342969640824
## 29000 min thresh: 0.10653851147236
## 30000 min thresh: 0.102987653446058
## 31000 min thresh: 0.0996658564595915
## 32000 min thresh: 0.0965516484537265
## 33000 min thresh: 0.0936261597594621
## 34000 min thresh: 0.0908727404340527
## 35000 min thresh: 0.0882766431898318
## 36000 min thresh: 0.0858247591629828
## 37000 min thresh: 0.0835053965264096
## 38000 min thresh: 0.0813080940551864
## 39000 min thresh: 0.0792234633716078
## 40000 min thresh: 0.0772430548513183
## 41000 min thresh: 0.0753592431512652
## 42000 min thresh: 0.0735651290893526
## 43000 min thresh: 0.0718544552142015
## 44000 min thresh: 0.0702215328871485
## 45000 min thresh: 0.0686611790858976
## 46000 min thresh: 0.0671686614504966
## 47000 min thresh: 0.0657396503442313
## 48000 min thresh: 0.0643701769063979
## 49000 min thresh: 0.063056596241156
## 50000 min thresh: 0.0617955550233524
## 51000 min thresh: 0.0605839629151851
## 52000 min thresh: 0.0594189672807051
## 53000 min thresh: 0.0582979307626326
## 54000 min thresh: 0.0572184113504807
## 55000 min thresh: 0.0561781446228989
## 56000 min thresh: 0.0551750278925519
## 57000 min thresh: 0.0542071060197825
## 58000 min thresh: 0.0532725586937979
## 59000 min thresh: 0.0523696890071903
## 60000 min thresh: 0.0514969131729188
## 61000 min thresh: 0.0506527512527204
## 62000 min thresh: 0.049835818782671
## 63000 min thresh: 0.0490448191963088
## 64000 min thresh: 0.0482785369580169
## 65000 min thresh: 0.0475358313302323
## 66000 min thresh: 0.0468156307072379
## 67000 min thresh: 0.0461169274563393
## 68000 min thresh: 0.0454387732142219
## 69000 min thresh: 0.0447802745922843
## 70000 min thresh: 0.0441405892500858
## 71000 min thresh: 0.0435189223005799
## 72000 min thresh: 0.0429145230149002
## 73000 min thresh: 0.0423266817979879
## 74000 min thresh: 0.0417547274094103
## 75000 min thresh: 0.0411980244065041
## 76000 min thresh: 0.0406559707893734
## 77000 min thresh: 0.0401279958293893
## 78000 min thresh: 0.0396135580647041
## 79000 min thresh: 0.0391121434480117
## 80000 min thresh: 0.0386232636332155
## 81000 min thresh: 0.0381464543890082
## 82000 min thresh: 0.0376812741285148
## 83000 min thresh: 0.0372273025452386
## 84000 min thresh: 0.0367841393464129
## 85000 min thresh: 0.0363514030757894
## 86000 min thresh: 0.0359287300185226
## 87000 min thresh: 0.0355157731816012
## 88000 min thresh: 0.0351122013437762
## 89000 min thresh: 0.0347176981695496
## 90000 min thresh: 0.0343319613822099
## 91000 min thresh: 0.0339547019914152
## 92000 min thresh: 0.0335856435711293
## 93000 min thresh: 0.0332245215841439
## 94000 min thresh: 0.0328710827497273
## 95000 min thresh: 0.0325250844511946
## 96000 min thresh: 0.0321862941805063
## 97000 min thresh: 0.0318544890172344
## 98000 min thresh: 0.0315294551394073
## 99000 min thresh: 0.0312109873640102
## 1e+05 min thresh: 0.0308988887150536
## 101000 min thresh: 0.0305929700173015
## 102000 min thresh: 0.0302930495138907
## 103000 min thresh: 0.0299989525062337
## 104000 min thresh: 0.0297105110146842
## 105000 min thresh: 0.029427563458602
## 106000 min thresh: 0.0291499543545215
## 107000 min thresh: 0.0288775340312522
## 108000 min thresh: 0.0286101583607912
## 109000 min thresh: 0.0283476885040648
## 110000 min thresh: 0.028089990670529
## 111000 min thresh: 0.0278369358907586
## 112000 min thresh: 0.0275883998012405
## 113000 min thresh: 0.0273442624405615
## 114000 min thresh: 0.0271044080563524
## 115000 min thresh: 0.0268687249222861
## 116000 min thresh: 0.0266371051645497
## 117000 min thresh: 0.0264094445972068
## 118000 min thresh: 0.026185642565944
## 119000 min thresh: 0.025965601799681
## 120000 min thresh: 0.0257492282696121
## 121000 min thresh: 0.0255364310552296
## 122000 min thresh: 0.0253271222169339
## 123000 min thresh: 0.0251212166748697
## 124000 min thresh: 0.0249186320936149
## 125000 min thresh: 0.0247192887724022
## 126000 min thresh: 0.0245231095405768
## 127000 min thresh: 0.0243300196579846
## 128000 min thresh: 0.0241399467200384
## 129000 min thresh: 0.023952820567192
## 130000 min thresh: 0.0237685731985916
## 131000 min thresh: 0.0235871386896913
## 132000 min thresh: 0.0234084531136043
## 133000 min thresh: 0.0232324544660022
## 134000 min thresh: 0.0230590825933838
## 135000 min thresh: 0.0228882791245295
## 136000 min thresh: 0.0227199874049689
## 137000 min thresh: 0.0225541524343388
## 138000 min thresh: 0.0223907208064457
## 139000 min thresh: 0.0222296406519234
## 140000 min thresh: 0.0220708615833523
## 141000 min thresh: 0.0219143346427063
## 142000 min thresh: 0.0217600122510214
## 143000 min thresh: 0.0216078481601824
## 144000 min thresh: 0.0214577974067123
## 145000 min thresh: 0.0213098162674727
## 146000 min thresh: 0.021163862217193
## 147000 min thresh: 0.0210198938877218
## 148000 min thresh: 0.02087787102894
## 149000 min thresh: 0.0207377544712414
## 150000 min thresh: 0.0205995060895206
## 151000 min thresh: 0.0204630887685796
## 152000 min thresh: 0.0203284663699193
## 153000 min thresh: 0.0201956036998092
## 154000 min thresh: 0.0200644664786262
## 155000 min thresh: 0.0199350213113583
## 156000 min thresh: 0.0198072356592578
## 157000 min thresh: 0.0196810778125694
## 158000 min thresh: 0.0195565168642993
## 159000 min thresh: 0.0194335226849702
## 160000 min thresh: 0.019312065898325
## 161000 min thresh: 0.0191921178579353
## 162000 min thresh: 0.0190736506246746
## 163000 min thresh: 0.0189566369450264
## 164000 min thresh: 0.0188410502301782
## 165000 min thresh: 0.018726864535883
## 166000 min thresh: 0.0186140545430389
## 167000 min thresh: 0.0185025955389776
## 168000 min thresh: 0.0183924633994166
## 169000 min thresh: 0.0182836345710496
## 170000 min thresh: 0.0181760860547531
## 171000 min thresh: 0.0180697953893856
## 172000 min thresh: 0.0179647406361435
## 173000 min thresh: 0.0178609003634722
## 174000 min thresh: 0.0177582536324824
## 175000 min thresh: 0.0176567799828824
## 176000 min thresh: 0.0175564594193721
## 177000 min thresh: 0.0174572723985192
## 178000 min thresh: 0.0173591998160572
## 179000 min thresh: 0.0172622229946233
## 180000 min thresh: 0.0171663236718988
## 181000 min thresh: 0.0170714839891428
## 182000 min thresh: 0.0169776864801084
## 183000 min thresh: 0.0168849140603168
## 184000 min thresh: 0.0167931500166815
## 185000 min thresh: 0.0167023779974766
## 186000 min thresh: 0.0166125820026153
## 187000 min thresh: 0.0165237463742555
## 188000 min thresh: 0.0164358557876936
## 189000 min thresh: 0.0163488952425508
## 190000 min thresh: 0.0162628500542395
## 191000 min thresh: 0.0161777058456999
## 192000 min thresh: 0.0160934485393861
## 193000 min thresh: 0.0160100643495089
## 194000 min thresh: 0.0159275397745192
## 195000 min thresh: 0.0158458615898167
## 196000 min thresh: 0.0157650168406854
## 197000 min thresh: 0.0156849928354438
## 198000 min thresh: 0.0156057771388064
## 199000 min thresh: 0.0155273575654341
## 2e+05 min thresh: 0.0154497221736949
## 201000 min thresh: 0.0153728592595933
## 202000 min thresh: 0.0152967573508938
## 203000 min thresh: 0.01522140520141
## 204000 min thresh: 0.0151467917854666
## 205000 min thresh: 0.015072906292517
## 206000 min thresh: 0.0149997381219275
## 207000 min thresh: 0.0149272768779015
## 208000 min thresh: 0.01485551236456
## 209000 min thresh: 0.0147844345811544
## 210000 min thresh: 0.0147140337174272
## 211000 min thresh: 0.0146443001490909
## 212000 min thresh: 0.0145752244334459
## 213000 min thresh: 0.0145067973051186
## 214000 min thresh: 0.0144390096719169
## 215000 min thresh: 0.0143718526108006
## 216000 min thresh: 0.0143053173639706
## 217000 min thresh: 0.0142393953350591
## 218000 min thresh: 0.0141740780854256
## 219000 min thresh: 0.0141093573305595
## 220000 min thresh: 0.0140452249365742
## 221000 min thresh: 0.0139816729167992
## 222000 min thresh: 0.0139186934284672
## 223000 min thresh: 0.0138562787694868
## 224000 min thresh: 0.0137944213752996
## 225000 min thresh: 0.0137331138158275
## 226000 min thresh: 0.0136723487924956
## 227000 min thresh: 0.013612119135337
## 228000 min thresh: 0.0135524178001698
## 229000 min thresh: 0.0134932378658547
## 230000 min thresh: 0.013434572531618
## 231000 min thresh: 0.0133764151144442
## 232000 min thresh: 0.0133187590465427
## 233000 min thresh: 0.0132615978728736
## 234000 min thresh: 0.0132049252487347
## 235000 min thresh: 0.0131487349374199
## 236000 min thresh: 0.0130930208079274
## 237000 min thresh: 0.0130377768327306
## 238000 min thresh: 0.0129829970856047
## 239000 min thresh: 0.0129286757395061
## 240000 min thresh: 0.0128748070645075
## 241000 min thresh: 0.0128213854257841
## 242000 min thresh: 0.0127684052816462
## 243000 min thresh: 0.0127158611816237
## 244000 min thresh: 0.0126637477645967
## 245000 min thresh: 0.0126120597569748
## 246000 min thresh: 0.0125607919709129
## 247000 min thresh: 0.0125099393025804
## 248000 min thresh: 0.0124594967304653
## 249000 min thresh: 0.0124094593137194
## 250000 min thresh: 0.0123598221905491
## 251000 min thresh: 0.0123105805766368
## 252000 min thresh: 0.0122617297636073
## 253000 min thresh: 0.0122132651175223
## 254000 min thresh: 0.0121651820774213
## 255000 min thresh: 0.0121174761538846
## 256000 min thresh: 0.0120701429276389
## 257000 min thresh: 0.0120231780481942
## 258000 min thresh: 0.0119765772325074
## 259000 min thresh: 0.0119303362636819
## 260000 min thresh: 0.0118844509896965
## 261000 min thresh: 0.0118389173221609
## 262000 min thresh: 0.0117937312351027
## 263000 min thresh: 0.011748888763779
## 264000 min thresh: 0.0117043860035193
## 265000 min thresh: 0.0116602191085907
## 266000 min thresh: 0.0116163842910878
## 267000 min thresh: 0.0115728778198554
## 268000 min thresh: 0.0115296960194247
## 269000 min thresh: 0.0114868352689795
## 270000 min thresh: 0.0114442920013463
## 271000 min thresh: 0.0114020627020005
## 272000 min thresh: 0.0113601439081034
## 273000 min thresh: 0.0113185322075513
## 274000 min thresh: 0.0112772242380534
## 275000 min thresh: 0.0112362166862231
## 276000 min thresh: 0.0111955062866933
## 277000 min thresh: 0.0111550898212493
## 278000 min thresh: 0.0111149641179826
## 279000 min thresh: 0.0110751260504578
## 280000 min thresh: 0.0110355725369031
## 281000 min thresh: 0.0109963005394149
## 282000 min thresh: 0.0109573070631792
## 283000 min thresh: 0.0109185891557114
## 284000 min thresh: 0.0108801439061118
## 285000 min thresh: 0.0108419684443321
## 286000 min thresh: 0.0108040599404667
## 287000 min thresh: 0.0107664156040494
## 288000 min thresh: 0.0107290326833706
## 289000 min thresh: 0.0106919084648064
## 290000 min thresh: 0.0106550402721595
## 291000 min thresh: 0.0106184254660217
## 292000 min thresh: 0.0105820614431384
## 293000 min thresh: 0.0105459456357944
## 294000 min thresh: 0.0105100755112096
## 295000 min thresh: 0.0104744485709448
## 296000 min thresh: 0.0104390623503246
## 297000 min thresh: 0.0104039144178668
## 298000 min thresh: 0.0103690023747266
## 299000 min thresh: 0.0103343238541492
## 3e+05 min thresh: 0.0102998765209377
## 301000 min thresh: 0.0102656580709261
## 302000 min thresh: 0.0102316662304679
## 303000 min thresh: 0.0101978987559301
## 304000 min thresh: 0.0101643534332021
## 305000 min thresh: 0.0101310280772092
## 306000 min thresh: 0.0100979205314409
## 307000 min thresh: 0.0100650286674829
## 308000 min thresh: 0.0100323503845635
##
## Warning in log(x): Se han producido NaNs
## Warning: 'err.fct' does not fit 'data' or 'act.fct'
## 308997 error: NaN time: 7.16 mins
## hidden: 5, 3 thresh: 0.01 rep: 2/2 steps: 1000 min thresh: 3.0751631201077
## 2000 min thresh: 1.54128175885913
## 3000 min thresh: 1.02834610755192
## 4000 min thresh: 0.771569307927883
## 5000 min thresh: 0.617404211992262
## 6000 min thresh: 0.514586191133116
## 7000 min thresh: 0.441124513315128
## 8000 min thresh: 0.386017185248659
## 9000 min thresh: 0.343149368485764
## 10000 min thresh: 0.308850980403936
## 11000 min thresh: 0.28078592876112
## 12000 min thresh: 0.257396505762528
## 13000 min thresh: 0.237604120497878
## 14000 min thresh: 0.220638244719286
## 15000 min thresh: 0.205933758306769
## 16000 min thresh: 0.193066778579283
## 17000 min thresh: 0.181713131658048
## 18000 min thresh: 0.171620662999153
## 19000 min thresh: 0.162590289748663
## 20000 min thresh: 0.154462736020119
## 21000 min thresh: 0.147109057222302
## 22000 min thresh: 0.140423748094522
## 23000 min thresh: 0.134319648268687
## 24000 min thresh: 0.128724121184882
## 25000 min thresh: 0.123576149890194
## 26000 min thresh: 0.118824102913569
## 27000 min thresh: 0.114423996524076
## 28000 min thresh: 0.110338129298373
## 29000 min thresh: 0.106533999144377
## 30000 min thresh: 0.102983436885434
## 31000 min thresh: 0.0996619075117007
## 32000 min thresh: 0.0965479424272275
## 33000 min thresh: 0.0936226749096942
## 34000 min thresh: 0.090869457536231
## 35000 min thresh: 0.0882735451843377
## 36000 min thresh: 0.0858218308588976
## 37000 min thresh: 0.0835026243524986
## 38000 min thresh: 0.0813054658496406
## 39000 min thresh: 0.0792209682037058
## 40000 min thresh: 0.0772406828695302
## 41000 min thresh: 0.0753569854532611
## 42000 min thresh: 0.0735629776103819
## 43000 min thresh: 0.0718524026308585
## 44000 min thresh: 0.0702195725341041
## 45000 min thresh: 0.0686593048836079
## 46000 min thresh: 0.0671668678422398
## 47000 min thresh: 0.0657379322410827
## 48000 min thresh: 0.0643685296390289
## 49000 min thresh: 0.063055015517454
## 50000 min thresh: 0.0617940368910899
## 51000 min thresh: 0.0605825037290302
## 52000 min thresh: 0.0594175636729993
## 53000 min thresh: 0.0582965796174447
## 54000 min thresh: 0.0572171097805198
## 55000 min thresh: 0.0561768899489307
## 56000 min thresh: 0.0551738176249678
## 57000 min thresh: 0.0542059378421394
## 58000 min thresh: 0.0532714304480127
## 59000 min thresh: 0.0523685986802264
## 60000 min thresh: 0.0514958588848119
## 61000 min thresh: 0.0506517312457532
## 62000 min thresh: 0.0498348314116069
## 63000 min thresh: 0.0490438629195253
## 64000 min thresh: 0.0482776103294812
## 65000 min thresh: 0.047534932992208
## 66000 min thresh: 0.0468147593836351
## 67000 min thresh: 0.0461160819466856
## 68000 min thresh: 0.0454379523881264
## 69000 min thresh: 0.0447794773844142
## 70000 min thresh: 0.0441398146555391
## 71000 min thresh: 0.0435181693706607
## 72000 min thresh: 0.0429137908532537
## 73000 min thresh: 0.0423259695570319
## 74000 min thresh: 0.0417540342870629
## 75000 min thresh: 0.0411973496431735
## 76000 min thresh: 0.0406553136651804
## 77000 min thresh: 0.0401273556616037
## 78000 min thresh: 0.0396129342053813
## 79000 min thresh: 0.0391115352818034
## 80000 min thresh: 0.0386226705753459
## 81000 min thresh: 0.0381458758833994
## 82000 min thresh: 0.0376807096460488
## 83000 min thresh: 0.0372267515821363
## 84000 min thresh: 0.0367836014227472
## 85000 min thresh: 0.0363508777340784
## 86000 min thresh: 0.0359282168224391
## 87000 min thresh: 0.0355152717147612
## 88000 min thresh: 0.035111711208617
## 89000 min thresh: 0.0347172189862689
## 90000 min thresh: 0.0343314927877955
## 91000 min thresh: 0.0339542436387212
## 92000 min thresh: 0.0335851951280204
## 93000 min thresh: 0.0332240827326934
## 94000 min thresh: 0.0328706531854613
## 95000 min thresh: 0.0325246638823906
## 96000 min thresh: 0.0321858823275391
## 97000 min thresh: 0.0318540856119439
## 98000 min thresh: 0.0315290599245222
## 99000 min thresh: 0.0312106000926051
## 1e+05 min thresh: 0.030898509150027
## 101000 min thresh: 0.0305925979308941
## 102000 min thresh: 0.0302926846872348
## 103000 min thresh: 0.0299985947289152
## 104000 min thresh: 0.0297101600843425
## 105000 min thresh: 0.0294272191805525
## 106000 min thresh: 0.0291496165413886
## 107000 min thresh: 0.028877202502632
## 108000 min thresh: 0.0286098329429297
## 109000 min thresh: 0.0283473690295541
## 110000 min thresh: 0.0280896769780226
## 111000 min thresh: 0.0278366278246967
## 112000 min thresh: 0.0275880972115936
## 113000 min thresh: 0.02734396518259
## 114000 min thresh: 0.0271041159903688
## 115000 min thresh: 0.0268684379134427
## 116000 min thresh: 0.026636823082626
## 117000 min thresh: 0.0264091673164174
## 118000 min thresh: 0.0261853699647488
## 119000 min thresh: 0.0259653337606092
## 120000 min thresh: 0.0257489646790882
## 121000 min thresh: 0.0255361718034188
## 122000 min thresh: 0.0253268671975906
## 123000 min thresh: 0.0251209657851847
## 124000 min thresh: 0.0249183852340852
## 125000 min thresh: 0.0247190458466941
## 126000 min thresh: 0.0245228704554026
## 127000 min thresh: 0.0243297843229848
## 128000 min thresh: 0.0241397150476645
## 129000 min thresh: 0.0239525924725982
## 130000 min thresh: 0.0237683485995364
## 131000 min thresh: 0.023586917506433
## 132000 min thresh: 0.0234082352688064
## 133000 min thresh: 0.0232322398846471
## 134000 min thresh: 0.0230588712026856
## 135000 min thresh: 0.0228880708538489
## 136000 min thresh: 0.0227197821857388
## 137000 min thresh: 0.0225539501999847
## 138000 min thresh: 0.022390521492316
## 139000 min thresh: 0.022229444195221
## 140000 min thresh: 0.0220706679230666
## 141000 min thresh: 0.0219141437195525
## 142000 min thresh: 0.0217598240073786
## 143000 min thresh: 0.0216076625400358
## 144000 min thresh: 0.0214576143555961
## 145000 min thresh: 0.0213096357324206
## 146000 min thresh: 0.0211636841466832
## 147000 min thresh: 0.0210197182316307
## 148000 min thresh: 0.020877697738493
## 149000 min thresh: 0.0207375834989706
## 150000 min thresh: 0.0205993373892172
## 151000 min thresh: 0.0204629222952573
## 152000 min thresh: 0.0203283020797688
## 153000 min thresh: 0.0201954415501669
## 154000 min thresh: 0.0200643064279285
## 155000 min thresh: 0.019934863319113
## 156000 min thresh: 0.0198070796860072
## 157000 min thresh: 0.0196809238198588
## 158000 min thresh: 0.0195563648146436
## 159000 min thresh: 0.0194333725418238
## 160000 min thresh: 0.0193119176260541
## 161000 min thresh: 0.0191919714217871
## 162000 min thresh: 0.0190735059907539
## 163000 min thresh: 0.0189564940802655
## 164000 min thresh: 0.0188409091023154
## 165000 min thresh: 0.0187267251134337
## 166000 min thresh: 0.0186139167952756
## 167000 min thresh: 0.0185024594359075
## 168000 min thresh: 0.018392328911759
## 169000 min thresh: 0.0182835016702137
## 170000 min thresh: 0.0181759547128197
## 171000 min thresh: 0.0180696655790852
## 172000 min thresh: 0.0179646123308406
## 173000 min thresh: 0.0178607735371451
## 174000 min thresh: 0.0177581282597053
## 175000 min thresh: 0.0176566560388081
## 176000 min thresh: 0.017556336879719
## 177000 min thresh: 0.0174571512395507
## 178000 min thresh: 0.0173590800145694
## 179000 min thresh: 0.0172621045279298
## 180000 min thresh: 0.0171662065178142
## 181000 min thresh: 0.0170713681259724
## 182000 min thresh: 0.016977571886633
## 183000 min thresh: 0.0168848007157779
## 184000 min thresh: 0.016793037900773
## 185000 min thresh: 0.0167022670903282
## 186000 min thresh: 0.0166124722847849
## 187000 min thresh: 0.0165236378267141
## 188000 min thresh: 0.0164357483918162
## 189000 min thresh: 0.0163487889801056
## 190000 min thresh: 0.0162627449073785
## 191000 min thresh: 0.0161776017969467
## 192000 min thresh: 0.016093345571627
## 193000 min thresh: 0.0160099624459871
## 194000 min thresh: 0.0159274389188191
## 195000 min thresh: 0.0158457617658593
## 196000 min thresh: 0.0157649180327187
## 197000 min thresh: 0.0156848950280372
## 198000 min thresh: 0.0156056803168366
## 199000 min thresh: 0.0155272617140845
## 2e+05 min thresh: 0.0154496272784418
## 201000 min thresh: 0.0153727653062034
## 202000 min thresh: 0.0152966643254133
## 203000 min thresh: 0.0152213130901611
## 204000 min thresh: 0.0151467005750374
## 205000 min thresh: 0.0150728159697576
## 206000 min thresh: 0.014999648673941
## 207000 min thresh: 0.0149271882920424
## 208000 min thresh: 0.0148554246284227
## 209000 min thresh: 0.0147843476825725
## 210000 min thresh: 0.0147139476444635
## 211000 min thresh: 0.0146442148900352
## 212000 min thresh: 0.0145751399768082
## 213000 min thresh: 0.0145067136396236
## 214000 min thresh: 0.0144389267865001
## 215000 min thresh: 0.0143717704946028
## 216000 min thresh: 0.0143052360063329
## 217000 min thresh: 0.0142393147255196
## 218000 min thresh: 0.0141739982137126
## 219000 min thresh: 0.0141092781865892
## 220000 min thresh: 0.0140451465104455
## 221000 min thresh: 0.0139815951987901
## 222000 min thresh: 0.0139186164090308
## 223000 min thresh: 0.0138562024392452
## 224000 min thresh: 0.0137943457250441
## 225000 min thresh: 0.0137330388365123
## 226000 min thresh: 0.0136722744752337
## 227000 min thresh: 0.0136120454713982
## 228000 min thresh: 0.0135523447809774
## 229000 min thresh: 0.0134931654829802
## 230000 min thresh: 0.0134345007767793
## 231000 min thresh: 0.0133763439795041
## 232000 min thresh: 0.0133186885235024
## 233000 min thresh: 0.0132615279538704
## 234000 min thresh: 0.0132048559260427
## 235000 min thresh: 0.0131486662034428
## 236000 min thresh: 0.0130929526551969
## 237000 min thresh: 0.0130377092539055
## 238000 min thresh: 0.0129829300734652
## 239000 min thresh: 0.0129286092869543
## 240000 min thresh: 0.0128747411645647
## 241000 min thresh: 0.0128213200715848
## 242000 min thresh: 0.0127683404664387
## 243000 min thresh: 0.0127157968987673
## 244000 min thresh: 0.0126636840075603
## 245000 min thresh: 0.012611996519333
## 246000 min thresh: 0.012560729246345
## 247000 min thresh: 0.0125098770848667
## 248000 min thresh: 0.0124594350134871
## 249000 min thresh: 0.0124093980914563
## 250000 min thresh: 0.0123597614570756
## 251000 min thresh: 0.0123105203261237
## 252000 min thresh: 0.0122616699903157
## 253000 min thresh: 0.012213205815806
## 254000 min thresh: 0.0121651232417206
## 255000 min thresh: 0.0121174177787275
## 256000 min thresh: 0.0120700850076398
## 257000 min thresh: 0.0120231205780499
## 258000 min thresh: 0.0119765202069977
## 259000 min thresh: 0.0119302796776666
## 260000 min thresh: 0.011884394838114
## 261000 min thresh: 0.0118388616000278
## 262000 min thresh: 0.0117936759375106
## 263000 min thresh: 0.0117488338858944
## 264000 min thresh: 0.0117043315405826
## 265000 min thresh: 0.0116601650559123
## 266000 min thresh: 0.0116163306440508
## 267000 min thresh: 0.0115728245739102
## 268000 min thresh: 0.0115296431700898
## 269000 min thresh: 0.0114867828118401
## 270000 min thresh: 0.0114442399320524
## 271000 min thresh: 0.0114020110162667
## 272000 min thresh: 0.011360092601707
## 273000 min thresh: 0.0113184812763323
## 274000 min thresh: 0.0112771736779109
## 275000 min thresh: 0.0112361664931162
## 276000 min thresh: 0.0111954564566399
## 277000 min thresh: 0.0111550403503252
## 278000 min thresh: 0.0111149150023189
## 279000 min thresh: 0.0110750772862416
## 280000 min thresh: 0.0110355241203758
## 281000 min thresh: 0.0109962524668706
## 282000 min thresh: 0.0109572593309653
## 283000 min thresh: 0.0109185417602267
## 284000 min thresh: 0.0108800968438051
## 285000 min thresh: 0.0108419217117032
## 286000 min thresh: 0.0108040135340636
## 287000 min thresh: 0.0107663695204681
## 288000 min thresh: 0.0107289869192541
## 289000 min thresh: 0.0106918630168431
## 290000 min thresh: 0.010654995137086
## 291000 min thresh: 0.0106183806406171
## 292000 min thresh: 0.0105820169242273
## 293000 min thresh: 0.0105459014202435
## 294000 min thresh: 0.0105100315959287
## 295000 min thresh: 0.0104744049528863
## 296000 min thresh: 0.0104390190264805
## 297000 min thresh: 0.0104038713852705
## 298000 min thresh: 0.0103689596304509
## 299000 min thresh: 0.0103342813953063
## 3e+05 min thresh: 0.0102998343446778
## 301000 min thresh: 0.0102656161744383
## 302000 min thresh: 0.0102316246109765
## 303000 min thresh: 0.010197857410698
## 304000 min thresh: 0.0101643123595267
## 305000 min thresh: 0.0101309872724243
## 306000 min thresh: 0.0100978799929147
## 307000 min thresh: 0.0100649883926174
## 308000 min thresh: 0.0100323103707948
##
## Warning in log(x): Se han producido NaNs
## Warning in log(x): 'err.fct' does not fit 'data' or 'act.fct'
## 308996 error: NaN time: 4.25 mins
plot(n, rep = 1)
n$result.matrix
## [,1] [,2]
## error NaN NaN
## reached.threshold 9.999981e-03 9.999973e-03
## steps 3.089970e+05 3.089960e+05
## Intercept.to.1layhid1 4.982530e+00 6.259942e+00
## precio.to.1layhid1 7.184836e+00 5.572345e+00
## metros_totales.to.1layhid1 6.878726e+00 5.257815e+00
## antiguedad.to.1layhid1 5.499488e+00 5.110272e+00
## precio_terreno.to.1layhid1 8.384913e+00 4.195687e+00
## metros_habitables.to.1layhid1 6.828599e+00 5.039723e+00
## universitarios.to.1layhid1 5.869398e+00 5.199630e+00
## dormitorios.to.1layhid1 5.676326e+00 5.723168e+00
## chimenea.to.1layhid1 5.982562e+00 3.891654e+00
## banyos.to.1layhid1 6.126169e+00 5.746695e+00
## habitaciones.to.1layhid1 7.389135e+00 7.918020e+00
## calefaccion.to.1layhid1 7.847513e+00 4.763018e+00
## consumo_calefacion.to.1layhid1 7.041115e+00 6.783992e+00
## desague.to.1layhid1 6.857109e+00 5.488970e+00
## vistas_lago.to.1layhid1 5.305859e+00 6.012722e+00
## aire_acondicionado.to.1layhid1 7.149835e+00 5.207177e+00
## Intercept.to.1layhid2 5.378830e+00 6.966801e+00
## precio.to.1layhid2 5.496763e+00 5.071029e+00
## metros_totales.to.1layhid2 5.705149e+00 4.798233e+00
## antiguedad.to.1layhid2 6.647084e+00 6.152356e+00
## precio_terreno.to.1layhid2 6.275654e+00 4.474981e+00
## metros_habitables.to.1layhid2 5.714653e+00 6.097751e+00
## universitarios.to.1layhid2 5.532215e+00 5.562506e+00
## dormitorios.to.1layhid2 6.709478e+00 5.610098e+00
## chimenea.to.1layhid2 7.167540e+00 6.558664e+00
## banyos.to.1layhid2 5.507995e+00 5.035799e+00
## habitaciones.to.1layhid2 4.199854e+00 6.379840e+00
## calefaccion.to.1layhid2 7.817897e+00 4.364430e+00
## consumo_calefacion.to.1layhid2 6.428456e+00 4.675623e+00
## desague.to.1layhid2 4.899607e+00 5.256006e+00
## vistas_lago.to.1layhid2 6.302570e+00 5.356985e+00
## aire_acondicionado.to.1layhid2 5.665519e+00 5.762387e+00
## Intercept.to.1layhid3 6.446017e+00 6.313229e+00
## precio.to.1layhid3 4.136929e+00 5.895468e+00
## metros_totales.to.1layhid3 8.080031e+00 5.837974e+00
## antiguedad.to.1layhid3 5.892638e+00 5.186045e+00
## precio_terreno.to.1layhid3 5.187794e+00 6.690054e+00
## metros_habitables.to.1layhid3 5.504404e+00 5.904151e+00
## universitarios.to.1layhid3 4.852793e+00 5.579601e+00
## dormitorios.to.1layhid3 6.127468e+00 4.789876e+00
## chimenea.to.1layhid3 4.831821e+00 8.177392e+00
## banyos.to.1layhid3 4.026638e+00 5.423634e+00
## habitaciones.to.1layhid3 5.249890e+00 5.271285e+00
## calefaccion.to.1layhid3 6.147508e+00 4.714763e+00
## consumo_calefacion.to.1layhid3 5.165758e+00 6.445685e+00
## desague.to.1layhid3 4.730172e+00 6.057487e+00
## vistas_lago.to.1layhid3 5.509019e+00 7.489152e+00
## aire_acondicionado.to.1layhid3 5.930658e+00 5.060515e+00
## Intercept.to.1layhid4 6.774284e+00 4.551450e+00
## precio.to.1layhid4 8.317263e+00 5.284768e+00
## metros_totales.to.1layhid4 5.906831e+00 4.873763e+00
## antiguedad.to.1layhid4 6.144150e+00 5.497510e+00
## precio_terreno.to.1layhid4 6.376803e+00 4.533164e+00
## metros_habitables.to.1layhid4 6.714303e+00 6.796722e+00
## universitarios.to.1layhid4 4.923151e+00 3.676481e+00
## dormitorios.to.1layhid4 6.273490e+00 6.736311e+00
## chimenea.to.1layhid4 7.388723e+00 4.942937e+00
## banyos.to.1layhid4 5.953604e+00 6.503255e+00
## habitaciones.to.1layhid4 6.221845e+00 5.099541e+00
## calefaccion.to.1layhid4 7.732734e+00 4.547166e+00
## consumo_calefacion.to.1layhid4 5.850645e+00 3.721191e+00
## desague.to.1layhid4 6.359465e+00 5.090402e+00
## vistas_lago.to.1layhid4 4.257111e+00 5.113630e+00
## aire_acondicionado.to.1layhid4 6.135466e+00 6.398439e+00
## Intercept.to.1layhid5 7.837943e+00 5.113238e+00
## precio.to.1layhid5 5.952384e+00 7.267616e+00
## metros_totales.to.1layhid5 5.228906e+00 6.700666e+00
## antiguedad.to.1layhid5 7.990432e+00 7.815100e+00
## precio_terreno.to.1layhid5 7.542649e+00 5.543106e+00
## metros_habitables.to.1layhid5 6.709740e+00 6.515411e+00
## universitarios.to.1layhid5 5.623469e+00 7.267316e+00
## dormitorios.to.1layhid5 6.560307e+00 6.425294e+00
## chimenea.to.1layhid5 7.627453e+00 6.653221e+00
## banyos.to.1layhid5 7.234614e+00 7.955772e+00
## habitaciones.to.1layhid5 8.432252e+00 9.032896e+00
## calefaccion.to.1layhid5 8.241337e+00 7.184379e+00
## consumo_calefacion.to.1layhid5 9.328957e+00 4.864751e+00
## desague.to.1layhid5 6.510811e+00 5.116410e+00
## vistas_lago.to.1layhid5 5.773520e+00 6.348530e+00
## aire_acondicionado.to.1layhid5 7.346007e+00 6.684646e+00
## Intercept.to.2layhid1 5.840154e+00 6.867420e+00
## 1layhid1.to.2layhid1 6.482893e+00 5.772330e+00
## 1layhid2.to.2layhid1 5.884376e+00 4.839478e+00
## 1layhid3.to.2layhid1 6.113227e+00 7.193617e+00
## 1layhid4.to.2layhid1 6.263704e+00 5.374291e+00
## 1layhid5.to.2layhid1 6.225516e+00 6.846064e+00
## Intercept.to.2layhid2 6.000409e+00 6.474449e+00
## 1layhid1.to.2layhid2 6.135781e+00 5.991440e+00
## 1layhid2.to.2layhid2 7.881790e+00 6.106204e+00
## 1layhid3.to.2layhid2 5.298286e+00 6.398302e+00
## 1layhid4.to.2layhid2 5.980997e+00 6.246699e+00
## 1layhid5.to.2layhid2 5.894241e+00 5.727886e+00
## Intercept.to.2layhid3 5.399327e+00 6.393992e+00
## 1layhid1.to.2layhid3 5.414006e+00 6.515561e+00
## 1layhid2.to.2layhid3 7.374425e+00 7.881780e+00
## 1layhid3.to.2layhid3 6.102341e+00 5.200315e+00
## 1layhid4.to.2layhid3 6.131467e+00 4.762649e+00
## 1layhid5.to.2layhid3 6.884506e+00 6.244730e+00
## Intercept.to.nueva_construccion -3.089939e+04 -3.089911e+04
## 2layhid1.to.nueva_construccion -3.089783e+04 -3.090038e+04
## 2layhid2.to.nueva_construccion -3.090071e+04 -3.090029e+04
## 2layhid3.to.nueva_construccion -3.090135e+04 -3.089960e+04
output <- neuralnet::compute(n, rep = 1, training_data[, -1])
head(output$net.result)
## [,1]
## 2 -123599.3
## 3 -123599.3
## 5 -123599.3
## 6 -123599.3
## 7 -123599.3
## 8 -123599.3
View(training_data[1, ])
#Se crea la matrix de confusión con la cual podremos saber que tan buena es nuestra red neuronal
output <- neuralnet::compute(n, rep = 1, training_data[, -1])
p1 <- output$net.result
pred1 <- ifelse(p1 > 0.5, 1, 0)
length(pred1[1])
## [1] 1
length(training_data[1])
## [1] 1
tab1 <- table(pred1, training_data$nueva_construccion)
tab1
##
## pred1 0 1
## 0 1185 51
#Con esta operación obtenemos el porcentaje de error total (usando datos de la matrix de confusión)
1 - sum(diag(tab1)) / sum(tab1)
## [1] 0.04126214
Un modelo eficaz con solo un 4.126% de error en la predicción ha sido desarrollado para determinar si una propiedad es o no una construcción reciente. De los datos totales, se lograron 1185 verdaderos negativos (TN), indicando correctamente que no es una nueva construcción, y solo 51 falsos negativos (FN), donde se clasificó incorrectamente como no una nueva construcción. Este bajo porcentaje de error sugiere una buena capacidad del modelo para distinguir entre las dos clases.