Regresión lineal simple

En este trabajo se presenta una descripción breve del modelo de regresión lineal simple y la forma de estimar los parámetros del modelo con R.

Modelo estadístico

El modelo estadístico de regresión lineal se puede definir como:

\(Y_i=\beta_0+\beta_1X_i+\epsilon_i\)

A continuación se presenta una corta descripción de los parámetros más usados en la función.

  • Formula: es un objeto de la clase fórmula para indicar la variable respuesta y las covariables. Por ejemplo, si formula = \(y \thicksim x_1 + x_2\) lo que se indica es que la variable respuesta es y, las covariables serían x1 y x2.

Ejemplo

A continuación el código para cargar los datos y para mostrar las 27 observaciones de la base de datos.

datos3 <- read.table("C:/Users/kata/Downloads/stats_log.txt", header=TRUE,sep= '')
head(datos3,27) 
##    Estatura L.Pie
## 1       159    36
## 2       164    39
## 3       172    38
## 4       167    37
## 5       164    36
## 6       161    38
## 7       168    39
## 8       181    43
## 9       183    41
## 10      158    36
## 11      156    36
## 12      173    40
## 13      158    36
## 14      178    42
## 15      181    43
## 16      182    41
## 17      176    42
## 18      162    39
## 19      156    36
## 20      152    34
## 21      181    43
## 22      173    41
## 23      155    36
## 24      189    45
## 25      170    38
## 26      170    40
## 27      168    38

Para crear un diagrama de dispersión que nos muestre la relación entre las dos variables usamos las siguientes instrucciones.

library(ggplot2)
ggplot(datos3, aes(x=Estatura, y=L.Pie)) + 
  geom_point() + theme_classic()+
  theme(panel.border = element_rect(fill = "transparent", color = "black", linewidth = 1.5))+
  labs(title = "Estatura V.S Largo del pie",
       subtitle = "Regresión lineal simple",
       tag = "Fig. 1")+ theme(plot.tag.position = "bottomright")+
       geom_smooth(method = "lm", color = "lightblue", se = FALSE)+
  theme(plot.title = element_text(family = "serif",            
                                  face = "bold",                 
                                  color = "lightblue", size=15))+ 
  theme(plot.tag =element_text(family = "serif", face = "bold"))+
  theme(axis.title = element_text(family = "serif", face = "bold")) +
  theme(panel.grid.major.x = element_line(color = "grey",
                                        linewidth = 0.25,
                                        linetype = 1)) +
    theme(panel.grid.minor.y = element_line(color = "grey",
                                        linewidth = 0.25,
                                        linetype = 1)) +
  theme(panel.grid.major.y = element_line(color = "grey",
                                        linewidth = 0.25,
                                        linetype = 1)) +
    theme(panel.grid.minor.x = element_line(color = "grey",
                                       linewidth = 0.25,
                                        linetype = 1)) +
  scale_y_continuous(breaks = seq(30, 50, by = 1)) + 
  scale_x_continuous(breaks = seq(100, 200, by = 5))
## `geom_smooth()` using formula = 'y ~ x'

mod1 <- lm(Estatura ~ L.Pie, data=datos3)
summary(mod1)
## 
## Call:
## lm(formula = Estatura ~ L.Pie, data = datos3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.7778 -2.5797 -0.8155  1.7222  7.5807 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  39.2683    10.2590   3.828  0.00077 ***
## L.Pie         3.3208     0.2624  12.657 2.27e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.82 on 25 degrees of freedom
## Multiple R-squared:  0.865,  Adjusted R-squared:  0.8596 
## F-statistic: 160.2 on 1 and 25 DF,  p-value: 2.274e-12
mod1 # Para imprimir el objeto mod1
## 
## Call:
## lm(formula = Estatura ~ L.Pie, data = datos3)
## 
## Coefficients:
## (Intercept)        L.Pie  
##      39.268        3.321