This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Load package "datasets":
library(datasets)
#Load data "mtcars":
data(mtcars)
data<-mtcars
library(ggplot2); 
# quick check the relation between mpg and transmission(am=0 for automatic transmission and am=1 for manual transmission)
qplot(mtcars$am,mtcars$mpg,data=mtcars) 

plot of chunk unnamed-chunk-2

# From plot above, mpg has a correlation on transmission. Need to subset from data on transmittion. 
# subset mtcars by transmission (0 = automatic, 1 = manual)
mtcarsauto=subset(mtcars,am==0,select=c(mpg,am,cyl))
mtcarsmanual=subset(mtcars,am==1,select=c(mpg,am,cyl))
qplot(mtcarsauto$cyl, mtcarsauto$mpg, data=mtcarsauto, color=am, geom=c("point","smooth"), method="lm", main="auto")

plot of chunk unnamed-chunk-4

qplot(mtcarsmanual$cyl, mtcarsmanual$mpg, data=mtcarsmanual, color=am, geom=c("point","smooth"), method="lm", main="manual")

plot of chunk unnamed-chunk-4

# Since the the fitting of linear regress of mpg on manual transmission is better that that on automatic transmission, let's look at the summary of individual fitting results
summary(lm(mpg~cyl,data=mtcarsauto))
## 
## Call:
## lm(formula = mpg ~ cyl, data = mtcarsauto)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.668 -1.069  0.132  1.381  4.132 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   30.874      2.590   11.92  1.1e-09 ***
## cyl           -1.976      0.364   -5.42  4.6e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.39 on 17 degrees of freedom
## Multiple R-squared:  0.634,  Adjusted R-squared:  0.612 
## F-statistic: 29.4 on 1 and 17 DF,  p-value: 4.58e-05
summary(lm(mpg~cyl,data=mtcarsmanual))
## 
## Call:
## lm(formula = mpg ~ cyl, data = mtcarsmanual)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -6.526 -1.664 -0.364  2.474  5.974 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   41.049      3.572   11.49  1.8e-07 ***
## cyl           -3.281      0.675   -4.86    5e-04 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.63 on 11 degrees of freedom
## Multiple R-squared:  0.682,  Adjusted R-squared:  0.653 
## F-statistic: 23.6 on 1 and 11 DF,  p-value: 0.000503
# The difference of mpg bwteen manual transmission and automatic transmission decreases as cyl number increases. At 4 and 6 cyl, th difference of mpg between manual transimission and automatic transmission is still distinguishable. At 8 cyl, the  mpg difference is indistinguishable.
# The quantity differnece between manual transmission and automatic transimission as function of cyl number are calculated below.

# mpg from manual transmission
mpgmanual=41.049+c(4,6,8)*-3.281

# mpg from automatic transmission
mpgauto=30.874+c(4,6,8)*-1.976

# the difference of mpg in the order of 4, 6 and 8 cyl
str(mpgmanual-mpgauto)
##  num [1:3] 4.955 2.345 -0.265

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.