Linear Regression using Rcmdr

Prof. Eric A. Suess

2015-05-11

## Loading required package: splines
## Loading required package: RcmdrMisc
## Loading required package: car
## Loading required package: sandwich
## The Commander GUI is launched only in interactive sessions
> summary(Wheat)
      Year          Wheat          Wages      
 Min.   :1565   Min.   :26.0   Min.   : 5.00  
 1st Qu.:1630   1st Qu.:33.0   1st Qu.: 6.14  
 Median :1695   Median :41.0   Median : 7.80  
 Mean   :1695   Mean   :43.3   Mean   :11.58  
 3rd Qu.:1760   3rd Qu.:47.0   3rd Qu.:14.88  
 Max.   :1821   Max.   :99.0   Max.   :30.00  
                               NA's   :3      
> library(abind, pos=16)
> with(Wheat, Hist(Wages, scale="frequency", breaks="Sturges", 
+   col="darkgray"))

plot of chunk unnamed-chunk-4

> with(Wheat, Hist(Wheat, scale="frequency", breaks="Sturges", 
+   col="darkgray"))

plot of chunk unnamed-chunk-5

> with(Wheat, Hist(Year, scale="frequency", breaks="Sturges", col="darkgray"))

plot of chunk unnamed-chunk-6

> densityPlot( ~ Wages, data=Wheat, bw="SJ", adjust=1, kernel="gaussian")

plot of chunk unnamed-chunk-7

> densityPlot( ~ Wheat, data=Wheat, bw="SJ", adjust=1, kernel="gaussian")

plot of chunk unnamed-chunk-8

> Boxplot( ~ Wages, data=Wheat, id.method="y")

plot of chunk unnamed-chunk-9

[1] "48" "49" "50"
> Boxplot( ~ Wheat, data=Wheat, id.method="y")

plot of chunk unnamed-chunk-10

[1] "47" "48" "49" "50" "51"
> cor(Wheat[,c("Wages","Wheat")], use="complete")
       Wages  Wheat
Wages 1.0000 0.5805
Wheat 0.5805 1.0000
> scatterplot(Wages~Wheat, reg.line=lm, smooth=TRUE, spread=TRUE, 
+   id.method='mahal', id.n = 2, boxplots='xy', span=0.5, data=Wheat)

plot of chunk unnamed-chunk-12

49 50 
49 50 
> RegModel.1 <- lm(Wages~Wheat, data=Wheat)
> summary(RegModel.1)

Call:
lm(formula = Wages ~ Wheat, data = Wheat)

Residuals:
   Min     1Q Median     3Q    Max 
-12.30  -4.75  -1.76   5.96  12.38 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -0.4790     2.5868   -0.19     0.85    
Wheat         0.2862     0.0579    4.94  9.9e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 6.04 on 48 degrees of freedom
  (3 observations deleted due to missingness)
Multiple R-squared:  0.337, Adjusted R-squared:  0.323 
F-statistic: 24.4 on 1 and 48 DF,  p-value: 9.92e-06
> summary(RegModel.1)

Call:
lm(formula = Wages ~ Wheat, data = Wheat)

Residuals:
   Min     1Q Median     3Q    Max 
-12.30  -4.75  -1.76   5.96  12.38 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -0.4790     2.5868   -0.19     0.85    
Wheat         0.2862     0.0579    4.94  9.9e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 6.04 on 48 degrees of freedom
  (3 observations deleted due to missingness)
Multiple R-squared:  0.337, Adjusted R-squared:  0.323 
F-statistic: 24.4 on 1 and 48 DF,  p-value: 9.92e-06
> AIC(RegModel.1)
[1] 325.6
> BIC(RegModel.1)
[1] 331.4
> library(MASS, pos=17)
> Confint(RegModel.1, level=0.95)
            Estimate   2.5 % 97.5 %
(Intercept)  -0.4790 -5.6802 4.7222
Wheat         0.2862  0.1697 0.4027
> Anova(RegModel.1, type="II")
Anova Table (Type II tests)

Response: Wages
          Sum Sq Df F value  Pr(>F)    
Wheat        889  1    24.4 9.9e-06 ***
Residuals   1749 48                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
> library(zoo, pos=18)

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
> library(lmtest, pos=18)
> bptest(Wages ~ Wheat, varformula = ~ fitted.values(RegModel.1), 
+   studentize=FALSE, data=Wheat)

    Breusch-Pagan test

data:  Wages ~ Wheat
BP = 1.7, df = 1, p-value = 0.2
> dwtest(Wages ~ Wheat, alternative="greater", data=Wheat)

    Durbin-Watson test

data:  Wages ~ Wheat
DW = 0.25, p-value <2e-16
alternative hypothesis: true autocorrelation is greater than 0
> outlierTest(RegModel.1)

No Studentized residuals with Bonferonni p < 0.05
Largest |rstudent|:
  rstudent unadjusted p-value Bonferonni p
7   -2.188            0.03371           NA
> oldpar <- par(oma=c(0,0,3,0), mfrow=c(2,2))
> plot(RegModel.1)

plot of chunk unnamed-chunk-26plot of chunk unnamed-chunk-26plot of chunk unnamed-chunk-26plot of chunk unnamed-chunk-26

> par(oldpar)
> qqPlot(RegModel.1, simulate=TRUE, id.method="y", id.n=2)

plot of chunk unnamed-chunk-28

 7 46 
 1 50 
> stripchart(Wheat$Wages, method="stack", xlab="Wages")

plot of chunk unnamed-chunk-29

> stripchart(Wheat$Wages, method="jitter", xlab="Wages")

plot of chunk unnamed-chunk-30