In [1]:
# rm(list=ls())
options(OutDec = ",")
#===================================================================
# Consumo e renda agregados dos EUA entre 1940 e 1950.
# O modelo:
# Consumo(y) = beta0 + beta1*Renda(x) + epsilon.
#===================================================================
dataxy <- read.table("../dados/exemplo_08.txt",header=T)
x <- dataxy[,2]
y <- dataxy[,3]
n <- length(x)
options(decimals=10,digits=10)
yfit <- lm(y ~ x)
print(summary(yfit))
print("=========================================================")
print(anova(yfit))
Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -35,346758 -26,439591 9,067752 20,000392 31,642016 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 51,8951087 80,8439749 0,64192 0,536932 x 0,6848014 0,2487525 2,75294 0,022369 * --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1 Residual standard error: 27,58819 on 9 degrees of freedom Multiple R-squared: 0,4571345, Adjusted R-squared: 0,3968162 F-statistic: 7,578693 on 1 and 9 DF, p-value: 0,02236888 [1] "=========================================================" Analysis of Variance Table Response: y Df Sum Sq Mean Sq F value Pr(>F) x 1 5768,2068 5768,2068 7,57869 0,022369 * Residuals 9 6849,9751 761,1083 --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1
In [2]:
#===================================================================
# Dispersão entre renda e consumo.
#===================================================================
par(mfrow=c(1,1),lwd=2.0,cex.lab=1.5,cex.axis=1.5,lab=c(11,10,5),
mar=c(5,5,1,2.5),xpd=T,cex.main=2.0,bty="n")
plot(x,y,pch=15,xlab="Renda (X)",ylab="Consumo (Y)",
xlim=c(230,380),ylim=c(210,340))
points(x[3:6],y[3:6],pch=15,col="blue")
lines(x,yfit$fitted.values)
legend(230,335,legend="Anos da Guerra",pch=15,col="blue",bty="n")
legend(230,330,legend="Ajuste 'Com Guerra'",lty=1,col="black",bty="n")
In [3]:
#===================================================================
# Consumo e renda agregados dos EUA entre 1940 e 1950.
# O modelo:
# Consumo(y) = beta0 + beta1*Renda(x) + epsilon.
# EXCLUINDO as observações de 1942 a 1945.
#===================================================================
zx <- dataxy[c(1:2,7:11),2]
zy <- dataxy[c(1:2,7:11),3]
zyfit <- lm(zy ~ zx)
print(summary(zyfit))
print("=========================================================")
print(anova(zyfit))
Call: lm(formula = zy ~ zx) Residuals: 1 2 3 4 5 6 4,50494041 -14,76443924 -4,12361210 11,11312010 -0,09503922 10,75802180 7 -7,39299175 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 15,90735459 31,64252307 0,50272 0,63651586 zx 0,85306102 0,09894426 8,62163 0,00034651 *** --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1 Residual standard error: 10,48035 on 5 degrees of freedom Multiple R-squared: 0,9369742, Adjusted R-squared: 0,924369 F-statistic: 74,33254 on 1 and 5 DF, p-value: 0,0003465053 [1] "=========================================================" Analysis of Variance Table Response: zy Df Sum Sq Mean Sq F value Pr(>F) zx 1 8164,5251 8164,5251 74,33254 0,00034651 *** Residuals 5 549,1892 109,8378 --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1
In [4]:
#===================================================================
# Dispersão entre renda e consumo.
#===================================================================
par(mfrow=c(1,1),lwd=2.0,cex.lab=1.5,cex.axis=1.5,lab=c(11,10,5),
mar=c(5,5,1,2.5),xpd=T,cex.main=2.0,bty="n")
plot(x,y,pch=15,xlab="Renda (X)",ylab="Consumo (Y)",
xlim=c(230,380),ylim=c(210,340))
points(x[3:6],y[3:6],pch=15,col="blue")
lines(x,yfit$fitted.values)
lines(zx,zyfit$fitted.values,col="red")
legend(230,335,legend="Anos da Guerra",pch=15,col="blue",bty="n")
legend(230,330,legend="Ajuste 'Com Guerra'",lty=1,col="black",bty="n")
legend(230,325,legend="Ajuste 'Sem Guerra'",lty=1,col=2,bty="n")
In [5]:
#===================================================================
# O modelo:
# Consumo(y) = beta0 + beta1*Renda(x) + beta2*W + epsilon.
# Análise incluindo a variável dicotômica dos anos de guerra (W).
# Lembre-se que pelos resultados o R^2 deve ser igual ou maior que
# do primeiro ajuste.
#===================================================================
x <- dataxy[,2]
y <- dataxy[,3]
w <- dataxy[,4]
#===================================================================
# Primeira forma.
#===================================================================
ywfit <- lm(y ~ x + w)
print(summary(ywfit))
print("=========================================================")
print(anova(ywfit))
Call: lm(formula = y ~ x + w) Residuals: Min 1Q Median 3Q Max -14,598514 -4,418046 -2,351711 7,242487 11,101041 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 14,49540274 27,29947958 0,53098 0,60985 x 0,85751111 0,08534054 10,04811 8,1893e-06 *** w -50,68973653 5,93237334 -8,54460 2,7101e-05 *** --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1 Residual standard error: 9,195489 on 8 degrees of freedom Multiple R-squared: 0,9463904, Adjusted R-squared: 0,932988 F-statistic: 70,61345 on 2 and 8 DF, p-value: 8,259837e-06 [1] "=========================================================" Analysis of Variance Table Response: y Df Sum Sq Mean Sq F value Pr(>F) x 1 5768,2068 5768,2068 68,21677 3,4683e-05 *** w 1 6173,5189 6173,5189 73,01013 2,7101e-05 *** Residuals 8 676,4562 84,5570 --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1
In [6]:
#===================================================================
# Segunda forma.
#===================================================================
xw <- cbind(x,w)
ywfit2 <- lm(y ~ xw)
print(summary(ywfit2))
print("=========================================================")
print(anova(ywfit2))
Call: lm(formula = y ~ xw) Residuals: Min 1Q Median 3Q Max -14,598514 -4,418046 -2,351711 7,242487 11,101041 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 14,49540274 27,29947958 0,53098 0,60985 xwx 0,85751111 0,08534054 10,04811 8,1893e-06 *** xww -50,68973653 5,93237334 -8,54460 2,7101e-05 *** --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1 Residual standard error: 9,195489 on 8 degrees of freedom Multiple R-squared: 0,9463904, Adjusted R-squared: 0,932988 F-statistic: 70,61345 on 2 and 8 DF, p-value: 8,259837e-06 [1] "=========================================================" Analysis of Variance Table Response: y Df Sum Sq Mean Sq F value Pr(>F) xw 2 11941,7257 5970,8628 70,61345 8,2598e-06 *** Residuals 8 676,4562 84,5570 --- Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1
In [7]:
#===================================================================
# Fim
#===================================================================