##Árbol de decisión
Se agregan las librerias a utilizar
library(tree)
## Warning: package 'tree' was built under R version 4.3.1
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Selección de la base de datos
library(readr)
DailyDelhiClimateTrain <- read_csv("DailyDelhiClimateTrain.csv")
## Rows: 1462 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): date
## dbl (4): meantemp, humidity, wind_speed, meanpressure
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(DailyDelhiClimateTrain)
DB_C <- DailyDelhiClimateTrain
Limpieza de base de datos
datos_sin_nulos <- na.omit(DB_C)
DB_C1<- datos_sin_nulos
# Selección muestra entrenamiento de un 70%
train=sample(seq(length(DB_C1$meantemp)),length(DB_C1$humidity)*0.7,replace=FALSE)
head(DB_C1)
## # A tibble: 6 × 5
## date meantemp humidity wind_speed meanpressure
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 51/51/2513 15 84.5 5 1516.
## 2 52/51/2513 7.4 92 2.98 1518.
## 3 53/51/2513 7.17 87 4.63 1519.
## 4 54/51/2513 8.67 71.3 1.23 1517.
## 5 55/51/2513 6 86.8 3.7 1516.
## 6 56/51/2513 7 82.8 1.48 1518
tail(DB_C1)
## # A tibble: 6 × 5
## date meantemp humidity wind_speed meanpressure
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 27/12/2516 16.8 67.6 8.34 1517.
## 2 28/12/2516 17.2 68.5 3.55 1516.
## 3 29/12/2516 15.2 87.9 6 1517.
## 4 35/12/2516 14.6 89.7 6.27 1518.
## 5 31/12/2516 15.6 87 7.32 1516.
## 6 51/51/2517 15 155 5 1516
unique(DB_C1$meantemp)
## [1] 15.000000 7.400000 7.166667 8.666667 6.000000 7.000000 8.857143
## [8] 14.000000 11.000000 15.714286 15.833333 12.833333 14.714286 13.833333
## [15] 16.500000 12.500000 11.285714 11.200000 9.500000 12.250000 12.666667
## [22] 12.857143 14.833333 14.125000 16.200000 16.000000 16.285714 18.000000
## [29] 17.428571 16.625000 16.666667 15.600000 15.428571 15.250000 15.875000
## [36] 15.333333 17.333333 19.166667 14.428571 13.666667 15.857143 17.714286
## [43] 25.000000 25.500000 16.857143 16.875000 17.857143 25.800000 19.428571
## [50] 19.000000 19.333333 17.600000 25.875000 25.857143 23.428571 24.166667
## [57] 25.428571 23.142857 24.000000 23.500000 21.500000 22.333333 25.333333
## [64] 22.666667 22.500000 29.166667 23.833333 25.250000 27.375000 27.000000
## [71] 24.142857 21.000000 22.428571 21.250000 23.200000 25.375000 25.166667
## [78] 26.200000 24.600000 25.600000 29.142857 28.714286 35.166667 35.000000
## [85] 28.857143 35.200000 28.250000 32.125000 29.200000 35.285714 28.285714
## [92] 35.625000 27.666667 28.625000 31.142857 29.875000 35.571429 31.857143
## [99] 29.833333 28.571429 32.857143 32.625000 32.750000 32.875000 34.500000
## [106] 34.285714 34.000000 35.750000 29.857143 31.714286 32.285714 33.000000
## [113] 32.833333 31.400000 35.333333 36.400000 36.000000 36.750000 37.500000
## [120] 38.428571 38.714286 37.800000 35.857143 34.142857 32.200000 33.625000
## [127] 32.000000 32.400000 35.600000 37.166667 31.285714 34.200000 36.166667
## [134] 36.625000 35.142857 35.714286 26.875000 28.400000 34.833333 33.142857
## [141] 35.666667 31.428571 31.500000 33.250000 33.857143 31.571429 32.375000
## [148] 32.800000 31.000000 31.166667 29.000000 29.750000 32.500000 28.875000
## [155] 31.750000 31.125000 35.500000 26.833333 28.200000 29.500000 29.714286
## [162] 28.333333 28.833333 27.166667 28.375000 28.500000 29.428571 29.666667
## [169] 27.714286 26.600000 27.428571 28.166667 32.142857 31.666667 35.833333
## [176] 28.000000 31.625000 29.250000 29.800000 28.666667 25.200000 28.142857
## [183] 26.857143 28.600000 24.833333 23.857143 26.142857 26.428571 26.333333
## [190] 24.571429 24.333333 24.875000 27.800000 22.800000 23.000000 22.875000
## [197] 22.857143 23.666667 21.625000 25.625000 21.166667 18.833333 25.571429
## [204] 25.142857 19.857143 16.833333 18.857143 16.571429 17.750000 17.625000
## [211] 17.000000 15.571429 18.500000 17.875000 18.250000 18.142857 19.125000
## [218] 18.625000 17.250000 17.500000 17.142857 16.125000 15.500000 16.142857
## [225] 14.750000 14.875000 15.375000 14.250000 13.500000 12.125000 11.875000
## [232] 12.375000 14.500000 13.375000 12.875000 11.428571 12.142857 12.000000
## [239] 12.285714 14.625000 13.571429 15.625000 13.875000 11.750000 14.375000
## [246] 18.428571 19.625000 13.625000 12.625000 13.250000 13.000000 15.125000
## [253] 17.125000 16.375000 18.125000 18.750000 18.875000 18.375000 19.875000
## [260] 22.000000 25.714286 23.125000 26.250000 24.857143 21.750000 22.750000
## [267] 22.375000 24.625000 25.125000 24.428571 24.125000 26.750000 35.125000
## [274] 27.250000 25.750000 26.375000 29.125000 27.625000 27.125000 24.250000
## [281] 29.375000 34.875000 34.375000 31.875000 29.571429 31.375000 29.625000
## [288] 34.625000 35.250000 33.125000 36.875000 38.500000 37.625000 37.875000
## [295] 37.250000 33.875000 36.375000 38.000000 32.250000 34.750000 31.388889
## [302] 34.755882 36.125000 35.875000 29.235294 35.375000 35.428571 34.125000
## [309] 27.875000 26.500000 31.250000 28.750000 26.571429 26.000000 24.500000
## [316] 23.750000 24.750000 23.875000 23.375000 23.625000 21.375000 19.250000
## [323] 19.375000 19.750000 15.285714 11.250000 9.375000 9.625000 9.875000
## [330] 9.250000 15.750000 9.000000 11.125000 11.625000 13.750000 11.714286
## [337] 11.500000 12.750000 17.375000 16.750000 21.142857 21.875000 22.142857
## [344] 16.250000 19.500000 21.125000 26.625000 23.571429 24.375000 28.125000
## [351] 27.750000 36.250000 37.375000 37.750000 33.750000 34.250000 33.375000
## [358] 36.500000 29.593750 27.500000 22.125000 13.125000 18.266667 15.562500
## [365] 13.600000 13.266667 12.357143 12.566667 12.187500 11.733333 14.437500
## [372] 11.187500 11.666667 14.562500 17.583333 19.562500 15.846154 15.266667
## [379] 16.363636 17.595960 17.769236 18.133333 19.687500 19.200000 17.566667
## [386] 17.642857 18.214286 18.714286 19.466667 23.153846 21.428571 21.687500
## [393] 22.562500 24.916667 24.933333 27.312500 23.933333 22.812500 23.714286
## [400] 25.562500 25.566667 24.562500 24.566667 23.937500 26.312500 26.187500
## [407] 26.785714 27.133333 28.133333 24.666667 25.933333 32.312500 33.312500
## [414] 32.812500 29.933333 29.266667 35.733333 35.687500 34.666667 34.562500
## [421] 31.916667 31.312500 33.437500 34.153846 34.571429 33.562500 34.687500
## [428] 33.714286 31.437500 35.133333 33.533333 37.294118 36.562500 37.214286
## [435] 37.400000 36.133333 36.800000 32.214286 31.526316 33.217391 35.269236
## [442] 38.272727 26.812500 32.642857 37.562500 38.200000 36.571429 34.733333
## [449] 33.500000 34.187500 35.937500 33.846154 36.437500 34.866667 34.312500
## [456] 35.785714 35.466667 29.562500 33.266667 35.800000 32.562500 35.437500
## [463] 34.133333 31.937500 35.312500 28.312500 29.533333 27.333333 33.111111
## [470] 33.800000 35.566667 33.117648 33.859524 31.615385 28.157143 29.535714
## [477] 35.321429 28.933333 31.678571 31.333333 29.928571 29.888889 32.571429
## [484] 33.185185 31.592593 32.185185 31.480000 35.178571 31.520000 31.222222
## [491] 31.785714 33.400000 35.540000 27.259259 27.960000 35.739135 35.894737
## [498] 31.692358 31.576924 31.100000 35.555556 31.235769 31.642857 32.533333
## [505] 31.727273 32.357692 33.444444 33.360000 35.537538 31.240000 31.135435
## [512] 32.440000 32.227273 32.541667 32.814815 33.269236 35.753754 35.960000
## [519] 35.920000 29.777778 29.962963 27.745746 28.428571 28.925926 29.576924
## [526] 28.459596 29.333333 28.540000 27.576924 26.555556 25.518519 25.814815
## [533] 24.826587 24.538462 24.384615 23.727273 25.640000 24.814815 23.115385
## [540] 22.925926 24.545455 23.735769 23.518519 23.920000 23.538462 24.296296
## [547] 23.346154 22.240000 21.769236 21.735769 25.666667 22.250000 21.538462
## [554] 22.578947 22.826587 21.421553 23.600000 24.294118 23.636364 22.454545
## [561] 21.611111 19.869565 19.258333 21.258333 18.900000 18.636364 18.538462
## [568] 16.900000 19.416667 16.444444 25.541667 19.959596 19.550000 18.555556
## [575] 18.166667 16.583333 19.800000 18.550000 17.285714 15.550000 17.318182
## [582] 16.850000 17.217391 15.238595 14.595239 15.552632
Creación del árbol
DB.tree = tree(DB_C1$meantemp~.,DB_C1,subset=train)
## Warning in tree(DB_C1$meantemp ~ ., DB_C1, subset = train): NAs introducidos
## por coerción
summary(DB.tree)
##
## Regression tree:
## tree(formula = DB_C1$meantemp ~ ., data = DB_C1, subset = train)
## Variables actually used in tree construction:
## [1] "meanpressure" "humidity"
## Number of terminal nodes: 6
## Residual mean deviance: 11.99 = 12190 / 1017
## Distribution of residuals:
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -13.51000 -2.28000 -0.09975 0.00000 2.27500 12.41000
plot(DB.tree);text(DB.tree,pretty=3)
Ver los valores del árbol
DB.tree
## node), split, n, deviance, yval
## * denotes terminal node
##
## 1) root 1023 59790 26.03
## 2) meanpressure < 1538.44 647 40210 23.14
## 4) meanpressure < 1177.63 185 1638 33.22 *
## 5) meanpressure > 1177.63 462 12210 19.10
## 10) meanpressure < 1515.94 254 5325 21.74
## 20) humidity < 68.0625 175 2656 23.42 *
## 21) humidity > 68.0625 79 1081 18.02 *
## 11) meanpressure > 1515.94 208 2945 15.87 *
## 3) meanpressure > 1538.44 376 4818 31.02
## 6) meanpressure < 1557.48 297 2498 31.84 *
## 7) meanpressure > 1557.48 79 1371 27.94 *