library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ 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(corrplot)
## corrplot 0.92 loaded
library(tidyr)
library(psych)
## 
## Attaching package: 'psych'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(ggplot2)

Datosepi2 <- read_excel("Base de datos modelo en cebada.xlsx", 
    col_types = c("numeric", "numeric", "numeric", 
        "numeric", "numeric", "numeric", 
        "numeric", "numeric", "numeric", 
        "numeric", "numeric", "numeric", 
        "numeric", "numeric", "numeric", 
        "numeric", "numeric"))
str(Datosepi2)
## tibble [69 × 17] (S3: tbl_df/tbl/data.frame)
##  $ Dia                           : num [1:69] 2023240 2023241 2023242 2023243 2023244 ...
##  $ h diarias t (10-18)°          : num [1:69] 9 9 22 7 8 11 12 18 13 10 ...
##  $ t max (°C)                    : num [1:69] 15 14.5 14.9 14.7 15 ...
##  $ t min (°C)                    : num [1:69] 13.2 12.4 13.4 13.1 13.3 ...
##  $ h con DPV                     : num [1:69] 17 12 28 14 12 9 9 15 14 16 ...
##  $ DPV min (kpa)                 : num [1:69] 0.451 0.335 0.382 0.497 0.438 ...
##  $ HR max (%)                    : num [1:69] 77 83.5 80.8 76.1 80.3 ...
##  $ h diarias HR >65%             : num [1:69] 15 18 34 14 15 21 20 19 19 14 ...
##  $ HR min (%)                    : num [1:69] 70 74.9 73.7 69.7 72.9 ...
##  $ Precipitación [mm]            : num [1:69] 0 13.4 0 0 0 17.6 0.2 0.4 0 0 ...
##  $ H diarias con pp(mm)          : num [1:69] 0 3 3 0 0 5 1 2 0 0 ...
##  $ Vel viento max(m/s)           : num [1:69] 1.53 1.3 1.67 1.78 1.56 ...
##  $ h diarias con vel viento >2m/s: num [1:69] 7 8 17 11 9 8 4 7 9 10 ...
##  $ Incidencia                    : num [1:69] 23.3 23.3 23.3 23.3 23.3 23.3 23.3 23.3 23.3 23.3 ...
##  $ Severidad 1                   : num [1:69] 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 ...
##  $ Severidad                     : num [1:69] 0.594 0.594 0.594 0.594 0.594 ...
##  $ Aplicaciones de fungicida     : num [1:69] 0 0 0 0 0 0 0 0 0 0 ...
cov(Datosepi2[, 2:17])
##                                h diarias t (10-18)°  t max (°C)  t min (°C)
## h diarias t (10-18)°                    33.20502984  1.26631953  2.58403711
## t max (°C)                               1.26631953  1.13551255  1.23780410
## t min (°C)                               2.58403711  1.23780410  1.43997572
## h con DPV                                2.87297528  0.38754540  0.03649818
## DPV min (kpa)                           -0.32758313  0.03591035  0.01767929
## HR max (%)                              14.39408548 -1.53159178 -0.66829141
## h diarias HR >65%                       20.67710997  0.32543549  0.78100033
## HR min (%)                              16.02351062 -2.24761733 -1.18487867
## Precipitación [mm]                      -2.61611253 -0.10158068 -0.22401736
## H diarias con pp(mm)                     5.39151748  0.03801681  0.44464782
## Vel viento max(m/s)                      4.91107674  0.07455652  0.45018806
## h diarias con vel viento >2m/s          -3.54390452  0.13843390 -0.27993787
## Incidencia                              16.31847471 -2.56028838 -0.24580889
## Severidad 1                             -7.17710997  0.43369511  0.39500745
## Severidad                               -0.55150256  0.03536588  0.03406565
## Aplicaciones de fungicida               -0.01300085 -0.01969278 -0.02429767
##                                   h con DPV DPV min (kpa)   HR max (%)
## h diarias t (10-18)°             2.87297528 -0.3275831350  14.39408548
## t max (°C)                       0.38754540  0.0359103504  -1.53159178
## t min (°C)                       0.03649818  0.0176792936  -0.66829141
## h con DPV                       18.77919864  0.2925393547 -15.94100082
## DPV min (kpa)                    0.29253935  0.0137780370  -0.67377220
## HR max (%)                     -15.94100082 -0.6737722014  33.94160644
## h diarias HR >65%                5.00703325 -0.2990167438  14.14792534
## HR min (%)                     -18.98132095 -0.7706876161  38.89449754
## Precipitación [mm]              -8.78312020 -0.1144668297   7.29251583
## H diarias con pp(mm)            -5.13448423 -0.1357951773   7.14436479
## Vel viento max(m/s)             -3.72855929 -0.0851081489   4.06778790
## h diarias con vel viento >2m/s   8.95822677  0.2099521052 -10.39830678
## Incidencia                     -53.82153311 -1.7258551895  87.41040811
## Severidad 1                     -9.22394501 -0.1827311751  11.38739277
## Severidad                       -0.76018622 -0.0152526165   0.94345565
## Aplicaciones de fungicida        0.01875533 -0.0002677578  -0.01449959
##                                h diarias HR >65%   HR min (%)
## h diarias t (10-18)°                 20.67710997  16.02351062
## t max (°C)                            0.32543549  -2.24761733
## t min (°C)                            0.78100033  -1.18487867
## h con DPV                             5.00703325 -18.98132095
## DPV min (kpa)                        -0.29901674  -0.77068762
## HR max (%)                           14.14792534  38.89449754
## h diarias HR >65%                    27.48849105  14.32104784
## HR min (%)                           14.32104784  46.07585415
## Precipitación [mm]                    0.27826087   7.51913050
## H diarias con pp(mm)                  2.46739130   9.04423327
## Vel viento max(m/s)                  -1.39543927   5.59565271
## h diarias con vel viento >2m/s        0.49616368 -11.65672235
## Incidencia                           17.46745524  97.70103176
## Severidad 1                           0.25415601  13.30735864
## Severidad                             0.02667839   1.10485342
## Aplicaciones de fungicida            -0.06010230  -0.01187915
##                                Precipitación [mm] H diarias con pp(mm)
## h diarias t (10-18)°                   -2.6161125           5.39151748
## t max (°C)                             -0.1015807           0.03801681
## t min (°C)                             -0.2240174           0.44464782
## h con DPV                              -8.7831202          -5.13448423
## DPV min (kpa)                          -0.1144668          -0.13579518
## HR max (%)                              7.2925158           7.14436479
## h diarias HR >65%                       0.2782609           2.46739130
## HR min (%)                              7.5191305           9.04423327
## Precipitación [mm]                     44.4419437           6.91291560
## H diarias con pp(mm)                    6.9129156          11.36104007
## Vel viento max(m/s)                     1.0369027           8.41651344
## h diarias con vel viento >2m/s         -1.6356777          -2.85890878
## Incidencia                              7.9790494          11.82511012
## Severidad 1                            -1.8459079           2.26422634
## Severidad                              -0.1413947           0.19740249
## Aplicaciones de fungicida              -0.1618926          -0.07715260
##                                Vel viento max(m/s)
## h diarias t (10-18)°                    4.91107674
## t max (°C)                              0.07455652
## t min (°C)                              0.45018806
## h con DPV                              -3.72855929
## DPV min (kpa)                          -0.08510815
## HR max (%)                              4.06778790
## h diarias HR >65%                      -1.39543927
## HR min (%)                              5.59565271
## Precipitación [mm]                      1.03690266
## H diarias con pp(mm)                    8.41651344
## Vel viento max(m/s)                    14.21595400
## h diarias con vel viento >2m/s         -3.21838716
## Incidencia                              8.79836929
## Severidad 1                             2.52822304
## Severidad                               0.22043210
## Aplicaciones de fungicida              -0.03723211
##                                h diarias con vel viento >2m/s  Incidencia
## h diarias t (10-18)°                            -3.543905e+00  16.3184747
## t max (°C)                                       1.384339e-01  -2.5602884
## t min (°C)                                      -2.799379e-01  -0.2458089
## h con DPV                                        8.958227e+00 -53.8215331
## DPV min (kpa)                                    2.099521e-01  -1.7258552
## HR max (%)                                      -1.039831e+01  87.4104081
## h diarias HR >65%                                4.961637e-01  17.4674552
## HR min (%)                                      -1.165672e+01  97.7010318
## Precipitación [mm]                              -1.635678e+00   7.9790494
## H diarias con pp(mm)                            -2.858909e+00  11.8251101
## Vel viento max(m/s)                             -3.218387e+00   8.7983693
## h diarias con vel viento >2m/s                   8.753623e+00 -39.1244103
## Incidencia                                      -3.912441e+01 928.3716035
## Severidad 1                                     -1.716752e+00 117.1601796
## Severidad                                       -1.528413e-01   9.5041226
## Aplicaciones de fungicida                       -4.262575e-04   0.9449133
##                                Severidad 1   Severidad
## h diarias t (10-18)°            -7.1771100 -0.55150256
## t max (°C)                       0.4336951  0.03536588
## t min (°C)                       0.3950074  0.03406565
## h con DPV                       -9.2239450 -0.76018622
## DPV min (kpa)                   -0.1827312 -0.01525262
## HR max (%)                      11.3873928  0.94345565
## h diarias HR >65%                0.2541560  0.02667839
## HR min (%)                      13.3073586  1.10485342
## Precipitación [mm]              -1.8459079 -0.14139466
## H diarias con pp(mm)             2.2642263  0.19740249
## Vel viento max(m/s)              2.5282230  0.22043210
## h diarias con vel viento >2m/s  -1.7167519 -0.15284127
## Incidencia                     117.1601796  9.50412264
## Severidad 1                     57.0712116  4.58793558
## Severidad                        4.5879356  0.36891884
## Aplicaciones de fungicida        0.1630435  0.01293159
##                                Aplicaciones de fungicida
## h diarias t (10-18)°                       -0.0130008525
## t max (°C)                                 -0.0196927837
## t min (°C)                                 -0.0242976668
## h con DPV                                   0.0187553282
## DPV min (kpa)                              -0.0002677578
## HR max (%)                                 -0.0144995915
## h diarias HR >65%                          -0.0601023018
## HR min (%)                                 -0.0118791530
## Precipitación [mm]                         -0.1618925831
## H diarias con pp(mm)                       -0.0771526002
## Vel viento max(m/s)                        -0.0372321090
## h diarias con vel viento >2m/s             -0.0004262575
## Incidencia                                  0.9449133276
## Severidad 1                                 0.1630434783
## Severidad                                   0.0129315857
## Aplicaciones de fungicida                   0.0848252344
c2 <- cor(Datosepi2[, 2:17]) #correlacion estadistico de covarianza estandarizado, entre -1 y 1 

x2<- Datosepi2[,2:17]
cm2 <- colMeans(x2); cm2 #Media de las vaiables
##           h diarias t (10-18)°                     t max (°C) 
##                    14.97101449                    14.35675649 
##                     t min (°C)                      h con DPV 
##                    12.86378950                    11.68115942 
##                  DPV min (kpa)                     HR max (%) 
##                     0.23184355                    88.58118056 
##              h diarias HR >65%                     HR min (%) 
##                    21.52173913                    82.32298727 
##             Precipitación [mm]           H diarias con pp(mm) 
##                     2.85217391                     1.81159420 
##            Vel viento max(m/s) h diarias con vel viento >2m/s 
##                     1.97982085                     6.50724638 
##                     Incidencia                    Severidad 1 
##                    83.10314010                    14.72826087 
##                      Severidad      Aplicaciones de fungicida 
##                     1.18016304                     0.05797101
plot(c2)

hist(Datosepi2$Severidad)

hist(Datosepi2$`Severidad 1`)

nortest::ad.test(Datosepi2$`Severidad 1`)
## 
##  Anderson-Darling normality test
## 
## data:  Datosepi2$`Severidad 1`
## A = 9.1672, p-value < 2.2e-16
corrplot(c2)

sev2 <- Datosepi2$Severidad ## simetria, se va a tomar como un factor de 0, 1 y 2 para distintos grados de                                      enfermedad ? 

sev3 <- Datosepi2$`Severidad 1`
barplot(sev2)

barplot(sev3)

psych::describe(Datosepi2)
##                                vars  n       mean    sd     median    trimmed
## Dia                               1 69 2023294.29 55.80 2023281.00 2023283.30
## h diarias t (10-18)°              2 69      14.97  5.76      15.00      14.61
## t max (°C)                        3 69      14.36  1.07      14.49      14.49
## t min (°C)                        4 69      12.86  1.20      13.15      13.01
## h con DPV                         5 69      11.68  4.33      11.00      11.40
## DPV min (kpa)                     6 69       0.23  0.12       0.22       0.23
## HR max (%)                        7 69      88.58  5.83      89.14      88.75
## h diarias HR >65%                 8 69      21.52  5.24      22.00      21.09
## HR min (%)                        9 69      82.32  6.79      81.80      82.28
## Precipitación [mm]               10 69       2.85  6.67       0.20       1.19
## H diarias con pp(mm)             11 69       1.81  3.37       1.00       1.16
## Vel viento max(m/s)              12 69       1.98  3.77       1.34       1.35
## h diarias con vel viento >2m/s   13 69       6.51  2.96       7.00       6.44
## Incidencia                       14 69      83.10 30.47      98.33      87.62
## Severidad 1                      15 69      14.73  7.55      12.50      13.62
## Severidad                        16 69       1.18  0.61       1.00       1.09
## Aplicaciones de fungicida        17 69       0.06  0.29       0.00       0.00
##                                  mad        min        max  range  skew
## Dia                            25.20 2023240.00 2023453.00 213.00  2.07
## h diarias t (10-18)°            5.93       6.00      34.00  28.00  0.63
## t max (°C)                      0.72       8.05      15.76   7.71 -3.26
## t min (°C)                      0.92       6.13      14.41   8.28 -2.85
## h con DPV                       2.97       3.00      28.00  25.00  1.15
## DPV min (kpa)                   0.14       0.00       0.53   0.53  0.27
## HR max (%)                      6.82      74.41      99.96  25.55 -0.24
## h diarias HR >65%               2.97      14.00      48.00  34.00  2.33
## HR min (%)                      7.01      68.06      99.96  31.90  0.14
## Precipitación [mm]              0.30       0.00      32.80  32.80  3.02
## H diarias con pp(mm)            1.48       0.00      24.00  24.00  4.38
## Vel viento max(m/s)             0.28       0.90      26.40  25.50  5.62
## h diarias con vel viento >2m/s  2.97       0.00      17.00  17.00  0.46
## Incidencia                      2.47      23.30     100.00  76.70 -1.43
## Severidad 1                     0.00       7.50      32.50  25.00  1.52
## Severidad                       0.00       0.59       2.60   2.01  1.49
## Aplicaciones de fungicida       0.00       0.00       2.00   2.00  5.27
##                                kurtosis   se
## Dia                                3.26 6.72
## h diarias t (10-18)°               0.27 0.69
## t max (°C)                        15.90 0.13
## t min (°C)                        12.71 0.14
## h con DPV                          2.75 0.52
## DPV min (kpa)                     -0.59 0.01
## HR max (%)                        -0.67 0.70
## h diarias HR >65%                  9.04 0.63
## HR min (%)                        -0.46 0.82
## Precipitación [mm]                 9.14 0.80
## H diarias con pp(mm)              24.80 0.41
## Vel viento max(m/s)               30.76 0.45
## h diarias con vel viento >2m/s     0.85 0.36
## Incidencia                         0.07 3.67
## Severidad 1                        1.18 0.91
## Severidad                          1.09 0.07
## Aplicaciones de fungicida         28.82 0.04
scatter.hist(log(sev2), Datosepi2$`h diarias t (10-18)°`)

scatter.hist(sev2, Datosepi2$`t max (°C)`)

scatter.hist(sev2, Datosepi2$`t min (°C)`)

scatter.hist(sev2, Datosepi2$`h con DPV`)

scatter.hist(log(sev2), Datosepi2$`DPV min (kpa)`)

scatter.hist(sev2, Datosepi2$`HR max (%)`)

scatter.hist(sev2, Datosepi2$`h diarias HR >65%`)

scatter.hist(log(sev2), Datosepi2$`HR min (%)`)#alta correlacion, sube al aplicarse una transformación log

scatter.hist(1/(sev2), Datosepi2$`Precipitación [mm]`)

scatter.hist(sev2, Datosepi2$`H diarias con pp(mm)`)

scatter.hist(sev2, Datosepi2$`Vel viento max(m/s)`) 

scatter.hist(sev2, Datosepi2$`h diarias con vel viento >2m/s`)

scatter.hist(sev2, Datosepi2$`Severidad 1`)

scatter.hist(sev2, Datosepi2$Severidad)

scatter.hist(sev2, Datosepi2$`Aplicaciones de fungicida`)

data2 <- Datosepi2 %>% mutate(`Aplicaciones de fungicida`= (`Aplicaciones de fungicida`)) 
                             
summary(data2)
##       Dia          h diarias t (10-18)°   t max (°C)      t min (°C)   
##  Min.   :2023240   Min.   : 6.00        Min.   : 8.05   Min.   : 6.13  
##  1st Qu.:2023264   1st Qu.:10.00        1st Qu.:14.07   1st Qu.:12.43  
##  Median :2023281   Median :15.00        Median :14.49   Median :13.15  
##  Mean   :2023294   Mean   :14.97        Mean   :14.36   Mean   :12.86  
##  3rd Qu.:2023298   3rd Qu.:18.00        3rd Qu.:15.00   3rd Qu.:13.58  
##  Max.   :2023453   Max.   :34.00        Max.   :15.76   Max.   :14.41  
##    h con DPV     DPV min (kpa)      HR max (%)    h diarias HR >65%
##  Min.   : 3.00   Min.   :0.0000   Min.   :74.41   Min.   :14.00    
##  1st Qu.: 9.00   1st Qu.:0.1504   1st Qu.:83.92   1st Qu.:18.00    
##  Median :11.00   Median :0.2167   Median :89.14   Median :22.00    
##  Mean   :11.68   Mean   :0.2318   Mean   :88.58   Mean   :21.52    
##  3rd Qu.:14.00   3rd Qu.:0.3200   3rd Qu.:92.81   3rd Qu.:24.00    
##  Max.   :28.00   Max.   :0.5271   Max.   :99.96   Max.   :48.00    
##    HR min (%)    Precipitación [mm] H diarias con pp(mm) Vel viento max(m/s)
##  Min.   :68.06   Min.   : 0.000     Min.   : 0.000       Min.   : 0.900     
##  1st Qu.:77.27   1st Qu.: 0.000     1st Qu.: 0.000       1st Qu.: 1.179     
##  Median :81.80   Median : 0.200     Median : 1.000       Median : 1.337     
##  Mean   :82.32   Mean   : 2.852     Mean   : 1.812       Mean   : 1.980     
##  3rd Qu.:86.83   3rd Qu.: 1.200     3rd Qu.: 3.000       3rd Qu.: 1.529     
##  Max.   :99.96   Max.   :32.800     Max.   :24.000       Max.   :26.400     
##  h diarias con vel viento >2m/s   Incidencia      Severidad 1   
##  Min.   : 0.000                 Min.   : 23.30   Min.   : 7.50  
##  1st Qu.: 4.000                 1st Qu.: 93.75   1st Qu.:12.50  
##  Median : 7.000                 Median : 98.33   Median :12.50  
##  Mean   : 6.507                 Mean   : 83.10   Mean   :14.73  
##  3rd Qu.: 9.000                 3rd Qu.:100.00   3rd Qu.:12.50  
##  Max.   :17.000                 Max.   :100.00   Max.   :32.50  
##    Severidad      Aplicaciones de fungicida
##  Min.   :0.5938   Min.   :0.00000          
##  1st Qu.:1.0000   1st Qu.:0.00000          
##  Median :1.0000   Median :0.00000          
##  Mean   :1.1802   Mean   :0.05797          
##  3rd Qu.:1.0000   3rd Qu.:0.00000          
##  Max.   :2.6000   Max.   :2.00000
str(data2)
## tibble [69 × 17] (S3: tbl_df/tbl/data.frame)
##  $ Dia                           : num [1:69] 2023240 2023241 2023242 2023243 2023244 ...
##  $ h diarias t (10-18)°          : num [1:69] 9 9 22 7 8 11 12 18 13 10 ...
##  $ t max (°C)                    : num [1:69] 15 14.5 14.9 14.7 15 ...
##  $ t min (°C)                    : num [1:69] 13.2 12.4 13.4 13.1 13.3 ...
##  $ h con DPV                     : num [1:69] 17 12 28 14 12 9 9 15 14 16 ...
##  $ DPV min (kpa)                 : num [1:69] 0.451 0.335 0.382 0.497 0.438 ...
##  $ HR max (%)                    : num [1:69] 77 83.5 80.8 76.1 80.3 ...
##  $ h diarias HR >65%             : num [1:69] 15 18 34 14 15 21 20 19 19 14 ...
##  $ HR min (%)                    : num [1:69] 70 74.9 73.7 69.7 72.9 ...
##  $ Precipitación [mm]            : num [1:69] 0 13.4 0 0 0 17.6 0.2 0.4 0 0 ...
##  $ H diarias con pp(mm)          : num [1:69] 0 3 3 0 0 5 1 2 0 0 ...
##  $ Vel viento max(m/s)           : num [1:69] 1.53 1.3 1.67 1.78 1.56 ...
##  $ h diarias con vel viento >2m/s: num [1:69] 7 8 17 11 9 8 4 7 9 10 ...
##  $ Incidencia                    : num [1:69] 23.3 23.3 23.3 23.3 23.3 23.3 23.3 23.3 23.3 23.3 ...
##  $ Severidad 1                   : num [1:69] 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 ...
##  $ Severidad                     : num [1:69] 0.594 0.594 0.594 0.594 0.594 ...
##  $ Aplicaciones de fungicida     : num [1:69] 0 0 0 0 0 0 0 0 0 0 ...
attach(data2)
hist(data2$Severidad)

# Ajustar el modelo lineal generalizado

mod4 <- glm(log(Severidad) ~ ., data = data2)
summary(mod4)
## 
## Call:
## glm(formula = log(Severidad) ~ ., data = data2)
## 
## Coefficients:
##                                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                       2.924e+00  1.629e+02   0.018   0.9858    
## Dia                              -2.062e-06  8.052e-05  -0.026   0.9797    
## `h diarias t (10-18)°`            1.756e-03  1.639e-03   1.071   0.2890    
## `t max (°C)`                      1.473e-02  3.129e-02   0.471   0.6397    
## `t min (°C)`                     -5.171e-03  2.694e-02  -0.192   0.8486    
## `h con DPV`                      -5.799e-04  2.081e-03  -0.279   0.7817    
## `DPV min (kpa)`                   7.589e-02  2.393e-01   0.317   0.7524    
## `HR max (%)`                     -6.614e-03  7.817e-03  -0.846   0.4013    
## `h diarias HR >65%`              -4.871e-04  1.511e-03  -0.322   0.7485    
## `HR min (%)`                      8.800e-03  4.679e-03   1.881   0.0656 .  
## `Precipitación [mm]`              2.032e-04  6.209e-04   0.327   0.7447    
## `H diarias con pp(mm)`            2.550e-04  1.668e-03   0.153   0.8791    
## `Vel viento max(m/s)`             2.205e-03  1.369e-03   1.611   0.1132    
## `h diarias con vel viento >2m/s` -2.411e-03  2.143e-03  -1.125   0.2657    
## Incidencia                        3.661e-03  1.779e-04  20.582   <2e-16 ***
## `Severidad 1`                     4.795e-02  6.308e-04  76.023   <2e-16 ***
## `Aplicaciones de fungicida`      -6.464e-03  1.209e-02  -0.535   0.5951    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.0007981258)
## 
##     Null deviance: 13.026376  on 68  degrees of freedom
## Residual deviance:  0.041503  on 52  degrees of freedom
## AIC: -279.9
## 
## Number of Fisher Scoring iterations: 2
# Obtener los coeficientes del modelo
coeficientes <- coef(mod4) 


# División de los datos en train y test
# ==============================================================================
set.seed(123)

n_filas_prediccion <- 69

id_train2 <- sample(nrow(data2), size = 0.7*nrow(data2), replace = F)
str(id_train2)
##  int [1:48] 31 51 14 42 50 54 43 37 52 67 ...
id_test2 <- setdiff(nrow(data2), id_train2)

datos_train2 <- data2[id_train2, ]
str(datos_train2)
## tibble [48 × 17] (S3: tbl_df/tbl/data.frame)
##  $ Dia                           : num [1:48] 2023270 2023290 2023453 2023281 2023289 ...
##  $ h diarias t (10-18)°          : num [1:48] 9 16 20 18 14 13 11 12 15 23 ...
##  $ t max (°C)                    : num [1:48] 13.4 13.9 15 15.5 14.3 ...
##  $ t min (°C)                    : num [1:48] 11.6 12.3 13.8 14.3 12.8 ...
##  $ h con DPV                     : num [1:48] 9 7 11 11 10 10 9 11 11 5 ...
##  $ DPV min (kpa)                 : num [1:48] 0.157 0.125 0.263 0.207 0.173 ...
##  $ HR max (%)                    : num [1:48] 92.7 93.7 86.9 90.3 90.9 ...
##  $ h diarias HR >65%             : num [1:48] 22 24 20 22 23 23 22 24 20 24 ...
##  $ HR min (%)                    : num [1:48] 86.3 88.2 79.7 84.4 84 ...
##  $ Precipitación [mm]            : num [1:48] 1.4 32.8 0 0 0 0 23.6 0.6 0.2 8.4 ...
##  $ H diarias con pp(mm)          : num [1:48] 3 3 0 0 0 0 3 2 1 8 ...
##  $ Vel viento max(m/s)           : num [1:48] 1.3 1.27 1.3 1.33 1.24 ...
##  $ h diarias con vel viento >2m/s: num [1:48] 6 5 7 5 4 5 7 6 7 3 ...
##  $ Incidencia                    : num [1:48] 100 98.3 23.3 93.8 98.3 ...
##  $ Severidad 1                   : num [1:48] 32.5 12.5 7.5 12.5 12.5 ...
##  $ Severidad                     : num [1:48] 2.6 1 0.594 1 1 ...
##  $ Aplicaciones de fungicida     : num [1:48] 0 0 0 0 0 0 0 0 0 0 ...
datos_test2  <- data2[-id_train2, ]
str(datos_test2)
## tibble [21 × 17] (S3: tbl_df/tbl/data.frame)
##  $ Dia                           : num [1:21] 2023240 2023242 2023243 2023450 2023452 ...
##  $ h diarias t (10-18)°          : num [1:21] 9 22 7 18 8 7 17 10 15 12 ...
##  $ t max (°C)                    : num [1:21] 15 14.9 14.7 14.3 14.5 ...
##  $ t min (°C)                    : num [1:21] 13.2 13.4 13.1 12.8 12.4 ...
##  $ h con DPV                     : num [1:21] 17 28 14 16 13 11 15 17 14 10 ...
##  $ DPV min (kpa)                 : num [1:21] 0.451 0.382 0.497 0.32 0.336 ...
##  $ HR max (%)                    : num [1:21] 77 80.8 76.1 82.8 83.1 ...
##  $ h diarias HR >65%             : num [1:21] 15 34 14 18 18 17 21 16 16 19 ...
##  $ HR min (%)                    : num [1:21] 70 73.7 69.7 76.1 77.5 ...
##  $ Precipitación [mm]            : num [1:21] 0 0 0 0 0 0.2 1.6 0 0 0 ...
##  $ H diarias con pp(mm)          : num [1:21] 0 3 0 0 0 1 3 0 0 0 ...
##  $ Vel viento max(m/s)           : num [1:21] 1.53 1.67 1.78 1.29 1.61 ...
##  $ h diarias con vel viento >2m/s: num [1:21] 7 17 11 7 10 4 10 3 8 6 ...
##  $ Incidencia                    : num [1:21] 23.3 23.3 23.3 23.3 23.3 100 100 100 100 100 ...
##  $ Severidad 1                   : num [1:21] 7.5 7.5 7.5 7.5 7.5 12.5 12.5 12.5 12.5 12.5 ...
##  $ Severidad                     : num [1:21] 0.594 0.594 0.594 0.594 0.594 ...
##  $ Aplicaciones de fungicida     : num [1:21] 0 0 0 0 0 0 0 0 1 0 ...
# Ajustar el modelo con los datos de entrenamiento
modelo2 <- glm(Severidad ~ ., data = datos_train2)
summary(modelo2)
## 
## Call:
## glm(formula = Severidad ~ ., data = datos_train2)
## 
## Coefficients:
##                                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                      -1.908e+01  5.669e+01  -0.337    0.739    
## Dia                               9.420e-06  2.801e-05   0.336    0.739    
## `h diarias t (10-18)°`            1.008e-03  6.031e-04   1.671    0.105    
## `t max (°C)`                      4.647e-03  1.169e-02   0.397    0.694    
## `t min (°C)`                     -3.346e-03  9.703e-03  -0.345    0.733    
## `h con DPV`                      -1.260e-03  8.784e-04  -1.434    0.161    
## `DPV min (kpa)`                   3.373e-03  9.040e-02   0.037    0.970    
## `HR max (%)`                     -1.361e-03  3.023e-03  -0.450    0.656    
## `h diarias HR >65%`              -3.476e-04  4.547e-04  -0.764    0.450    
## `HR min (%)`                      1.393e-03  1.715e-03   0.812    0.423    
## `Precipitación [mm]`             -1.395e-04  2.265e-04  -0.616    0.543    
## `H diarias con pp(mm)`            1.009e-03  1.032e-03   0.978    0.336    
## `Vel viento max(m/s)`            -5.857e-04  1.256e-03  -0.467    0.644    
## `h diarias con vel viento >2m/s`  5.198e-04  9.310e-04   0.558    0.581    
## Incidencia                        7.819e-05  5.662e-05   1.381    0.177    
## `Severidad 1`                     8.014e-02  2.143e-04 373.966   <2e-16 ***
## `Aplicaciones de fungicida`      -1.400e-03  3.517e-03  -0.398    0.693    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 5.676701e-05)
## 
##     Null deviance: 16.7156510  on 47  degrees of freedom
## Residual deviance:  0.0017598  on 31  degrees of freedom
## AIC: -318.04
## 
## Number of Fisher Scoring iterations: 2
predicciones2 <- predict(modelo2, newdata = datos_test2, interval="prediction", level= 0.95)

#graf <- cbind(data2, predicciones2)

str(predicciones2)
##  Named num [1:21] 0.585 0.584 0.589 0.595 0.593 ...
##  - attr(*, "names")= chr [1:21] "1" "2" "3" "4" ...
#ggplot(datos_test, aes(x = data2$`HR max (%)`, y = data2$Severidad)) +
 # geom_point(color="blue") +
  #geom_line(aes(y = predicciones2), color = "red")

# Crear un gráfico de dispersión con las predicciones y los datos
#plot(data1$`h HR>65%`, data1$Incidencia, col = "blue", main = "Predicciones del modelo vs. Datos reales", xlab = "Temperatura", ylab = "Incidencia")
 #points(data1$Incidencia, predicciones, col = "red")
#legend("topright", legend = c("Datos reales", "Predicciones"), col = c("blue", "red"), pch = 1)


# Evaluar el modelo en los datos de prueba
r22 <- summary(modelo2)$r.squared
mse2 <- mean((datos_test2$Severidad - predicciones2)^2)
print(paste0("Coeficiente de determinación R^2: ", r22))
## [1] "Coeficiente de determinación R^2: "
print(paste0("Mean squared error: ", mse2))
## [1] "Mean squared error: 9.68275903973555e-05"