library(ISLR)
## Warning: package 'ISLR' was built under R version 3.4.4
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.4.4
## 
## 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
set.seed(08)
Auto%>%filter(origin==02)%>%
  select(mpg,cylinders,displacement,horsepower,weight,acceleration)%>%
  sample_n(size=50,replace=FALSE)->datos_parcial

Modelo de Regresion Multiple

library(stargazer)
## Warning: package 'stargazer' was built under R version 3.4.4
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
options(scipen = 9999)
modelo_lineal<-lm(formula = mpg~cylinders+displacement+horsepower+weight+acceleration,data = datos_parcial)
summary(modelo_lineal)
## 
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight + 
##     acceleration, data = datos_parcial)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.6230  -2.3799  -0.6386   1.7206   9.5078 
## 
## Coefficients:
##               Estimate Std. Error t value  Pr(>|t|)    
## (Intercept)  43.248241   9.055414   4.776 0.0000201 ***
## cylinders     1.837901   1.762651   1.043    0.3028    
## displacement -0.018272   0.072907  -0.251    0.8033    
## horsepower   -0.177105   0.072270  -2.451    0.0183 *  
## weight       -0.003394   0.003621  -0.937    0.3537    
## acceleration  0.085544   0.374206   0.229    0.8202    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.776 on 44 degrees of freedom
## Multiple R-squared:  0.5208, Adjusted R-squared:  0.4663 
## F-statistic: 9.562 on 5 and 44 DF,  p-value: 0.000003155
stargazer(modelo_lineal,title="regresion multiple", type="text",digits=8)
## 
## regresion multiple
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                 mpg            
## -----------------------------------------------
## cylinders                   1.83790100         
##                            (1.76265100)        
##                                                
## displacement                -0.01827223        
##                            (0.07290731)        
##                                                
## horsepower                 -0.17710500**       
##                            (0.07227045)        
##                                                
## weight                      -0.00339445        
##                            (0.00362134)        
##                                                
## acceleration                0.08554387         
##                            (0.37420570)        
##                                                
## Constant                  43.24824000***       
##                            (9.05541400)        
##                                                
## -----------------------------------------------
## Observations                    50             
## R2                          0.52075350         
## Adjusted R2                 0.46629370         
## Residual Std. Error    4.77618200 (df = 44)    
## F Statistic         9.56215900*** (df = 5; 44) 
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

nuevo modelo de regresion lineal

library(stargazer)
options(scipen = 9999)
modelo_lineal2<-lm(formula = mpg~cylinders+displacement+horsepower+weight,data = datos_parcial)
summary(modelo_lineal)
## 
## Call:
## lm(formula = mpg ~ cylinders + displacement + horsepower + weight + 
##     acceleration, data = datos_parcial)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.6230  -2.3799  -0.6386   1.7206   9.5078 
## 
## Coefficients:
##               Estimate Std. Error t value  Pr(>|t|)    
## (Intercept)  43.248241   9.055414   4.776 0.0000201 ***
## cylinders     1.837901   1.762651   1.043    0.3028    
## displacement -0.018272   0.072907  -0.251    0.8033    
## horsepower   -0.177105   0.072270  -2.451    0.0183 *  
## weight       -0.003394   0.003621  -0.937    0.3537    
## acceleration  0.085544   0.374206   0.229    0.8202    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.776 on 44 degrees of freedom
## Multiple R-squared:  0.5208, Adjusted R-squared:  0.4663 
## F-statistic: 9.562 on 5 and 44 DF,  p-value: 0.000003155
stargazer(modelo_lineal,title="regresion multiple", type="text",digits=8)
## 
## regresion multiple
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                 mpg            
## -----------------------------------------------
## cylinders                   1.83790100         
##                            (1.76265100)        
##                                                
## displacement                -0.01827223        
##                            (0.07290731)        
##                                                
## horsepower                 -0.17710500**       
##                            (0.07227045)        
##                                                
## weight                      -0.00339445        
##                            (0.00362134)        
##                                                
## acceleration                0.08554387         
##                            (0.37420570)        
##                                                
## Constant                  43.24824000***       
##                            (9.05541400)        
##                                                
## -----------------------------------------------
## Observations                    50             
## R2                          0.52075350         
## Adjusted R2                 0.46629370         
## Residual Std. Error    4.77618200 (df = 44)    
## F Statistic         9.56215900*** (df = 5; 44) 
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

presentacion de resultados

stargazer(modelo_lineal,modelo_lineal2 , title = "tabla de regresion para data frame 1 2 y 3", type = "text", digits = 8)
## 
## tabla de regresion para data frame 1 2 y 3
## ==========================================================================
##                                      Dependent variable:                  
##                     ------------------------------------------------------
##                                              mpg                          
##                                (1)                         (2)            
## --------------------------------------------------------------------------
## cylinders                   1.83790100                 1.81696900         
##                            (1.76265100)               (1.74163600)        
##                                                                           
## displacement               -0.01827223                 -0.01992219        
##                            (0.07290731)               (0.07178113)        
##                                                                           
## horsepower                -0.17710500**              -0.19033030***       
##                            (0.07227045)               (0.04285440)        
##                                                                           
## weight                     -0.00339445                 -0.00290977        
##                            (0.00362134)               (0.00290469)        
##                                                                           
## acceleration                0.08554387                                    
##                            (0.37420570)                                   
##                                                                           
## Constant                  43.24824000***             44.85228000***       
##                            (9.05541400)               (5.66354900)        
##                                                                           
## --------------------------------------------------------------------------
## Observations                    50                         50             
## R2                          0.52075350                 0.52018430         
## Adjusted R2                 0.46629370                 0.47753400         
## Residual Std. Error    4.77618200 (df = 44)       4.72561900 (df = 45)    
## F Statistic         9.56215900*** (df = 5; 44) 12.19650000*** (df = 4; 45)
## ==========================================================================
## Note:                                          *p<0.1; **p<0.05; ***p<0.01

intervalos de confianza para los modelos 1, 2

confint(object = modelo_lineal,level = .95)
##                    2.5 %       97.5 %
## (Intercept)  24.99825243 61.498229214
## cylinders    -1.71448876  5.390289980
## displacement -0.16520726  0.128662808
## horsepower   -0.32275648 -0.031453420
## weight       -0.01069278  0.003903872
## acceleration -0.66861820  0.839705943
confint(object = modelo_lineal2,level = .95)
##                     2.5 %       97.5 %
## (Intercept)  33.445303556 56.259248988
## cylinders    -1.690865612  5.324804332
## displacement -0.164496802  0.124652424
## horsepower   -0.276643488 -0.104017087
## weight       -0.008760115  0.002940578

vector de coeficientes estimados

options(scipen=999)
modelo_lineal$coefficients
##  (Intercept)    cylinders displacement   horsepower       weight 
## 43.248240823  1.837900608 -0.018272227 -0.177104948 -0.003394454 
## acceleration 
##  0.085543869
modelo_lineal2$coefficients
##  (Intercept)    cylinders displacement   horsepower       weight 
## 44.852276272  1.816969360 -0.019922189 -0.190330288 -0.002909769