WHAT IS R?
R is a free matrix programming language and software environment
that is widely used among statisticians for developing statistical
software and data analysis. It is based on an earlier language S
that became the commercial product S-Plus. It runs on Windows, Mac
and Linux. Unlike S-Plus, R is free. See http://en.wikipedia.org/wiki/R_(programming_language)
For regression analysis one needs
It is enough initially to just have R. But it is also convenient
to use the front-end R-studio and you are likely to also need to
add some user-written packages.
The repository for R packages is CRAN (The Comprehensive R Archive Network).
> y =
c(1,2,2,2,3)
> x = c(1,2,3,4,5)
> y
[1] 1 2 2 2 3
> x
[1] 1 2 3 4 5
> mean(y)
[1] 2
> summary(y)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1
2 2
2 2 3
> plot(y,x)
.... output omitted ...
> lm(y~x)
Call:
lm(formula = y ~x)
Coefficients:
(Intercept)
x
0.8 0.4
> lm.cars <-
lm(y~x)
> summary(lm.cars)
Call:
lm(formula = y ~x)
Residuals:
1
2
3
4 5
-2.000e-01 4.000e-01 -6.855e-17 -4.000e-01 2.000e-01
Coefficients:
Estimate
Std. Error t value Pr(>|t|)
(Intercept) 0.8000
0.3830
2.089 0.1279
x
0.4000
0.1155 3.464 0.0405 *
---
Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 `
' 1
Residual standard error: 0.3651 on 3 degrees of freedom
Multiple R-squared:
0.8, Adjusted
R-squared: 0.7333
F-statistic: 12 on 1 and 3 DF, p-value:
0.04052
>
library(sandwich)
Error in library(sandwich): there is no package called
`sandwich'
> install.packages("sandwich")
.... output omitted
> library(sandwich)
> model <- lm(y ~x)
> vcovHC(model)
(Intercept) x
(Intercept) 0.28489796 -0.07959184
x
-0.07959184 0.02653061
>
sqrt(diag(vcovHC(model)))
(Intercept) x
0.5337583 0.1628822
>
install.packages("xtable")
> library(xtable)
> xtable(model)
> x
[1] 1 2 3 4 5
> X <- cbind(1,x)
> X
x
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
[5,] 1 5
> bhat <- solve(t(X)%*%X)%*%t(X)%*%y
> bhat
[,1]
0.8
x 0.4
mydata
<-
read.csv("http://cameron.econ.ucdavis.edu/excel/carsdata.csv")
>
summary(mydata)
CARS
HH.SIZE
Min.
:1 Min. :1
1st Qu.:2
1st Qu.:2
Median :2
Median :3
Mean
:2 Mean :3
3rd Qu.:2
3rd Qu.:4
Max.
:3 Max.
:5
>