# REGRESION LINEAL MULTIPLE
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
# Buscar la Ruta de Archivo Exel
# file.choose()
ruta_exel<-"D:\\Modelo Lineales\\RML.xlsx"
RML<-read_excel(ruta_exel)


# Regresion Lineal Multiple
Resultado<- lm(Desocupacion~Importacion+Exportacion,data = RML)
Residuales<-Resultado$residuals
summary(Resultado)
## 
## Call:
## lm(formula = Desocupacion ~ Importacion + Exportacion, data = RML)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.21497 -0.37325 -0.00571  0.44222  1.47034 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  15.5129     0.5979  25.946  < 2e-16 ***
## Importacion  -1.0582     0.1807  -5.856 1.85e-06 ***
## Exportacion  -0.5019     0.2700  -1.859   0.0725 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7021 on 31 degrees of freedom
## Multiple R-squared:  0.832,  Adjusted R-squared:  0.8212 
## F-statistic: 76.77 on 2 and 31 DF,  p-value: 9.817e-13
vcov(Resultado)
##             (Intercept) Importacion Exportacion
## (Intercept)  0.35747740 -0.01399171 -0.07722096
## Importacion -0.01399171  0.03265272 -0.03904429
## Exportacion -0.07722096 -0.03904429  0.07288091
anova(Resultado)
## Analysis of Variance Table
## 
## Response: Desocupacion
##             Df Sum Sq Mean Sq  F value    Pr(>F)    
## Importacion  1 73.988  73.988 150.0760 2.042e-13 ***
## Exportacion  1  1.704   1.704   3.4568   0.07251 .  
## Residuals   31 15.283   0.493                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Nivel de Significancia
confint(Resultado,level = 0.95)
##                 2.5 %      97.5 %
## (Intercept) 14.293468 16.73229453
## Importacion -1.426759 -0.68967635
## Exportacion -1.052527  0.04866601
# Diagramas Plots
RML$Desocupacion<-(RML$Desocupacion)
pairs(x=RML[-1])

# Normalidad
qqnorm(Resultado$residuals)
qqline(Resultado$residuals)

shapiro.test(Resultado$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  Resultado$residuals
## W = 0.9821, p-value = 0.8363
# Correlacion 
names (RML)
## [1] "Mes"          "Desocupacion" "Importacion"  "Exportacion"
head(RML)
## # A tibble: 6 x 4
##   Mes    Desocupacion Importacion Exportacion
##   <chr>         <dbl>       <dbl>       <dbl>
## 1 Marzo          10.6        2.91        2.07
## 2 Abril          10.9        3.19        2.21
## 3 Mayo           11.4        2.77        2.46
## 4 Junio          11.5        3           2.57
## 5 Julio          11.6        3.74        2.84
## 6 Agosto         11.6        3.25        2.83
str(RML)
## tibble [34 x 4] (S3: tbl_df/tbl/data.frame)
##  $ Mes         : chr [1:34] "Marzo" "Abril" "Mayo" "Junio" ...
##  $ Desocupacion: num [1:34] 10.6 10.9 11.4 11.5 11.6 11.6 11.2 10.7 10.4 10 ...
##  $ Importacion : num [1:34] 2.91 3.19 2.77 3 3.74 3.25 3.25 3.89 4 4.17 ...
##  $ Exportacion : num [1:34] 2.07 2.21 2.46 2.57 2.84 2.83 2.52 3.79 3.41 4.02 ...
RML1<-RML[,-1]
names(RML1)
## [1] "Desocupacion" "Importacion"  "Exportacion"
cor(RML1$Desocupacion,RML1$Importacion)
## [1] -0.9018179
cor(RML1$Desocupacion,RML1$Exportacion)
## [1] -0.8038411
round(cor(RML1),3)
##              Desocupacion Importacion Exportacion
## Desocupacion        1.000      -0.902      -0.804
## Importacion        -0.902       1.000       0.800
## Exportacion        -0.804       0.800       1.000
plot(RML1)

# Test of F-Fisher
var.test(RML$Importacion,RML$Exportacion)
## 
##  F test to compare two variances
## 
## data:  RML$Importacion and RML$Exportacion
## F = 2.232, num df = 33, denom df = 33, p-value = 0.02386
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  1.114717 4.469142
## sample estimates:
## ratio of variances 
##           2.232001
var(RML$Importacion)/var(RML$Exportacion)
## [1] 2.232001
# Test of Breusch-Pagan
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.0.2
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

bptest(Resultado)
## 
##  studentized Breusch-Pagan test
## 
## data:  Resultado
## BP = 3.0702, df = 2, p-value = 0.2154