1.- Cargar los datos 2.- Las librerías 3.- Explorando los datos 4.- Realizar la gráfica de dispersión 5.- Identificar Correlación de Pearson 6.- Modelo de regresión lineal simple 7.- Predecir peso de una persona en función de la altuta 8.-Interpretar el modelo de regresión lineal simple
datos <- women
library(dplyr)
##
## 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
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(knitr)
library(ggplot2)
str(datos)
## 'data.frame': 15 obs. of 2 variables:
## $ height: num 58 59 60 61 62 63 64 65 66 67 ...
## $ weight: num 115 117 120 123 126 129 132 135 139 142 ...
summary(datos)
## height weight
## Min. :58.0 Min. :115.0
## 1st Qu.:61.5 1st Qu.:124.5
## Median :65.0 Median :135.0
## Mean :65.0 Mean :136.7
## 3rd Qu.:68.5 3rd Qu.:148.0
## Max. :72.0 Max. :164.0
ggplot(datos, aes(x = height, y = weight)) +
geom_point()
−0.90 = Correlación negativa muy fuerte. −0.75 = Correlación negativa considerable. −0.50 = Correlación negativa media. −0.25 = Correlación negativa débil. −0.10 = Correlación negativa muy débil. 0.00 = No existe correlación alguna entre las variables. +0.10 = Correlación positiva muy débil. +0.25 = Correlación positiva débil. +0.50 = Correlación positiva media. +0.75 = Correlación positiva considerable. +0.90 = Correlación positiva muy fuerte. +1.00 = Correlación positiva perfecta (“A mayor X, mayor Y” o “a menor X, menor Y”, de manera proporcional. Cada vez que X aumenta, Y aumenta siempre una cantidad constante)
CR <- cor(datos$height, datos$weight) # Pearson
CR
## [1] 0.9954948
modelo <- lm(data = datos, formula = weight ~ height)
# modelo
summary(modelo)
##
## Call:
## lm(formula = weight ~ height, data = datos)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.7333 -1.1333 -0.3833 0.7417 3.1167
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -87.51667 5.93694 -14.74 1.71e-09 ***
## height 3.45000 0.09114 37.85 1.09e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.525 on 13 degrees of freedom
## Multiple R-squared: 0.991, Adjusted R-squared: 0.9903
## F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14
a = modelo$coefficients[1]
b = modelo$coefficients[2]
a
## (Intercept)
## -87.51667
b
## height
## 3.45
ggplot(data = datos, mapping = aes(x = height, y = weight)) +
geom_point(color = "firebrick", size = 2) +
labs(title = 'weight ~ height', x = 'Altura') +
geom_smooth(method = "lm", se = FALSE, color = "black") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5))
## `geom_smooth()` using formula 'y ~ x'
x = c(68,72,78, 63)
y = a + b * x
y
## [1] 147.0833 160.8833 181.5833 129.8333
Predecir conforme a la función predict() Se utilizan los mismos valores de x La función predict() requiere un modelo y el nuevo conjunto de datos que debe tener la variable independiente del mismo nombre height
prediccion <- predict(modelo, newdata = data.frame(height = x))
prediccion
## 1 2 3 4
## 147.0833 160.8833 181.5833 129.8333