O arquivo wage1.RData contém dados de 526 indivíduos para o ano de 1976. As variáveis incluem salário e escolaridade.
load(file.path(dir.dados, "wage1.RData"))
Inspecionando o conjunto de dados:
desc.wage1
## variable label
## 1 wage average hourly earnings
## 2 educ years of education
## 3 exper years potential experience
## 4 tenure years with current employer
## 5 nonwhite =1 if nonwhite
## 6 female =1 if female
## 7 married =1 if married
## 8 numdep number of dependents
## 9 smsa =1 if live in SMSA
## 10 northcen =1 if live in north central U.S
## 11 south =1 if live in southern region
## 12 west =1 if live in western region
## 13 construc =1 if work in construc. indus.
## 14 ndurman =1 if in nondur. manuf. indus.
## 15 trcommpu =1 if in trans, commun, pub ut
## 16 trade =1 if in wholesale or retail
## 17 services =1 if in services indus.
## 18 profserv =1 if in prof. serv. indus.
## 19 profocc =1 if in profess. occupation
## 20 clerocc =1 if in clerical occupation
## 21 servocc =1 if in service occupation
## 22 lwage log(wage)
## 23 expersq exper^2
## 24 tenursq tenure^2
str(dados.wage1)
## 'data.frame': 526 obs. of 24 variables:
## $ wage : num 3.1 3.24 3 6 5.3 ...
## $ educ : int 11 12 11 8 12 16 18 12 12 17 ...
## $ exper : int 2 22 2 44 7 9 15 5 26 22 ...
## $ tenure : int 0 2 0 28 2 8 7 3 4 21 ...
## $ nonwhite: int 0 0 0 0 0 0 0 0 0 0 ...
## $ female : int 1 1 0 0 0 0 0 1 1 0 ...
## $ married : int 0 1 0 1 1 1 0 0 0 1 ...
## $ numdep : int 2 3 2 0 1 0 0 0 2 0 ...
## $ smsa : int 1 1 0 1 0 1 1 1 1 1 ...
## $ northcen: int 0 0 0 0 0 0 0 0 0 0 ...
## $ south : int 0 0 0 0 0 0 0 0 0 0 ...
## $ west : int 1 1 1 1 1 1 1 1 1 1 ...
## $ construc: int 0 0 0 0 0 0 0 0 0 0 ...
## $ ndurman : int 0 0 0 0 0 0 0 0 0 0 ...
## $ trcommpu: int 0 0 0 0 0 0 0 0 0 0 ...
## $ trade : int 0 0 1 0 0 0 1 0 1 0 ...
## $ services: int 0 1 0 0 0 0 0 0 0 0 ...
## $ profserv: int 0 0 0 0 0 1 0 0 0 0 ...
## $ profocc : int 0 0 0 0 0 1 1 1 1 1 ...
## $ clerocc : int 0 0 0 1 0 0 0 0 0 0 ...
## $ servocc : int 0 1 0 0 0 0 0 0 0 0 ...
## $ lwage : num 1.13 1.18 1.1 1.79 1.67 ...
## $ expersq : int 4 484 4 1936 49 81 225 25 676 484 ...
## $ tenursq : int 0 4 0 784 4 64 49 9 16 441 ...
## - attr(*, "datalabel")= chr ""
## - attr(*, "time.stamp")= chr "25 Jun 2011 23:03"
## - attr(*, "formats")= chr "%8.2g" "%8.0g" "%8.0g" "%8.0g" ...
## - attr(*, "types")= int 254 251 251 251 251 251 251 251 251 251 ...
## - attr(*, "val.labels")= chr "" "" "" "" ...
## - attr(*, "var.labels")= chr "average hourly earnings" "years of education" "years potential experience" "years with current employer" ...
## - attr(*, "version")= int 10
Obs. Visualize melhor os data frames no RStudio com:
View(desc.wage1)
View(dados.wage1)
Queremos estimar o modelo:
\[wage = \beta_0 + \beta_1 educ + u\]
modelo.1 <- lm(wage ~educ, data = dados.wage1)
summary(modelo.1)
##
## Call:
## lm(formula = wage ~ educ, data = dados.wage1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3396 -2.1501 -0.9674 1.1921 16.6085
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.90485 0.68497 -1.321 0.187
## educ 0.54136 0.05325 10.167 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.378 on 524 degrees of freedom
## Multiple R-squared: 0.1648, Adjusted R-squared: 0.1632
## F-statistic: 103.4 on 1 and 524 DF, p-value: < 2.2e-16
O modelo estimado portanto é:
\[\hat{wage} = -0.90485 + 0.54136 educ\]
Observe que os coeficientes podem ser acessados diretamente através de:
modelo.1$coefficients
## (Intercept) educ
## -0.9048516 0.5413593
A função fitted mostra os valores ajustados para os \(y_i\):
y.chapeu <- fitted(modelo.1)
head(y.chapeu)
## 1 2 3 4 5 6
## 5.050100 5.591459 5.050100 3.426022 5.591459 7.756896
View(cbind(dados.wage1$wage, y.chapeu))
Outra maneira é diretamente através de:
head(modelo.1$fitted.values)
## 1 2 3 4 5 6
## 5.050100 5.591459 5.050100 3.426022 5.591459 7.756896
u.chapeu <- residuals(modelo.1)
head(u.chapeu)
## 1 2 3 4 5 6
## -1.9501003 -2.3514594 -2.0501002 2.5739776 -0.2914593 0.9931035
View(cbind(dados.wage1$wage, y.chapeu, u.chapeu))
var(dados.wage1$wage)
## [1] 13.63888
Essas quantidades são geradas pelo ajuste do modelo, e podem ser acessadas através da tabela de Análise de Variância:
anova(modelo.1)
## Analysis of Variance Table
##
## Response: wage
## Df Sum Sq Mean Sq F value Pr(>F)
## educ 1 1179.7 1179.73 103.36 < 2.2e-16 ***
## Residuals 524 5980.7 11.41
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Os valores são reportados em Sum Sq (Sum of Squares):
Como
\[\text{SQT} = \text{SQE} + \text{SQR}\] Devemos ter \[\text{SQT} = 1179.7 + 5980.7 = 7160.4\]
O \(R^2\) é reportado pelo nome Multiple R-squared.
\[R^2 = 0.1648\] Que também pode ser obtido através de:
\[R^2 = \frac{\text{SQE}}{\text{SQT}} = \frac{1179.7}{7160.4} = 0.1648\]
Carregando os dados:
load(file.path(dir.dados, "wage2.RData"))
O modelo procurado é:
\[ wage = \beta_0 + \beta_1 IQ + u\]
modelo.2 <- lm(wage ~ IQ, data = dados.wage2)
summary(modelo.2)
##
## Call:
## lm(formula = wage ~ IQ, data = dados.wage2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -898.7 -256.5 -47.3 201.1 2072.6
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 116.9916 85.6415 1.366 0.172
## IQ 8.3031 0.8364 9.927 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 384.8 on 933 degrees of freedom
## Multiple R-squared: 0.09554, Adjusted R-squared: 0.09457
## F-statistic: 98.55 on 1 and 933 DF, p-value: < 2.2e-16
O coeficiente associado a \(IQ\) é \(\hat{\beta_1} = 8.3031\). Logo, para cada unidade adicional de \(IQ\) o aumento previsto no salário é de 8,3031. A variação esperada em \(wage\) em função de \(IQ\) é dada por:
\[\Delta \hat{y} = \hat{\beta_1} \Delta x\] Observando que nesse caso \(\Delta x = 15\), resulta:
\[\Delta \hat{y} = 8,3031 \times 15 = 124,5465\]
Nesse caso, devemos ajustar o seguinte modelo:
\[ \log(wage) = \beta_0 + \beta_1 IQ + u\]
modelo.3 <- lm(lwage ~ IQ, data = dados.wage2)
summary(modelo.3)
##
## Call:
## lm(formula = lwage ~ IQ, data = dados.wage2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.09324 -0.25547 0.02261 0.27544 1.21487
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.8869944 0.0890206 66.13 <2e-16 ***
## IQ 0.0088072 0.0008694 10.13 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3999 on 933 degrees of freedom
## Multiple R-squared: 0.09909, Adjusted R-squared: 0.09813
## F-statistic: 102.6 on 1 and 933 DF, p-value: < 2.2e-16
Nesse caso, a interpretação do coeficiente de inclinação pode ser estabelecida pela seguinte relação:
\[\%\Delta y = (100 \cdot \hat{\beta}_1) \Delta x\] Um aumento de \(IQ\) em 15 pontos corresponde ao seguinte aumento percentual em \(y\):
\[\%\Delta y = (100 \cdot 0,0088) \times 15 = 13,2\]
load(file.path(dir.dados, "vote1.RData"))
\[ voteA = \beta_0 + \beta_1 shareA + u\]
modelo.4 <- lm(voteA ~ shareA, data = dados.vote1)
summary(modelo.4)
##
## Call:
## lm(formula = voteA ~ shareA, data = dados.vote1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.8919 -4.0660 -0.1682 3.4965 29.9772
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 26.81221 0.88721 30.22 <2e-16 ***
## shareA 0.46383 0.01454 31.90 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.385 on 171 degrees of freedom
## Multiple R-squared: 0.8561, Adjusted R-squared: 0.8553
## F-statistic: 1018 on 1 and 171 DF, p-value: < 2.2e-16
A saída do modelo registra: “Multiple R-squared: 0.8561”, o que indica que 85,61% da variabilidade da votação percentual de um candidato é explicada pela sua participação nos gastos de campanha.