Regresión Lineal
Las librerías
library(readr)
## Warning: package 'readr' was built under R version 3.6.2
library(dplyr) # Para filter,select...
## Warning: package 'dplyr' was built under R version 3.6.2
##
## 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
library(stats) # Para regresion lineal
library(ggplot2) # Para graficar datos mas potentes.
## Warning: package 'ggplot2' was built under R version 3.6.2
Cargar los datos
- datos de entrenamiento train.csv
- datos de validación test.csv
entrena <- read.csv("~/Mis clases ITD/Semestre Enero Junio 2020/Analisis Inteligente de Datos/datos/random-linear-regression/train.csv")
entrena
## x y
## 1 24.000 21.5494520
## 2 50.000 47.4644630
## 3 15.000 17.2186563
## 4 38.000 36.5863980
## 5 87.000 87.2889839
## 6 36.000 32.4638749
## 7 12.000 10.7808968
## 8 81.000 80.7633986
## 9 25.000 24.6121515
## 10 5.000 6.9633191
## 11 16.000 11.2375734
## 12 16.000 13.5329021
## 13 24.000 24.6032390
## 14 39.000 39.4004998
## 15 54.000 48.4375384
## 16 60.000 61.6990032
## 17 26.000 26.9283242
## 18 73.000 70.4052055
## 19 29.000 29.3409241
## 20 31.000 25.3089519
## 21 68.000 69.0293434
## 22 87.000 84.9948470
## 23 58.000 57.0431030
## 24 54.000 50.5921991
## 25 84.000 83.0277220
## 26 58.000 57.0575271
## 27 49.000 47.9588334
## 28 20.000 24.3422643
## 29 90.000 94.6848828
## 30 48.000 48.0397070
## 31 4.000 7.0813234
## 32 25.000 21.9923991
## 33 42.000 42.3315166
## 34 0.000 0.3290894
## 35 60.000 61.9230370
## 36 93.000 91.1771642
## 37 39.000 39.4535801
## 38 7.000 5.9960696
## 39 21.000 22.5901594
## 40 68.000 61.1804441
## 41 84.000 85.0277896
## 42 0.000 -1.2863109
## 43 58.000 61.9427396
## 44 19.000 21.9603335
## 45 36.000 33.6619419
## 46 19.000 17.6094624
## 47 59.000 58.5630564
## 48 51.000 52.8239076
## 49 19.000 22.1363481
## 50 33.000 35.0746735
## 51 85.000 86.1882231
## 52 44.000 42.6322770
## 53 5.000 4.0981774
## 54 59.000 61.2229864
## 55 14.000 17.7067758
## 56 9.000 11.8531257
## 57 75.000 80.2305169
## 58 69.000 62.6493174
## 59 10.000 9.6168598
## 60 17.000 20.0279770
## 61 58.000 61.7510743
## 62 74.000 71.6101030
## 63 21.000 23.7715462
## 64 51.000 51.9014204
## 65 19.000 22.6607368
## 66 50.000 50.0289793
## 67 24.000 26.6879437
## 68 0.000 0.3769119
## 69 12.000 6.8064190
## 70 75.000 77.3398600
## 71 21.000 28.9026021
## 72 64.000 66.7346608
## 73 5.000 0.7075106
## 74 58.000 57.0774838
## 75 32.000 28.4145320
## 76 41.000 44.4627212
## 77 7.000 7.4596060
## 78 4.000 2.3167081
## 79 5.000 4.9285462
## 80 49.000 52.5033607
## 81 90.000 91.1910962
## 82 3.000 8.4891643
## 83 11.000 6.9633720
## 84 32.000 31.9798996
## 85 83.000 81.4281205
## 86 25.000 22.6236542
## 87 83.000 78.5250509
## 88 26.000 25.8071406
## 89 76.000 73.5108178
## 90 95.000 91.7754670
## 91 53.000 49.2186352
## 92 77.000 80.5044539
## 93 42.000 50.0563612
## 94 25.000 25.4629255
## 95 54.000 55.3216426
## 96 55.000 59.1244888
## 97 0.000 1.1006867
## 98 73.000 71.9802079
## 99 35.000 30.1366641
## 100 86.000 83.8842741
## 101 90.000 89.9100475
## 102 13.000 8.3356546
## 103 46.000 47.8838896
## 104 46.000 45.0039741
## 105 32.000 31.1566457
## 106 8.000 9.1903757
## 107 71.000 74.8313500
## 108 28.000 30.2317761
## 109 24.000 24.2191403
## 110 56.000 57.8721915
## 111 49.000 50.6172839
## 112 79.000 78.6747004
## 113 90.000 86.2367070
## 114 89.000 89.1040926
## 115 41.000 43.2659508
## 116 27.000 26.6827328
## 117 58.000 59.4638304
## 118 26.000 28.9005583
## 119 31.000 31.3004160
## 120 70.000 71.1433266
## 121 71.000 68.4739206
## 122 39.000 39.9823886
## 123 7.000 4.0757761
## 124 48.000 47.8581754
## 125 56.000 51.2039022
## 126 45.000 43.9367213
## 127 41.000 38.1362668
## 128 3.000 3.5746616
## 129 37.000 36.4139958
## 130 24.000 22.2190852
## 131 68.000 63.5312572
## 132 47.000 49.8670279
## 133 27.000 21.5314001
## 134 68.000 64.0571023
## 135 74.000 70.7754984
## 136 95.000 92.1574976
## 137 79.000 81.2225916
## 138 21.000 25.1011407
## 139 95.000 94.0885340
## 140 54.000 53.2516617
## 141 56.000 59.1623662
## 142 80.000 75.2414843
## 143 26.000 28.2232583
## 144 25.000 25.3332373
## 145 8.000 6.3646157
## 146 95.000 95.4609216
## 147 94.000 88.6418376
## 148 54.000 58.7031869
## 149 7.000 6.8154913
## 150 99.000 99.4039468
## 151 36.000 32.7704925
## 152 48.000 47.0586788
## 153 65.000 60.5332178
## 154 42.000 40.3092986
## 155 93.000 89.4222269
## 156 86.000 86.8213207
## 157 26.000 26.1169754
## 158 51.000 53.2665760
## 159 100.000 96.6232789
## 160 94.000 95.7844103
## 161 6.000 6.0472867
## 162 24.000 24.4738791
## 163 75.000 75.9684476
## 164 7.000 3.8293810
## 165 53.000 52.5170368
## 166 73.000 72.8045753
## 167 16.000 14.1099910
## 168 80.000 80.8608706
## 169 77.000 77.0198822
## 170 89.000 86.2697244
## 171 80.000 77.1373547
## 172 55.000 51.4764948
## 173 19.000 17.3455753
## 174 56.000 57.7285357
## 175 47.000 44.1502939
## 176 56.000 59.2436274
## 177 2.000 -1.0532756
## 178 82.000 86.7900225
## 179 57.000 60.1403186
## 180 44.000 44.0422206
## 181 26.000 24.5227488
## 182 52.000 52.9530552
## 183 41.000 43.1613350
## 184 44.000 45.6756258
## 185 3.000 -2.8307495
## 186 31.000 29.1969318
## 187 97.000 96.4981240
## 188 21.000 22.5453232
## 189 17.000 20.1074143
## 190 7.000 4.0354303
## 191 61.000 61.1456852
## 192 10.000 13.9716365
## 193 52.000 55.3452989
## 194 10.000 12.1844117
## 195 65.000 64.0007766
## 196 71.000 70.3188322
## 197 4.000 -0.9368950
## 198 24.000 18.9142228
## 199 26.000 23.8759033
## 200 51.000 47.5775361
## 201 42.000 43.2736092
## 202 62.000 66.4827875
## 203 74.000 75.7260553
## 204 77.000 80.5964334
## 205 3.000 -2.2358799
## 206 50.000 47.0465496
## 207 24.000 21.5963558
## 208 37.000 32.8755896
## 209 58.000 57.9578296
## 210 52.000 52.2476003
## 211 27.000 24.5828690
## 212 14.000 12.1257381
## 213 100.000 100.0158026
## 214 3530.157 NA
## 215 72.000 74.0468266
## 216 5.000 1.6119475
## 217 71.000 70.3683631
## 218 54.000 52.2683173
## 219 84.000 83.1286166
## 220 42.000 43.6476505
## 221 54.000 49.4478543
## 222 74.000 72.6356699
## 223 54.000 52.7813064
## 224 53.000 57.1119514
## 225 78.000 79.1050629
## 226 97.000 101.6228548
## 227 49.000 53.5825402
## 228 71.000 68.9213930
## 229 48.000 46.9666961
## 230 51.000 51.0264287
## 231 89.000 85.5207355
## 232 99.000 99.5168576
## 233 93.000 94.6391126
## 234 49.000 46.7835774
## 235 18.000 21.2132196
## 236 65.000 58.3726600
## 237 83.000 87.2205968
## 238 100.000 102.4967859
## 239 41.000 43.8831433
## 240 52.000 53.0665576
## 241 29.000 26.3346478
## 242 97.000 98.5200893
## 243 7.000 9.4004976
## 244 51.000 52.9402670
## 245 58.000 53.8302088
## 246 50.000 45.9451114
## 247 67.000 65.0132736
## 248 89.000 86.5069584
## 249 76.000 75.6328080
## 250 35.000 36.7803503
## 251 99.000 100.5328916
## 252 31.000 29.0446614
## 253 52.000 51.7035243
## 254 11.000 9.1999547
## 255 66.000 71.7001585
## 256 50.000 49.8263406
## 257 39.000 37.4997110
## 258 60.000 53.6508468
## 259 35.000 33.9256197
## 260 53.000 49.9263969
## 261 14.000 8.1481543
## 262 49.000 49.7235904
## 263 16.000 16.1671276
## 264 76.000 75.3003300
## 265 13.000 9.5773686
## 266 51.000 48.3808836
## 267 70.000 72.9533167
## 268 98.000 92.5957385
## 269 86.000 88.8552359
## 270 100.000 99.0036177
## 271 46.000 45.0943957
## 272 51.000 46.9436268
## 273 50.000 48.3344960
## 274 91.000 94.9232957
## 275 48.000 47.7816525
## 276 81.000 81.2896075
## 277 38.000 37.8315502
## 278 40.000 39.6918525
## 279 79.000 76.9266485
## 280 96.000 88.0299053
## 281 60.000 56.9917887
## 282 70.000 72.5892938
## 283 44.000 44.9810344
## 284 11.000 11.9901764
## 285 6.000 1.9195133
## 286 5.000 1.6288261
## 287 72.000 66.2774665
## 288 55.000 57.5388726
## 289 95.000 94.7029108
## 290 41.000 41.2146990
## 291 25.000 25.0416924
## 292 1.000 3.7782099
## 293 55.000 50.5071178
## 294 4.000 9.6824085
## 295 48.000 48.8814761
## 296 55.000 54.4034860
## 297 75.000 71.7023316
## 298 68.000 69.3584839
## 299 100.000 99.9849159
## 300 25.000 26.0332372
## 301 75.000 75.4891031
## 302 34.000 36.5962306
## 303 38.000 40.9510219
## 304 92.000 86.7831627
## 305 21.000 15.5070118
## 306 88.000 85.8607787
## 307 75.000 79.2061011
## 308 76.000 80.8064377
## 309 44.000 48.5971728
## 310 10.000 13.9341505
## 311 21.000 27.3051179
## 312 16.000 14.0022630
## 313 32.000 33.6741600
## 314 13.000 13.1161288
## 315 26.000 24.7664919
## 316 70.000 73.6847788
## 317 77.000 77.5314954
## 318 77.000 76.2450320
## 319 88.000 88.0578931
## 320 35.000 35.0244580
## 321 24.000 21.6585774
## 322 17.000 17.3368156
## 323 91.000 94.3677896
## 324 32.000 33.4339631
## 325 36.000 32.5217940
## 326 89.000 90.5774130
## 327 69.000 71.2563413
## 328 30.000 31.2321286
## 329 6.000 5.3988401
## 330 22.000 18.5624139
## 331 67.000 71.9712104
## 332 9.000 5.2257596
## 333 74.000 73.5964342
## 334 50.000 49.7694898
## 335 85.000 82.6908751
## 336 3.000 1.6523091
## 337 0.000 -3.8366521
## 338 59.000 62.0381156
## 339 62.000 61.2651458
## 340 17.000 13.2499163
## 341 90.000 88.6167269
## 342 23.000 21.1365553
## 343 19.000 23.8501748
## 344 93.000 92.0120340
## 345 14.000 10.2671226
## 346 58.000 54.1468162
## 347 87.000 87.0064571
## 348 37.000 37.6944735
## 349 20.000 19.6227865
## 350 35.000 34.7856101
## 351 63.000 62.0319098
## 352 56.000 52.6700380
## 353 62.000 58.0903148
## 354 98.000 97.1944882
## 355 90.000 90.5015530
## 356 51.000 50.5123462
## 357 93.000 94.4521187
## 358 22.000 21.1079464
## 359 38.000 37.3629843
## 360 13.000 10.2857484
## 361 98.000 96.0493242
## 362 99.000 100.0953697
## 363 31.000 30.6063167
## 364 94.000 96.1900054
## 365 73.000 71.3082803
## 366 37.000 34.5931104
## 367 23.000 19.0233288
## 368 11.000 10.7666969
## 369 88.000 90.5799868
## 370 47.000 48.7178768
## 371 79.000 78.7413976
## 372 91.000 85.2349227
## 373 71.000 71.6578996
## 374 10.000 8.9389906
## 375 39.000 39.8960605
## 376 92.000 91.8509112
## 377 99.000 99.1120037
## 378 28.000 26.2219649
## 379 32.000 33.2158423
## 380 32.000 35.7239269
## 381 75.000 76.8860449
## 382 99.000 99.3087457
## 383 27.000 25.7716107
## 384 64.000 67.8516941
## 385 98.000 98.5037108
## 386 38.000 31.1133190
## 387 46.000 45.5117103
## 388 13.000 12.6553781
## 389 96.000 95.5606537
## 390 9.000 9.5264316
## 391 34.000 36.1089321
## 392 49.000 46.4362832
## 393 1.000 -3.8399811
## 394 50.000 48.9730204
## 395 94.000 93.2530550
## 396 27.000 23.4765097
## 397 20.000 17.1355113
## 398 12.000 14.5589614
## 399 45.000 41.5399273
## 400 91.000 91.6473055
## 401 61.000 66.1665256
## 402 10.000 9.2308575
## 403 47.000 47.4137789
## 404 33.000 34.7644156
## 405 84.000 86.1079664
## 406 24.000 21.8126795
## 407 48.000 48.8996395
## 408 48.000 46.7810864
## 409 9.000 12.9132855
## 410 93.000 94.5520314
## 411 99.000 94.9706875
## 412 8.000 2.3791725
## 413 20.000 21.4798299
## 414 38.000 35.7979546
## 415 78.000 82.0763803
## 416 81.000 78.8709771
## 417 42.000 47.2492425
## 418 95.000 96.1885233
## 419 78.000 78.3849193
## 420 44.000 42.9427406
## 421 68.000 64.4323160
## 422 87.000 84.2119148
## 423 58.000 57.3069783
## 424 52.000 52.5210144
## 425 26.000 25.7440243
## 426 75.000 75.4228340
## 427 48.000 53.6252301
## 428 71.000 75.1446631
## 429 77.000 74.1215151
## 430 34.000 36.2480724
## 431 24.000 20.2166590
## 432 70.000 66.9475812
## 433 29.000 34.0727825
## 434 76.000 73.1385004
## 435 98.000 92.8592915
## 436 28.000 28.3679381
## 437 87.000 85.5930873
## 438 9.000 10.6845375
## 439 87.000 86.1070862
## 440 33.000 33.2203142
## 441 64.000 66.0956342
## 442 17.000 19.3048655
## 443 49.000 48.8454208
## 444 95.000 93.7317631
## 445 75.000 75.4575861
## 446 89.000 91.2423923
## 447 81.000 87.1569085
## 448 25.000 25.5375283
## 449 47.000 46.0662948
## 450 50.000 49.6527766
## 451 5.000 7.3822442
## 452 68.000 71.1118994
## 453 84.000 83.5057052
## 454 8.000 8.7911399
## 455 41.000 33.3063890
## 456 26.000 26.4036252
## 457 89.000 91.7296073
## 458 78.000 82.5303072
## 459 34.000 36.6776273
## 460 92.000 86.9845035
## 461 27.000 32.3478418
## 462 12.000 16.7835397
## 463 2.000 1.5765844
## 464 22.000 17.4618141
## 465 0.000 2.1161130
## 466 26.000 24.3480433
## 467 50.000 48.2949120
## 468 84.000 85.5214545
## 469 70.000 73.7143478
## 470 66.000 63.1518950
## 471 42.000 38.4621368
## 472 19.000 19.4710079
## 473 94.000 94.0742822
## 474 71.000 67.9205129
## 475 19.000 22.5809624
## 476 16.000 16.0162989
## 477 49.000 48.4330789
## 478 29.000 29.6673599
## 479 29.000 26.6556633
## 480 86.000 86.2820674
## 481 50.000 50.8230492
## 482 86.000 88.5725171
## 483 30.000 32.5998075
## 484 23.000 21.0246937
## 485 20.000 20.7289498
## 486 16.000 20.3805119
## 487 57.000 57.2518015
## 488 8.000 6.9675371
## 489 8.000 10.2400850
## 490 62.000 64.9484109
## 491 55.000 55.3589391
## 492 30.000 31.2436559
## 493 86.000 90.7204882
## 494 62.000 58.7501270
## 495 51.000 55.8500320
## 496 61.000 60.1992587
## 497 86.000 85.0329541
## 498 61.000 60.3882308
## 499 21.000 18.4467979
## 500 81.000 82.1883925
## 501 97.000 94.2963344
## 502 5.000 7.6820246
## 503 61.000 61.0185809
## 504 47.000 53.6056222
## 505 98.000 94.4772880
## 506 30.000 27.9645947
## 507 63.000 62.5566259
## 508 0.000 1.4062544
## 509 100.000 101.7003412
## 510 18.000 13.8497399
## 511 30.000 28.9976931
## 512 98.000 99.0431569
## 513 16.000 15.5613551
## 514 22.000 24.6352839
## 515 55.000 53.9839337
## 516 43.000 42.9144973
## 517 75.000 74.2966211
## 518 91.000 91.1701288
## 519 46.000 49.4244088
## 520 85.000 82.4768352
## 521 55.000 56.1530395
## 522 36.000 37.1706313
## 523 49.000 46.3692866
## 524 94.000 97.0238346
## 525 43.000 40.8318210
## 526 22.000 24.0849831
## 527 37.000 41.1438636
## 528 24.000 21.9738807
## 529 95.000 100.7408970
## 530 61.000 61.1997160
## 531 75.000 74.3951700
## 532 68.000 69.0437717
## 533 58.000 56.6871879
## 534 5.000 5.8603917
## 535 53.000 55.7202136
## 536 80.000 79.2202182
## 537 83.000 86.3017752
## 538 25.000 25.2697189
## 539 34.000 36.3329445
## 540 26.000 27.6557423
## 541 90.000 94.7969053
## 542 60.000 58.6736667
## 543 49.000 56.1593447
## 544 19.000 18.4091939
## 545 92.000 86.2693699
## 546 29.000 26.5943619
## 547 8.000 8.4525202
## 548 57.000 56.1813152
## 549 29.000 27.6545267
## 550 19.000 20.8739179
## 551 81.000 77.8335444
## 552 50.000 50.0178783
## 553 15.000 9.2908563
## 554 70.000 75.0284725
## 555 39.000 38.3037698
## 556 43.000 44.7078640
## 557 21.000 22.5101657
## 558 98.000 102.4959452
## 559 86.000 86.7684524
## 560 16.000 13.8974858
## 561 25.000 24.8182427
## 562 31.000 33.9422486
## 563 93.000 92.2697006
## 564 67.000 68.7336508
## 565 49.000 47.3851688
## 566 25.000 32.3757691
## 567 88.000 87.6738868
## 568 54.000 54.5764837
## 569 21.000 18.0645022
## 570 8.000 7.8965398
## 571 32.000 35.0034108
## 572 35.000 36.7282332
## 573 67.000 65.8497543
## 574 90.000 89.5929549
## 575 59.000 61.6902620
## 576 15.000 11.6049932
## 577 67.000 71.0826803
## 578 42.000 43.7190116
## 579 44.000 41.5742101
## 580 77.000 74.2555242
## 581 68.000 66.2831044
## 582 36.000 36.6243808
## 583 11.000 10.3237487
## 584 10.000 7.1564577
## 585 65.000 67.8860313
## 586 98.000 101.1097591
## 587 98.000 98.6132033
## 588 49.000 50.1908384
## 589 31.000 27.8389626
## 590 56.000 55.9249564
## 591 70.000 76.4734087
## 592 91.000 92.0575638
## 593 25.000 27.3524544
## 594 54.000 55.3208348
## 595 39.000 41.3999035
## 596 91.000 93.5905702
## 597 3.000 5.2970540
## 598 22.000 21.0142942
## 599 2.000 2.2670595
## 600 2.000 -0.1218605
## 601 65.000 66.4954621
## 602 71.000 73.8363769
## 603 42.000 42.1014088
## 604 76.000 77.3513573
## 605 43.000 41.0225178
## 606 8.000 14.7530527
## 607 86.000 83.2819902
## 608 87.000 89.9337434
## 609 3.000 2.2865717
## 610 58.000 55.6142130
## 611 62.000 62.1531341
## 612 89.000 89.5580353
## 613 95.000 94.0029186
## 614 28.000 26.7802385
## 615 0.000 -0.7645376
## 616 1.000 0.2828660
## 617 49.000 44.2680052
## 618 21.000 19.8517414
## 619 46.000 47.1596001
## 620 11.000 8.3593666
## 621 89.000 92.0815708
## 622 37.000 41.8873405
## 623 29.000 30.5413129
## 624 44.000 46.8765447
## 625 96.000 96.3565948
## 626 16.000 17.9170699
## 627 74.000 71.6794992
## 628 35.000 32.6499755
## 629 42.000 39.3448297
## 630 16.000 17.0340200
## 631 56.000 52.8752407
## 632 18.000 15.8541485
## 633 100.000 108.8716183
## 634 54.000 49.3047725
## 635 92.000 89.4749477
## 636 63.000 63.6734824
## 637 81.000 83.7841095
## 638 73.000 73.5113692
## 639 48.000 46.8029724
## 640 1.000 5.8099468
## 641 85.000 85.2302797
## 642 14.000 10.5821396
## 643 25.000 21.3769832
## 644 45.000 46.0537745
## 645 98.000 95.2389253
## 646 97.000 94.1514921
## 647 58.000 54.5486805
## 648 93.000 87.3626045
## 649 88.000 88.4774160
## 650 89.000 84.4804568
## 651 47.000 48.7964707
## 652 6.000 10.7667568
## 653 34.000 30.4888292
## 654 30.000 29.7684619
## 655 16.000 13.5157475
## 656 86.000 86.1295588
## 657 40.000 43.3002275
## 658 52.000 51.9211023
## 659 15.000 16.4918529
## 660 4.000 7.9980734
## 661 95.000 97.6668957
## 662 99.000 89.8054537
## 663 35.000 38.0716657
## 664 58.000 60.2785232
## 665 10.000 6.7091958
## 666 16.000 18.3548892
## 667 53.000 56.3705820
## 668 58.000 62.8006420
## 669 42.000 41.2515563
## 670 24.000 19.4263754
## 671 84.000 82.8893580
## 672 64.000 63.6136498
## 673 12.000 11.2962720
## 674 61.000 60.0227488
## 675 75.000 72.6033933
## 676 15.000 11.8796457
## 677 100.000 100.7012737
## 678 43.000 45.1242081
## 679 13.000 14.8110680
## 680 48.000 48.0936803
## 681 45.000 42.2914567
## 682 52.000 52.7338979
## 683 34.000 36.7239699
## 684 30.000 28.6453520
## 685 65.000 62.1667527
## 686 100.000 95.5845952
## 687 67.000 66.0432530
## 688 99.000 99.9566225
## 689 45.000 46.1494198
## 690 87.000 89.1375496
## 691 73.000 69.7178781
## 692 9.000 12.3173665
## 693 81.000 78.2029627
## 694 72.000 71.3099537
## 695 81.000 81.4554471
## 696 58.000 58.5950064
## 697 93.000 94.6250937
## 698 82.000 88.6037700
## 699 66.000 63.6486853
## 700 97.000 94.9752655
valida <- read.csv("~/Mis clases ITD/Semestre Enero Junio 2020/Analisis Inteligente de Datos/datos/random-linear-regression/test.csv")
valida
## x y
## 1 77 79.7751520
## 2 21 23.1772789
## 3 22 25.6092616
## 4 20 17.8573881
## 5 36 41.8498644
## 6 15 9.8052349
## 7 62 58.8746593
## 8 95 97.6179370
## 9 20 18.3951275
## 10 5 8.7467477
## 11 4 2.8114158
## 12 19 17.0953724
## 13 96 95.1490718
## 14 62 61.3880066
## 15 36 40.2470172
## 16 15 14.8224859
## 17 65 66.9580687
## 18 14 16.6350798
## 19 87 90.6551374
## 20 69 77.2298264
## 21 89 92.1190628
## 22 51 46.9138771
## 23 89 89.8263444
## 24 27 21.7138035
## 25 97 97.4120698
## 26 58 57.0163136
## 27 79 78.3105654
## 28 21 19.1315097
## 29 93 93.0348339
## 30 27 26.5911240
## 31 99 97.5515534
## 32 31 31.4352482
## 33 33 35.1272478
## 34 80 78.6104243
## 35 28 33.0711283
## 36 47 51.6996717
## 37 53 53.6223522
## 38 69 69.4630607
## 39 28 27.4249724
## 40 33 36.3464419
## 41 91 95.0614086
## 42 71 68.1672476
## 43 50 50.9615553
## 44 76 78.0423745
## 45 4 5.6076649
## 46 37 36.1133478
## 47 70 67.2352155
## 48 68 65.0132404
## 49 40 38.1475387
## 50 35 34.3114145
## 51 94 95.2850394
## 52 88 87.8474991
## 53 52 54.0817063
## 54 31 31.9306352
## 55 59 59.6124709
## 56 0 -1.0401142
## 57 39 47.4937477
## 58 64 62.6008977
## 59 69 70.9146434
## 60 57 56.1483411
## 61 13 14.0557288
## 62 72 68.1136715
## 63 76 75.5970135
## 64 61 59.2257450
## 65 82 85.4550416
## 66 18 17.7619712
## 67 41 38.6888868
## 68 50 50.9634364
## 69 55 51.8350387
## 70 13 17.0761107
## 71 46 46.5614177
## 72 13 10.3475446
## 73 79 77.9103297
## 74 53 50.1700862
## 75 15 13.2569065
## 76 28 31.3227493
## 77 81 73.9308764
## 78 69 74.4511438
## 79 52 52.0193229
## 80 84 83.6882050
## 81 68 70.3698748
## 82 27 23.4447916
## 83 56 49.8305180
## 84 48 49.8822659
## 85 40 41.0452558
## 86 39 33.3783439
## 87 82 81.2975013
## 88 100 105.5918375
## 89 59 56.8245701
## 90 43 48.6725264
## 91 67 67.0215061
## 92 38 38.4307639
## 93 63 58.6146689
## 94 91 89.1237751
## 95 60 60.9105427
## 96 14 13.8395988
## 97 21 16.8908519
## 98 87 84.0667682
## 99 73 70.3496977
## 100 32 33.3847414
## 101 2 -1.6329683
## 102 82 88.5447590
## 103 19 17.4404762
## 104 74 75.6929855
## 105 42 41.9760711
## 106 12 12.5924474
## 107 1 0.2753073
## 108 90 98.1325801
## 109 89 87.4572156
## 110 0 -2.3447385
## 111 41 39.3294153
## 112 16 16.6871521
## 113 94 96.5888860
## 114 97 97.7034220
## 115 66 67.0171596
## 116 24 25.6347626
## 117 17 13.4131076
## 118 90 95.1564728
## 119 13 9.7441643
## 120 0 -3.4678838
## 121 64 62.8281635
## 122 96 97.2740546
## 123 98 95.5801718
## 124 12 7.4685018
## 125 41 45.4459959
## 126 47 46.6901397
## 127 78 74.4993599
## 128 20 21.6350065
## 129 89 91.5954885
## 130 29 26.4948796
## 131 64 67.3865470
## 132 75 74.2536284
## 133 12 12.0799165
## 134 25 21.3227373
## 135 28 29.3177005
## 136 30 26.4871368
## 137 65 68.9469977
## 138 59 59.1059900
## 139 64 64.3752109
## 140 53 60.2075835
## 141 71 70.3432971
## 142 97 97.1082562
## 143 73 75.7584178
## 144 9 10.8046273
## 145 12 12.1121994
## 146 63 63.2831238
## 147 99 98.0301772
## 148 60 63.1935435
## 149 35 34.8534823
## 150 2 -2.8199140
## 151 60 59.8313966
## 152 32 29.3850502
## 153 94 97.0014837
## 154 84 85.1865727
## 155 63 61.7406319
## 156 22 18.8479816
## 157 81 78.7900853
## 158 93 95.1240048
## 159 33 30.4888129
## 160 7 10.4146809
## 161 42 38.9831744
## 162 46 46.1102106
## 163 54 52.4510363
## 164 16 21.1652395
## 165 49 52.2862061
## 166 43 44.1886394
## 167 95 97.1383202
## 168 66 67.2200800
## 169 21 18.9832231
## 170 35 24.3884599
## 171 80 79.4476952
## 172 37 40.0350486
## 173 54 53.3200576
## 174 56 54.5544698
## 175 1 -2.7611826
## 176 32 37.8018280
## 177 58 57.4874144
## 178 32 36.0629299
## 179 46 49.8353817
## 180 72 74.6895328
## 181 17 14.8615940
## 182 97 101.0697879
## 183 93 99.4357788
## 184 91 91.6924075
## 185 37 34.1247325
## 186 4 6.0793901
## 187 54 59.0724717
## 188 51 56.4304602
## 189 27 30.4941293
## 190 46 48.3517263
## 191 92 89.7315361
## 192 73 72.8628253
## 193 77 80.9714429
## 194 91 91.3656637
## 195 61 60.0713750
## 196 99 99.8738271
## 197 4 8.6557142
## 198 72 69.3985850
## 199 19 19.3878013
## 200 57 53.1162843
## 201 78 78.3968301
## 202 26 25.7561251
## 203 74 75.0748468
## 204 90 92.8877228
## 205 66 69.4549850
## 206 13 13.1210984
## 207 40 48.0984313
## 208 77 79.3142548
## 209 67 68.4882075
## 210 75 73.2300846
## 211 23 24.6836271
## 212 45 41.9036892
## 213 59 62.2263568
## 214 44 45.9639688
## 215 23 23.5264715
## 216 55 51.8003587
## 217 55 51.1077427
## 218 95 95.7974734
## 219 12 9.2411390
## 220 4 7.6465298
## 221 7 9.2816998
## 222 100 103.5266162
## 223 48 47.4100672
## 224 42 42.0383577
## 225 96 96.1198248
## 226 39 38.0576641
## 227 100 105.4503788
## 228 87 88.8030691
## 229 14 15.4930114
## 230 14 12.4262461
## 231 37 40.0070960
## 232 5 5.6340309
## 233 88 87.3693893
## 234 91 89.7395199
## 235 65 66.6149964
## 236 74 72.9138853
## 237 56 57.1910351
## 238 16 11.2171048
## 239 5 0.6760767
## 240 28 28.1566854
## 241 92 95.3958003
## 242 46 52.0549070
## 243 54 59.7086458
## 244 39 36.7922476
## 245 44 37.0845770
## 246 31 24.1843798
## 247 68 67.2872533
## 248 86 82.8705940
## 249 90 89.8999910
## 250 38 36.9417318
## 251 21 19.8756224
## 252 95 90.7148165
## 253 56 61.0936776
## 254 60 60.1113496
## 255 65 64.8329632
## 256 78 81.4038177
## 257 89 92.4021769
## 258 6 2.5766254
## 259 67 63.8076817
## 260 36 38.6778076
## 261 16 16.8283970
## 262 100 99.7868725
## 263 45 44.6891343
## 264 73 71.0037782
## 265 57 51.5732672
## 266 20 19.8784648
## 267 76 79.5034150
## 268 34 34.5887649
## 269 55 55.7383467
## 270 72 68.1972191
## 271 55 55.8162851
## 272 8 9.3914168
## 273 56 56.0144811
## 274 72 77.9969477
## 275 58 55.3704995
## 276 6 11.8945783
## 277 96 94.7908171
## 278 23 25.6904155
## 279 58 53.5204232
## 280 23 18.3139676
## 281 19 21.4263779
## 282 25 30.4130328
## 283 64 67.6814215
## 284 21 17.0854783
## 285 59 60.9179271
## 286 19 14.9951432
## 287 16 16.7492394
## 288 42 41.4692388
## 289 43 42.8452611
## 290 61 59.1291297
## 291 92 91.3086367
## 292 11 8.6733364
## 293 41 39.3148529
## 294 1 5.3136862
## 295 8 5.4052205
## 296 71 68.5458879
## 297 46 47.3348763
## 298 55 54.0906369
## 299 62 63.2971706
## 300 47 52.4594669
Explorando datos
str(entrena)
## 'data.frame': 700 obs. of 2 variables:
## $ x: num 24 50 15 38 87 36 12 81 25 5 ...
## $ y: num 21.5 47.5 17.2 36.6 87.3 ...
summary(entrena)
## x y
## Min. : 0.00 Min. : -3.84
## 1st Qu.: 25.00 1st Qu.: 24.93
## Median : 49.00 Median : 48.97
## Mean : 54.99 Mean : 49.94
## 3rd Qu.: 75.00 3rd Qu.: 74.93
## Max. :3530.16 Max. :108.87
## NA's :1
str(valida)
## 'data.frame': 300 obs. of 2 variables:
## $ x: int 77 21 22 20 36 15 62 95 20 5 ...
## $ y: num 79.8 23.2 25.6 17.9 41.8 ...
summary(valida)
## x y
## Min. : 0.00 Min. : -3.468
## 1st Qu.: 27.00 1st Qu.: 25.677
## Median : 53.00 Median : 52.171
## Mean : 50.94 Mean : 51.205
## 3rd Qu.: 73.00 3rd Qu.: 74.303
## Max. :100.00 Max. :105.592
Graficando los datos de entrenamiento
- Con summary(entrena), se observa que hay un valor máximo de 3500 , algo raro!
- Se genera un gráfico de dispersión extraño,
- Se interpreta que hay un valor en x de 3500 aproximadamente
- Además su valor en y es nulo, no se visualiza
- Lo consideramos como basura y habrá que limpiar el conjunto de datos
plot(entrena)

Limpieza de datos
- ¿Cuál es el registro de entrenamiento que tiene valores nulos o NA, tanto en las variables x como y?
- Se identifique que es el registro 214 y es solo 1.
- ¿Cúal valor en x está por encima de 100, el 214
- ¿Qué hacemos con ese registro?
- Limpiamos el conjunto de datos entrenamiento. Simplemente quitamos el registro 214
filter(entrena, is.null(x) | is.null(y) | is.na(x) | is.na(y))
## x y
## 1 3530.157 NA
filter(entrena, x > 100)
## x y
## 1 3530.157 NA
entrena <- entrena[-214,]
Nuevamente hacer gráfica con
plot(entrena)

Graficando con ggplot()
ggplot() + geom_point(data = entrena, aes(x = x, y = y)) +
xlab("Variable Independiente") +
ylab("Variable Dependiente") +
ggtitle("Conjunto de Entrenamiento (entrena)")

Regresión lineal simple
- El conjunto de datos está formado por una serie de puntos que, claramente, tiene una dependencia lineal entre ellos.
- Aplicar una regresión sobre estos datos implica obtener la línea recta que mejor ajuste la relación existente entre la variable independiente y la variable dependiente.
- Crear un regresor haciendo uso de la función lm del paquete stats:
- Establecer una semilla con la funciój set.seed() para genera mismos valores
- primer argumento de lm se coloca la fórmula variable dependiente ~ variable independiente
# set.seed(1234)
regresor <- lm(y ~ x, data = entrena)
regresor
##
## Call:
## lm(formula = y ~ x, data = entrena)
##
## Coefficients:
## (Intercept) x
## -0.1073 1.0007
- Significa que y = -0.1073 + 1.0007 por x
- Fórmula y = bx + a; y = a + bx
- a, es la intersección
- b, es la pendiente
Explorar el regresor
summary(regresor)
##
## Call:
## lm(formula = y ~ x, data = entrena)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.1523 -2.0179 0.0325 1.8573 8.9132
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.107265 0.212170 -0.506 0.613
## x 1.000656 0.003672 272.510 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.809 on 697 degrees of freedom
## Multiple R-squared: 0.9907, Adjusted R-squared: 0.9907
## F-statistic: 7.426e+04 on 1 and 697 DF, p-value: < 2.2e-16
- ¿qué siginfican los estadísticos?
Generar un predictor
- Crear un vector de predicciones basado en el conjunto de entrenamiento
y_predict <- predict(regresor, entrena)
Visualizar la curva de ajuste
ggplot() + geom_point(data = entrena, aes(x = x, y = y), size = 0.9) +
geom_line(aes( x = entrena$x, y = y_predict), color = "red") +
xlab("Variable Independiente") +
ylab("Variable Dependiente") +
ggtitle("Curva de Ajuste sobre Conjunto de Entrenamiento")

Realizar predicciones sobre conjunto de datos para validación
- Datos de valida
- Contiene puntos que no fueron usados durante el entrenamiento
y_valida_predict <- predict(regresor, valida)
Visualizando datos de validación
ggplot() + geom_point(data = valida, aes(x = x, y = y), size = 0.9) +
geom_line(aes( x = valida$x, y = y_valida_predict), color = "red") +
xlab("Variable Independiente") +
ylab("Variable Dependiente") +
ggtitle("Curva de Ajuste sobre Conjunto de Validación")

Correlación
- Correlación existente entre los valores y del conjunto de entrenamiento, y los valores predichos para dicho conjunto
cor(valida$y, y_valida_predict)
## [1] 0.9945453
- El valor de 0.9945 indica una alta correlación y, por lo tanto, un buen ajuste a los datos por parte del modelo obtenido.
Predecir valores
- Determinar una predicción particular para un valor cualquiera de x,
- Varias predicciones
- Transformar a data.frame los valores de x
predict_value <- predict(regresor, data.frame(x = c(25, 50, 58.7, 75)))
predict_value
## 1 2 3 4
## 24.90914 49.92555 58.63126 74.94196
Interpretación ….