Read in data and get the variables you want
cd = read.csv("http://www.rob-mcculloch.org/data/susedcars.csv")
cd = cd[,c("price","mileage","year")]
cd$price = cd$price/1000
cd$mileage = cd$mileage/1000
head(cd)
## price mileage year
## 1 43.995 36.858 2008
## 2 44.995 46.883 2012
## 3 25.999 108.759 2007
## 4 33.880 35.187 2007
## 5 34.895 48.153 2007
## 6 5.995 121.748 2002
plot
plot(cd$mileage,cd$price,xlab='mileage',ylab='price',col='blue',cex=.8,pch=16)
Regress price on mileage
lmmod1 = lm(price~mileage,cd)
print(lmmod1$coefficients)
## (Intercept) mileage
## 56.3597845 -0.3499745
Regress price on mileage and year
lmmod = lm(price~mileage + year,cd)
print(lmmod$coefficients)
## (Intercept) mileage year
## -5365.4898723 -0.1537219 2.6943495
Standard Regression Output
summary(lmmod)
##
## Call:
## lm(formula = price ~ mileage + year, data = cd)
##
## Residuals:
## Min 1Q Median 3Q Max
## -21.857 -4.855 -1.670 3.483 34.499
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.365e+03 1.716e+02 -31.27 <2e-16 ***
## mileage -1.537e-01 8.339e-03 -18.43 <2e-16 ***
## year 2.694e+00 8.526e-02 31.60 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.544 on 997 degrees of freedom
## Multiple R-squared: 0.8325, Adjusted R-squared: 0.8321
## F-statistic: 2477 on 2 and 997 DF, p-value: < 2.2e-16