EJEMPLO DE UNTRODUCCIÓN DE REGRESIÓN #Creando valores independientes y dependientes con 5 valores cada uno:
ind = c(0,10,20,30,40) #IND-INDEPENDIENTE
dep = c(4,22,44,60,82) #DEP-DEPENDIENTE
#La regresión lineal se describirá así: dep = bo + b1ind + e =(dep(dependiente)= b0(coeficiente de la intersección)+ b1(coeficiente de la varible)ind(los valores que tome la independiente) +e(el error))
#La sintaxis basica en el R: lm(Y ~ modelo) =lm(Comando lm)(Y(dependiente)~(simbolo virgulilla)modelo
lm(dep~ind) # Formula de la regresion lineal
##
## Call:
## lm(formula = dep ~ ind)
##
## Coefficients:
## (Intercept) ind
## 3.60 1.94
lmr <-lm(dep~ind)#(dependiente~independiente)
#Resumimos la formula con summary
summary(lmr)
##
## Call:
## lm(formula = dep ~ ind)
##
## Residuals:
## 1 2 3 4 5
## 0.4 -1.0 1.6 -1.8 0.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.60000 1.23288 2.92 0.0615 .
## ind 1.94000 0.05033 38.54 3.84e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.592 on 3 degrees of freedom
## Multiple R-squared: 0.998, Adjusted R-squared: 0.9973
## F-statistic: 1486 on 1 and 3 DF, p-value: 3.842e-05
#Los coeficientes: el intecepto y la pendiente de “ind”
coef(lmr)
## (Intercept) ind
## 3.60 1.94
resid(lmr)
## 1 2 3 4 5
## 0.4 -1.0 1.6 -1.8 0.8
fitted(lmr)
## 1 2 3 4 5
## 3.6 23.0 42.4 61.8 81.2
#Veremos la documentación del Fitting Linear Models
?lm
## starting httpd help server ... done
plot(lmr)
#1)Los residuos (aquellos faltantes) y los esperados. Los residuos deben estar distribuidos aleatoriamente.
#2)Grafico Q-Q. Los residuos deben seguir la linea.Para ver la normalidad de los residuos
#3)La raiz cuadrada de los residuos estandarizados y los valores esperados. No debe haber una forma o patron.
#4)Para ver los valores distantes (menores que 1) y Leverage (menores que 0.2).
plot(ind,dep)
#PRIMERA PARTE - OPERACIONES BASICAS
library (foreign)#cargando comando de importacion
ejemplo1 <- read.spss("Intro Reg Lin.sav",use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)#cargando un atchivo de spss
## re-encoding from CP1252
library(Hmisc)
## Warning: package 'Hmisc' was built under R version 4.0.5
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.0.5
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, units
names(ejemplo1)
## [1] "supf" "a_exp" "coste"
attach(ejemplo1)
#MODELANDO-REGRESION LINEAL CON UNA SOLA INDEPENDIENTE
modelo1<- lm(coste ~ supf)
summary(modelo1)
##
## Call:
## lm(formula = coste ~ supf)
##
## Residuals:
## Min 1Q Median 3Q Max
## -138.059 -20.113 4.911 33.304 138.763
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -19.99 26.88 -0.744 0.462
## supf 189.36 24.01 7.888 1.6e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 62.11 on 38 degrees of freedom
## Multiple R-squared: 0.6208, Adjusted R-squared: 0.6109
## F-statistic: 62.22 on 1 and 38 DF, p-value: 1.6e-09
plot(modelo1)
#REGRESION LINEAL CON DOS INDEPENDIENTES
modelo1b<- lm(coste ~ supf + a_exp)
summary(modelo1b)
##
## Call:
## lm(formula = coste ~ supf + a_exp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -142.678 -24.131 1.004 33.206 136.587
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.810 48.402 -0.037 0.970
## supf 188.454 24.342 7.742 2.98e-09 ***
## a_exp -1.214 2.675 -0.454 0.653
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 62.77 on 37 degrees of freedom
## Multiple R-squared: 0.6229, Adjusted R-squared: 0.6025
## F-statistic: 30.56 on 2 and 37 DF, p-value: 1.458e-08