#Cargar librerias
library(readr)
# install.packages("corrplot") # Nuevo
library(corrplot) # Para correlación
## corrplot 0.84 loaded
library(caret)  # Para dividir conjunto de datos
## Loading required package: lattice
## Loading required package: ggplot2
# install.packages("MASS") # NUEVO
library(MASS)

#Datos
oxidoNit <- c(0.90, 0.91, 0.96, 0.89, 1.00, 1.10, 1.15, 1.03, 0.77, 1.07, 1.07, 0.94, 1.10, 1.10, 1.10, 0.91, 0.87, 0.78, 0.82, 0.95)
humedad <- c(72.4, 41.6, 34.3, 35.1, 10.7, 12.9, 8.3, 20.1, 72.2, 24.0, 23.2, 47.4, 31.5, 10.6, 11.2, 73.3, 75.4, 96.6, 107.4, 54.9)
temperatura <- c(76.3, 70.3, 77.1, 68.0, 79.0, 67.4, 66.8, 76.9, 77.7, 67.7, 76.8, 86.6, 76.9, 86.3, 86.0, 76.3, 77.9, 78.7, 86.8, 70.9)
presion <- c(29.18, 29.35, 29.24, 29.27, 29.78, 29.39, 29.69, 29.48, 29.09, 29.60, 29.38, 29.35, 29.63, 29.56, 29.48, 29.40, 29.28, 29.29, 29.03, 29.37)

datos <- data.frame(oxidoNit, humedad, temperatura, presion)
datos
##    oxidoNit humedad temperatura presion
## 1      0.90    72.4        76.3   29.18
## 2      0.91    41.6        70.3   29.35
## 3      0.96    34.3        77.1   29.24
## 4      0.89    35.1        68.0   29.27
## 5      1.00    10.7        79.0   29.78
## 6      1.10    12.9        67.4   29.39
## 7      1.15     8.3        66.8   29.69
## 8      1.03    20.1        76.9   29.48
## 9      0.77    72.2        77.7   29.09
## 10     1.07    24.0        67.7   29.60
## 11     1.07    23.2        76.8   29.38
## 12     0.94    47.4        86.6   29.35
## 13     1.10    31.5        76.9   29.63
## 14     1.10    10.6        86.3   29.56
## 15     1.10    11.2        86.0   29.48
## 16     0.91    73.3        76.3   29.40
## 17     0.87    75.4        77.9   29.28
## 18     0.78    96.6        78.7   29.29
## 19     0.82   107.4        86.8   29.03
## 20     0.95    54.9        70.9   29.37
#Longitudes de observaciones de cada variable
length(oxidoNit)
## [1] 20
length(temperatura)
## [1] 20
length(humedad)
## [1] 20
length(presion)
## [1] 20
#Cargar los datos a una variable data.frame
datos <- data.frame(oxidoNit, humedad, temperatura, presion)
datos
##    oxidoNit humedad temperatura presion
## 1      0.90    72.4        76.3   29.18
## 2      0.91    41.6        70.3   29.35
## 3      0.96    34.3        77.1   29.24
## 4      0.89    35.1        68.0   29.27
## 5      1.00    10.7        79.0   29.78
## 6      1.10    12.9        67.4   29.39
## 7      1.15     8.3        66.8   29.69
## 8      1.03    20.1        76.9   29.48
## 9      0.77    72.2        77.7   29.09
## 10     1.07    24.0        67.7   29.60
## 11     1.07    23.2        76.8   29.38
## 12     0.94    47.4        86.6   29.35
## 13     1.10    31.5        76.9   29.63
## 14     1.10    10.6        86.3   29.56
## 15     1.10    11.2        86.0   29.48
## 16     0.91    73.3        76.3   29.40
## 17     0.87    75.4        77.9   29.28
## 18     0.78    96.6        78.7   29.29
## 19     0.82   107.4        86.8   29.03
## 20     0.95    54.9        70.9   29.37
#Explorando los datos
summary(datos)
##     oxidoNit         humedad        temperatura       presion     
##  Min.   :0.7700   Min.   :  8.30   Min.   :66.80   Min.   :29.03  
##  1st Qu.:0.8975   1st Qu.: 18.30   1st Qu.:70.75   1st Qu.:29.28  
##  Median :0.9550   Median : 34.70   Median :76.90   Median :29.38  
##  Mean   :0.9710   Mean   : 43.16   Mean   :76.52   Mean   :29.39  
##  3rd Qu.:1.0775   3rd Qu.: 72.25   3rd Qu.:78.78   3rd Qu.:29.50  
##  Max.   :1.1500   Max.   :107.40   Max.   :86.80   Max.   :29.78
str(datos)
## 'data.frame':    20 obs. of  4 variables:
##  $ oxidoNit   : num  0.9 0.91 0.96 0.89 1 1.1 1.15 1.03 0.77 1.07 ...
##  $ humedad    : num  72.4 41.6 34.3 35.1 10.7 12.9 8.3 20.1 72.2 24 ...
##  $ temperatura: num  76.3 70.3 77.1 68 79 67.4 66.8 76.9 77.7 67.7 ...
##  $ presion    : num  29.2 29.4 29.2 29.3 29.8 ...
#Graficas
#Visualizando los datos con plot()
plot(datos)

#Correlaciones 
cor(x=datos, method = "pearson")
##               oxidoNit    humedad temperatura    presion
## oxidoNit     1.0000000 -0.8773635  -0.1807651  0.7740084
## humedad     -0.8773635  1.0000000   0.2535071 -0.7554882
## temperatura -0.1807651  0.2535071   1.0000000 -0.1936361
## presion      0.7740084 -0.7554882  -0.1936361  1.0000000
#Mostrando correlaciones con pairs()
pairs(x=datos, lower.panel = NULL)

corrplot(corr = cor(x=datos, method = "pearson"), method = "number")

modelo <- lm(oxidoNit ~ ., data = datos)

modelo
## 
## Call:
## lm(formula = oxidoNit ~ ., data = datos)
## 
## Coefficients:
## (Intercept)      humedad  temperatura      presion  
##  -3.5077781   -0.0026250    0.0007989    0.1541550
#Probando el modelo con un nuevo dato
#Para una humedad de 50%,
#temperatura de 76 ◦F y
#presión barométrica de 29.30,
#¿Cuánto es la cantidad estimada de óxido nitroso
h = 50
t = 76
p = 29.30
nuevodato <- data.frame(humedad = h, temperatura = t, presion = p)
nuevodato
##   humedad temperatura presion
## 1      50          76    29.3
y.predict <- predict(modelo, nuevodato)
y.predict
##         1 
## 0.9384342
# Es lo mismo si usamos los coeficientes

y.predict.2 <- modelo$coefficients[1] + modelo$coefficients[2] * h + modelo$coefficients[3] * t + modelo$coefficients[4] * p  
y.predict.2
## (Intercept) 
##   0.9384342
#¿El modelo predice bien o mal?
#Ver ificar las las correlaciones
#El coefieicnete de determinanción
#Las significaciones
summary(modelo)
## 
## Call:
## lm(formula = oxidoNit ~ ., data = datos)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.11799 -0.02526  0.01345  0.04103  0.06523 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -3.5077781  3.0048641  -1.167  0.26017   
## humedad     -0.0026250  0.0006549  -4.008  0.00101 **
## temperatura  0.0007989  0.0020451   0.391  0.70121   
## presion      0.1541550  0.1013675   1.521  0.14784   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.05617 on 16 degrees of freedom
## Multiple R-squared:  0.8005, Adjusted R-squared:  0.763 
## F-statistic:  21.4 on 3 and 16 DF,  p-value: 7.609e-06
#Interpretación
#Se realizaron 20 obsevaciones de tipo numerico
#hay una importante correlacion entre la humedad con la variable independiente de tipo negativa y es inversamente proporcional -hay un 80% entre las variables con las dependientes
#Con respeto a la predicción, el intervalo de predicción es mayor que el de confianza.
#Se espera que en promedio el óxido nitroso tenga un valor de 0.9384342 ppm.

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.