# Regresión lineal

set.seed(10)

x1 <- rnorm(30,10)
x2 <- rnorm(30,20)
x3 <- rnorm(30,30)
e <- rnorm(30)

y <- x1 + 2 * x2 + 3 * x3 + e

x <- data.frame(x1=x1,x2=x2,x3=x3)

y.lm <- lm(y ~ ., data=x) # Se añanden las 3 variables 

print(summary(y.lm))
## 
## Call:
## lm(formula = y ~ ., data = x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.8401 -0.6310  0.3031  0.7778  1.1017 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.5446     6.9612   0.366 0.717662    
## x1            0.9472     0.2208   4.289 0.000219 ***
## x2            1.9947     0.2104   9.480 6.37e-10 ***
## x3            2.9412     0.1929  15.244 1.76e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9739 on 26 degrees of freedom
## Multiple R-squared:  0.9453, Adjusted R-squared:  0.939 
## F-statistic: 149.9 on 3 and 26 DF,  p-value: < 2.2e-16
# Predecir valores a partir del modelo de la regresión
nuevo <- data.frame(x1=29,x2=21,x3=33)
prediccion <- predict(y.lm,newdata=nuevo,interval="confidence")
print(prediccion)
##        fit      lwr    upr
## 1 168.9591 159.8582 178.06