You are on page 1of 8

# Taller_por_el_Origen.

R
user

2019-04-06
# Taller Regresión por el origen.
y = c(576, 497, 789, 862, 361, 688, 532)
x1 = c(112, 122, 147, 173, 94, 151, 109)

# Las medias

y1=y-mean(y)
y1

xx=x1-mean(x1)
xx

## ## [1] -17.714286 -7.714286 17.285714 43.285714 -35.714286 21.285714

## [7] -20.714286

## # Modelos d e regresion para cada ejemplo

mod=lm(y~x1)
mod1=lm(y~x1 - 1)
mod2=lm(y1~xx)

## # Sumario de los modelos para cada ejemplo

summary(mod)

##
## Call:
## lm(formula = y ~ x1)
##
## Residuals:
## 1 2 3 4 5 6 7
## 65.118 -72.658 72.401 -7.417 -44.085 -52.109 38.751
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -147.4122 124.9309 -1.180 0.29110
## x1 5.8776 0.9445 6.223 0.00157 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 64.77 on 5 degrees of freedom
## Multiple R-squared: 0.8857, Adjusted R-squared: 0.8628
## F-statistic: 38.73 on 1 and 5 DF, p-value: 0.001567

summary(mod1)

##
## Call:
## lm(formula = y ~ x1 - 1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -88.77 -60.63 10.45 37.16 85.63
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## x1 4.785 0.191 25.05 2.67e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 66.86 on 6 degrees of freedom
## Multiple R-squared: 0.9905, Adjusted R-squared: 0.9889
## F-statistic: 627.4 on 1 and 6 DF, p-value: 2.666e-07

summary(mod2)

##
## Call:
## lm(formula = y1 ~ xx)
##
## Residuals:
## 1 2 3 4 5 6 7
## 65.118 -72.658 72.401 -7.417 -44.085 -52.109 38.751
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.525e-14 2.448e+01 0.000 1.00000
## xx 5.878e+00 9.445e-01 6.223 0.00157 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 64.77 on 5 degrees of freedom
## Multiple R-squared: 0.8857, Adjusted R-squared: 0.8628
## F-statistic: 38.73 on 1 and 5 DF, p-value: 0.001567

# Grafica de comparación

## plot(x1,y, type="p", xlim=c(0,200),

ylim=c(0,1000));abline(lm(y~x1),col="green");abline(lm(y1~xx),
col="blue");abline(lm(y~x1 - 1), col="red");
#legend(locator(1), c("Original", "Por el origen sin b0","Por el origen
medias"), pch=c(19,19),
#col=c("green","blue","red"))

anova(mod)

## ## Analysis of Variance Table

##
## Response: y
## Df Sum Sq Mean Sq F value Pr(>F)
## x1 1 162487 162487 38.73 0.001567 **
## Residuals 5 20977 4195
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

sum(mod\$residuals)

## [1] -4.263256e-14

sum(y)

## [1] 4305

sum(mod\$fitted.values)
## [1] 4305

sum(mod\$residuals*x1)

## [1] 9.094947e-13

sum(mod\$residuas*mod\$fitted.values)

## [1] 0

total=sum(y^2); total

## [1] 2831039

regresion=sum(mod\$fitted.values^2); regresion

## [1] 2810062

residuales=sum(mod\$residuals^2); residuales

## [1] 20976.99

regresion + residuales

## [1] 2831039

## # Intervalos de confianza para los parámetros.

confint(mod, level=0.90)

## 5 % 95 %
## (Intercept) -399.154112 104.329668
## x1 3.974513 7.780741

anova(mod1)

## ## Analysis of Variance Table

##
## Response: y
## Df Sum Sq Mean Sq F value Pr(>F)
## x1 1 2804221 2804221 627.39 2.666e-07 ***
## Residuals 6 26818 4470
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

sum(mod1\$residuals)
## [1] -39.62477

sum(y)

## [1] 4305

sum(mod1\$fitted.values)

## [1] 4344.625

sum(mod1\$residuals*x1)

## [1] 2.273737e-13

sum(mod1\$residuas*mod1\$fitted.values)

## [1] 0

total=sum(y^2); total

## [1] 2831039

regresion=sum(mod1\$fitted.values^2); regresion

## [1] 2804221

residuales=sum(mod1\$residuals^2); residuales

## [1] 26818.17

regresion + residuales

## [1] 2831039

## # Intervalos de confianza para los parámetros.

confint(mod1, level=0.90)

## 5 % 95 %
## x1 4.413625 5.156033

medias

anova(mod2)

## ## Analysis of Variance Table

##
## Response: y1
## Df Sum Sq Mean Sq F value Pr(>F)
## xx 1 162487 162487 38.73 0.001567 **
## Residuals 5 20977 4195
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

mean(xx)

## [1] -8.120488e-15

sum(mod2\$residuals)

## [1] 3.552714e-15

sum(y1)

## [1] 0

sum(mod2\$fitted.values)

## [1] 0

sum(mod2\$residuals*xx)

## [1] 1.364242e-12

sum(mod2\$residuas*mod2\$fitted.values)

## [1] 0

total=sum(y1^2); total

## [1] 183464

regresion=sum(mod2\$fitted.values^2); regresion

## [1] 162487

residuales=sum(mod2\$residuals^2); residuales

## [1] 20976.99

regresion + residuales

## [1] 183464

## # Intervalos de confianza para los parámetros.

confint(mod2, level=0.90)

## 5 % 95 %
## (Intercept) -49.331360 49.331360
## xx 3.974513 7.780741
# Intervalos de confianza para la predicción

# Predicción

y=c(0,0)
x1=c(100,110)
d1=data.frame(y,x1)

## predict=predict(mod,d1, interval="none"); predict

## 1 2
## 440.3505 499.1268

## ## fit lwr upr

## 1 440.3505 344.6187 536.0823
## 2 499.1268 420.0624 578.1911

## ## fit lwr upr

## 1 440.3505 248.2898 632.4112
## 2 499.1268 314.8066 683.4469

## # Intervalos de confianza para la predicción

# Predicción

y=c(0,0)
x1=c(100,110)
d1=data.frame(y,x1)

## predict=predict(mod1,d1, interval="none"); predict

## 1 2
## 478.4829 526.3312

## predict=predict(mod1,d1, interval="confidence", level=0.95); predict

## fit lwr upr
## 1 478.4829 431.7398 525.2260
## 2 526.3312 474.9138 577.7486

## ## fit lwr upr

## 1 478.4829 308.3457 648.6201
## 2 526.3312 354.8509 697.8115

## # Intervalos de confianza para la predicción

# Predicción

y1=c(0,0)
xx=c(100,110)
d1=data.frame(y1,xx)

## predict=predict(mod2,d1, interval="none"); predict

## 1 2
## 587.7627 646.5390

## ## fit lwr upr

## 1 587.7627 336.9601 838.5653
## 2 646.5390 372.1676 920.9104

## ## fit lwr upr

## 1 587.7627 286.7234 888.8021
## 2 646.5390 325.5991 967.4789