# rm(list=ls())
options(OutDec = ",")
#==============================================================================
# Exemplo:
# Variancia do estimador do coeficiente angular do modelo de regressao:
# y = beta0 + beta1 x + epsilon, para tamanhos de amostra 1000
#==============================================================================
set.seed(12345)
n <- 1000 # Tamanho de cada amostra
alpha <- 0.5 # Intercepto
beta <- 0.5 # Coeficiente angular
sigma <- 0.5 # Desvio padrao do erro
epsilon <- rnorm(n,0,sigma)
# primeira regressao, variancia de x "pequena"
x1 <- rnorm(n,0,1)
y1 <- alpha + beta*x1 + epsilon
# segunda regressao, variancia de x "grande"
x2 <- rnorm(n,0,2)
y2 <- alpha + beta*x2 + epsilon
xseq <- seq(-6.5,6.5,length=500)
yseq <- alpha + beta*xseq
#==============================================================================
# Grafico de dispersao (menor variância em x)
#==============================================================================
par(mfrow=c(1,1),lwd=2.0,cex.lab=1.5,cex.axis=1.5,lab=c(8,5,5),
mar=c(5,5,2,2.5),xpd=T,cex.main=2.0)
plot(x1,y1,pch="*",xlab=expression(x),ylab=expression(y),
xlim=c(-6.5,6.5),ylim=c(-3.5,3.5))
lines(xseq,yseq,col="red",lwd=2)
text(-5,3,"n = 1000")
#==============================================================================
# Grafico de dispersao (menor variância em x)
#==============================================================================
par(mfrow=c(1,1),lwd=2.0,cex.lab=1.5,cex.axis=1.5,lab=c(8,5,5),
mar=c(5,5,2,2.5),xpd=T,cex.main=2.0)
plot(x2,y2,pch="*",xlab=expression(x),ylab=expression(y),
xlim=c(-6.5,6.5),ylim=c(-3.5,3.5))
lines(xseq,yseq,col="red",lwd=2)
text(-5,3,"n = 1000")
#==============================================================================
# Ajustes de minimos quadrados e comparacoes
#==============================================================================
yfit1 <- lm(y1 ~ x1)
yfit2 <- lm(y2 ~ x2)
print("==== Resumo do Ajuste 1 ====")
print(summary(yfit1))
print("==== Resumo do Ajuste 2 ====")
print(summary(yfit2))
print("==== ==== ==== ==== ==== ==== ====")
print("==== ==== ==== ==== ==== ==== ====")
print("==== ANOVA Ajuste 1 ====")
print(anova(yfit1))
print("==== ANOVA Ajuste 2 ====")
print(anova(yfit2))
[1] "==== Resumo do Ajuste 1 ===="
Call:
lm(formula = y1 ~ x1)
Residuals:
Min 1Q Median 3Q Max
-1,39951 -0,31974 0,00172 0,32375 1,66565
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0,52368 0,01579 33,16 <2e-16 ***
x1 0,51920 0,01566 33,15 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1
Residual standard error: 0,4992 on 998 degrees of freedom
Multiple R-squared: 0,524, Adjusted R-squared: 0,5235
F-statistic: 1099 on 1 and 998 DF, p-value: < 2,2e-16
[1] "==== Resumo do Ajuste 2 ===="
Call:
lm(formula = y2 ~ x2)
Residuals:
Min 1Q Median 3Q Max
-1,41489 -0,32362 0,00039 0,32512 1,63736
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0,52294 0,01580 33,09 <2e-16 ***
x2 0,49683 0,00826 60,15 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1
Residual standard error: 0,4996 on 998 degrees of freedom
Multiple R-squared: 0,7838, Adjusted R-squared: 0,7836
F-statistic: 3618 on 1 and 998 DF, p-value: < 2,2e-16
[1] "==== ==== ==== ==== ==== ==== ===="
[1] "==== ==== ==== ==== ==== ==== ===="
[1] "==== ANOVA Ajuste 1 ===="
Analysis of Variance Table
Response: y1
Df Sum Sq Mean Sq F value Pr(>F)
x1 1 273,82 273,820 1098,6 < 2,2e-16 ***
Residuals 998 248,75 0,249
---
Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1
[1] "==== ANOVA Ajuste 2 ===="
Analysis of Variance Table
Response: y2
Df Sum Sq Mean Sq F value Pr(>F)
x2 1 902,90 902,90 3617,6 < 2,2e-16 ***
Residuals 998 249,09 0,25
---
Signif. codes: 0 ‘***’ 0,001 ‘**’ 0,01 ‘*’ 0,05 ‘.’ 0,1 ‘ ’ 1