library(nortest)
data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
plot(mtcars$wt, mtcars$mpg)
grid()

boxplot(mtcars)

boxplot(mtcars[, c(1,2,5:11)])

hist(mtcars$mpg)

plot(density(mtcars$mpg))
shapiro.test(mtcars$mpg)
## 
##  Shapiro-Wilk normality test
## 
## data:  mtcars$mpg
## W = 0.94756, p-value = 0.1229
#Regresión
modelo1 = lm(mpg ~ wt, data= mtcars)
summary(modelo1)
## 
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528, Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10
## mpg (rendimineto se estima en 37,2851 menos la pendiente -5.3441 por el peso wt)
## el menos -5.34 dice que por canda unidad de peso el redimiento perdido es de -5.3441


## SUPUESTO DE NORMALODAD

u= modelo1$residuals

#media de 0.

summary(u)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -4.5432 -2.3647 -0.1252  0.0000  1.4096  6.8727
#Ho: errores con distribución normal

shapiro.test(u)
## 
##  Shapiro-Wilk normality test
## 
## data:  u
## W = 0.94508, p-value = 0.1044
#p-value = 0.1044 se asume que tiene una distribucionj normal.


# test de normalidad de anderson
ad.test(u)
## 
##  Anderson-Darling normality test
## 
## data:  u
## A = 0.46842, p-value = 0.233
# p-value = 0.233 confrima la distribucon normal


# kolmogorov smirnoff
lillie.test(u)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  u
## D = 0.082516, p-value = 0.8379
# p-value = 0.8379 confrima la distribucon normal

## VARIANZA DE LOS ERRORES
##bruspegan para heterecedasticiad
lmtest::bptest(modelo1)

## 
##  studentized Breusch-Pagan test
## 
## data:  modelo1
## BP = 0.040438, df = 1, p-value = 0.8406
# p-value = 0.8406 se asume que la varianza es  constante

##errores indpendientes - no autocorrelacion

lmtest::dwtest(modelo1)
## 
##  Durbin-Watson test
## 
## data:  modelo1
## DW = 1.2517, p-value = 0.0102
## alternative hypothesis: true autocorrelation is greater than 0
## p-value = 0.0102 hay relaxcion con los errores, el modelo cumple todo menos la autocorrelacion de los errores.
library(MASS)
boxcox(lm(mpg ~ wt, data = mtcars))

## cuando lamda esta mas cerca de 0 la mejor trasnformacion es la logaritmica.

modelo2= lm(log(mpg) ~ wt, data = mtcars)
summary(modelo2)
## 
## Call:
## lm(formula = log(mpg) ~ wt, data = mtcars)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.210346 -0.085932 -0.006136  0.061335  0.308623 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.83191    0.08396   45.64  < 2e-16 ***
## wt          -0.27178    0.02500  -10.87 6.31e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1362 on 30 degrees of freedom
## Multiple R-squared:  0.7976, Adjusted R-squared:  0.7908 
## F-statistic: 118.2 on 1 and 30 DF,  p-value: 6.31e-12
## por un incremento unitario en el peso el rendimeinto se va a reduri en un -27,178%

lmtest::bptest(modelo2)
## 
##  studentized Breusch-Pagan test
## 
## data:  modelo2
## BP = 1.7974, df = 1, p-value = 0.18
u2=modelo2$residuals
shapiro.test(u2)
## 
##  Shapiro-Wilk normality test
## 
## data:  u2
## W = 0.96184, p-value = 0.3081
lmtest::bptest(modelo2)
## 
##  studentized Breusch-Pagan test
## 
## data:  modelo2
## BP = 1.7974, df = 1, p-value = 0.18
lmtest::dwtest(modelo2)
## 
##  Durbin-Watson test
## 
## data:  modelo2
## DW = 1.5016, p-value = 0.06009
## alternative hypothesis: true autocorrelation is greater than 0
boxplot(mtcars$wt)

modelo3 = lm(mpg ~log(wt), data = mtcars)
modelo4 = lm(log(mpg) ~ log(wt), data = mtcars)
stargazer::stargazer(modelo1, modelo2, modelo3, modelo4, type="text", df=FALSE)
## 
## ==============================================================
##                                Dependent variable:            
##                     ------------------------------------------
##                        mpg     log(mpg)     mpg      log(mpg) 
##                        (1)       (2)        (3)        (4)    
## --------------------------------------------------------------
## wt                  -5.344*** -0.272***                       
##                      (0.559)   (0.025)                        
##                                                               
## log(wt)                                  -17.086*** -0.842*** 
##                                           (1.510)    (0.075)  
##                                                               
## Constant            37.285***  3.832***  39.257***   3.902*** 
##                      (1.878)   (0.084)    (1.758)    (0.088)  
##                                                               
## --------------------------------------------------------------
## Observations           32         32         32         32    
## R2                    0.753     0.798      0.810      0.806   
## Adjusted R2           0.745     0.791      0.804      0.799   
## Residual Std. Error   3.046     0.136      2.669      0.133   
## F Statistic         91.375*** 118.191*** 128.016*** 124.360***
## ==============================================================
## Note:                              *p<0.1; **p<0.05; ***p<0.01