R Markdown

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:

setwd('C:/Users/praisons/Documents/CBA/Term3/SA2')

cigdata <- read.csv('CigaretteConsumption.csv', header = TRUE, sep=',')

fit <- lm(formula = Sales~State+Age+HS+Income+Black+Female+Price, data = cigdata)

names(cigdata)
## [1] "State"  "Age"    "HS"     "Income" "Black"  "Female" "Price"  "Sales"
summary(fit)
## 
## Call:
## lm(formula = Sales ~ State + Age + HS + Income + Black + Female + 
##     Price, data = cigdata)
## 
## Residuals:
## ALL 51 residuals are 0: no residual degrees of freedom!
## 
## Coefficients: (6 not defined because of singularities)
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept)  1.213e+02         NA      NA       NA
## StateAL     -3.150e+01         NA      NA       NA
## StateAR     -2.100e+01         NA      NA       NA
## StateAZ     -6.100e+00         NA      NA       NA
## StateCA      1.700e+00         NA      NA       NA
## StateCO      3.500e+00         NA      NA       NA
## StateCT     -1.300e+00         NA      NA       NA
## StateDC      7.910e+01         NA      NA       NA
## StateDE      3.370e+01         NA      NA       NA
## StateFL      2.300e+00         NA      NA       NA
## StateGA     -1.140e+01         NA      NA       NA
## StateHI     -3.920e+01         NA      NA       NA
## StateID     -1.890e+01         NA      NA       NA
## StateIL      3.500e+00         NA      NA       NA
## StateIN      1.330e+01         NA      NA       NA
## StateIO     -1.280e+01         NA      NA       NA
## StateKA     -7.300e+00         NA      NA       NA
## StateKY      3.450e+01         NA      NA       NA
## StateLA     -5.400e+00         NA      NA       NA
## StateMA      3.000e+00         NA      NA       NA
## StateMD      2.200e+00         NA      NA       NA
## StateME      7.200e+00         NA      NA       NA
## StateMI      7.300e+00         NA      NA       NA
## StateMN     -1.700e+01         NA      NA       NA
## StateMO      9.723e-14         NA      NA       NA
## StateMS     -2.790e+01         NA      NA       NA
## StateMT     -1.010e+01         NA      NA       NA
## StateNB     -1.320e+01         NA      NA       NA
## StateNC      5.110e+01         NA      NA       NA
## StateND     -2.750e+01         NA      NA       NA
## StateNH      1.444e+02         NA      NA       NA
## StateNJ     -6.000e-01         NA      NA       NA
## StateNM     -3.130e+01         NA      NA       NA
## StateNV      6.820e+01         NA      NA       NA
## StateNY     -2.300e+00         NA      NA       NA
## StateOH      3.000e-01         NA      NA       NA
## StateOK     -1.290e+01         NA      NA       NA
## StateOR      3.570e+01         NA      NA       NA
## StatePA     -1.400e+01         NA      NA       NA
## StateRI      2.600e+00         NA      NA       NA
## StateSC     -1.770e+01         NA      NA       NA
## StateSD     -2.860e+01         NA      NA       NA
## StateTN     -2.150e+01         NA      NA       NA
## StateTX     -1.490e+01         NA      NA       NA
## StateUT     -5.580e+01         NA      NA       NA
## StateVA      3.000e+00         NA      NA       NA
## StateVT      1.300e+00         NA      NA       NA
## StateWA     -2.460e+01         NA      NA       NA
## StateWI     -1.490e+01         NA      NA       NA
## StateWV     -6.800e+00         NA      NA       NA
## StateWY      1.090e+01         NA      NA       NA
## Age                 NA         NA      NA       NA
## HS                  NA         NA      NA       NA
## Income              NA         NA      NA       NA
## Black               NA         NA      NA       NA
## Female              NA         NA      NA       NA
## Price               NA         NA      NA       NA
## 
## Residual standard error: NaN on 0 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:    NaN 
## F-statistic:   NaN on 50 and 0 DF,  p-value: NA
coefficients(fit)
##   (Intercept)       StateAL       StateAR       StateAZ       StateCA 
##  1.213000e+02 -3.150000e+01 -2.100000e+01 -6.100000e+00  1.700000e+00 
##       StateCO       StateCT       StateDC       StateDE       StateFL 
##  3.500000e+00 -1.300000e+00  7.910000e+01  3.370000e+01  2.300000e+00 
##       StateGA       StateHI       StateID       StateIL       StateIN 
## -1.140000e+01 -3.920000e+01 -1.890000e+01  3.500000e+00  1.330000e+01 
##       StateIO       StateKA       StateKY       StateLA       StateMA 
## -1.280000e+01 -7.300000e+00  3.450000e+01 -5.400000e+00  3.000000e+00 
##       StateMD       StateME       StateMI       StateMN       StateMO 
##  2.200000e+00  7.200000e+00  7.300000e+00 -1.700000e+01  9.723124e-14 
##       StateMS       StateMT       StateNB       StateNC       StateND 
## -2.790000e+01 -1.010000e+01 -1.320000e+01  5.110000e+01 -2.750000e+01 
##       StateNH       StateNJ       StateNM       StateNV       StateNY 
##  1.444000e+02 -6.000000e-01 -3.130000e+01  6.820000e+01 -2.300000e+00 
##       StateOH       StateOK       StateOR       StatePA       StateRI 
##  3.000000e-01 -1.290000e+01  3.570000e+01 -1.400000e+01  2.600000e+00 
##       StateSC       StateSD       StateTN       StateTX       StateUT 
## -1.770000e+01 -2.860000e+01 -2.150000e+01 -1.490000e+01 -5.580000e+01 
##       StateVA       StateVT       StateWA       StateWI       StateWV 
##  3.000000e+00  1.300000e+00 -2.460000e+01 -1.490000e+01 -6.800000e+00 
##       StateWY           Age            HS        Income         Black 
##  1.090000e+01            NA            NA            NA            NA 
##        Female         Price 
##            NA            NA
# Run regression for cars data

carsdata <- read.csv('Cars.csv', header = TRUE, sep=',')

head(carsdata)
##   HP      MPG VOL       SP       WT
## 1 49 53.70068  89 104.1854 28.76206
## 2 55 50.01340  92 105.4613 30.46683
## 3 55 50.01340  92 105.4613 30.19360
## 4 70 45.69632  92 113.4613 30.63211
## 5 53 50.50423  92 104.4613 29.88915
## 6 70 45.69632  89 113.1854 29.59177
names(carsdata)
## [1] "HP"  "MPG" "VOL" "SP"  "WT"
fit <- lm(formula = MPG~HP+VOL+SP+WT, data = carsdata)

fit
## 
## Call:
## lm(formula = MPG ~ HP + VOL + SP + WT, data = carsdata)
## 
## Coefficients:
## (Intercept)           HP          VOL           SP           WT  
##     30.6773      -0.2054      -0.3361       0.3956       0.4006
summary(fit)
## 
## Call:
## lm(formula = MPG ~ HP + VOL + SP + WT, data = carsdata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.6320 -2.9944 -0.3705  2.2149 15.6179 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 30.67734   14.90030   2.059   0.0429 *  
## HP          -0.20544    0.03922  -5.239  1.4e-06 ***
## VOL         -0.33605    0.56864  -0.591   0.5563    
## SP           0.39563    0.15826   2.500   0.0146 *  
## WT           0.40057    1.69346   0.237   0.8136    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.488 on 76 degrees of freedom
## Multiple R-squared:  0.7705, Adjusted R-squared:  0.7585 
## F-statistic:  63.8 on 4 and 76 DF,  p-value: < 2.2e-16
coefficients(fit)
## (Intercept)          HP         VOL          SP          WT 
##  30.6773359  -0.2054437  -0.3360508   0.3956269   0.4005741
anova(fit)
## Analysis of Variance Table
## 
## Response: MPG
##           Df Sum Sq Mean Sq  F value    Pr(>F)    
## HP         1 3506.6  3506.6 174.1099 < 2.2e-16 ***
## VOL        1 1500.8  1500.8  74.5153 6.763e-13 ***
## SP         1  131.5   131.5   6.5274   0.01262 *  
## WT         1    1.1     1.1   0.0560   0.81365    
## Residuals 76 1530.7    20.1                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
plot(fit)

fitted(fit)
##        1        2        3        4        5        6        7        8 
## 43.44193 42.38879 42.27934 42.53836 42.17265 43.02062 42.32536 48.07622 
##        9       10       11       12       13       14       15       16 
## 48.28120 40.79123 41.52153 47.80957 39.95980 41.52758 41.76632 41.61814 
##       17       18       19       20       21       22       23       24 
## 41.15094 47.98606 41.30861 37.87128 38.57706 37.35200 37.89770 39.56251 
##       25       26       27       28       29       30       31       32 
## 39.93381 46.73871 35.48166 38.78153 38.24861 36.00285 34.84604 37.21630 
##       33       34       35       36       37       38       39       40 
## 37.13920 34.82541 37.22361 37.53950 39.27145 38.24220 38.54286 35.93917 
##       41       42       43       44       45       46       47       48 
## 34.21298 35.36313 37.50473 38.07998 35.79652 36.26134 34.21826 35.59393 
##       49       50       51       52       53       54       55       56 
## 36.91805 33.31108 33.21313 33.30236 29.19865 27.52359 28.32071 28.56723 
##       57       58       59       60       61       62       63       64 
## 35.81584 33.02108 35.37335 32.29910 29.87686 28.76094 25.14188 26.47041 
##       65       66       67       68       69       70       71       72 
## 25.97652 36.35652 26.09759 23.64162 24.39887 20.21195 27.80846 22.44207 
##       73       74       75       76       77       78       79       80 
## 23.07668 18.71731 23.84935 21.07461 21.28210 17.89905 26.13645 12.31661 
##       81 
## 15.55948
coef(fit)
## (Intercept)          HP         VOL          SP          WT 
##  30.6773359  -0.2054437  -0.3360508   0.3956269   0.4005741
residuals(fit)
##          1          2          3          4          5          6 
## 10.2587466  7.6246083  7.7340597  3.1579626  8.3315838  2.6757032 
##          7          8          9         10         11         12 
##  7.6880405 -1.3596642 -1.5646482  1.5078500  3.1313020 -8.4554734 
##         13         14         15         16         17         18 
## -0.6057086  3.1252554  3.9685696  3.0346894  1.6389684 -8.6319611 
##         19         20         21         22         23         24 
##  1.4812984  1.0305547 -0.1660608  5.4764823  0.4129031  0.9122089 
##         25         26         27         28         29         30 
## -1.6232007 -8.3277058  2.9293443 -0.3705217  5.2208220 -0.5986605 
##         31         32         33         34         35         36 
##  4.5851953  2.2149327 -0.8537415  1.4600425  2.3080186  0.4192422 
##         37         38         39         40         41         42 
## -1.3127053 -4.1715306 -4.4721963 -4.9250413  0.9397515 -0.2104056 
##         43         44         45         46         47         48 
## -3.4340655 -2.9272579 -0.1529590 -1.6998387  0.3432343 -0.5416044 
##         49         50         51         52         53         54 
## -5.9039220 -3.6811456 -3.5831982 -3.6724290  0.4312871 -3.0362191 
##         55         56         57         58         59         60 
## -1.4684346 -0.7109743 -4.7022521 -3.3911486 -5.2414271 -3.4388698 
##         61         62         63         64         65         66 
## -2.5225932 -4.1518066 -1.6259672 -2.9544920 -2.3713610  3.6934752 
##         67         68         69         70         71         72 
## -2.9944195 -0.5384461 -1.2957003  1.0617543 -8.1299580  0.7615007 
##         73         74         75         76         77         78 
##  0.1268910  0.3690266 -4.7630064 -2.3117761 15.6179040  1.2988377 
##         79         80         81 
##  7.8635469  7.5171216 -3.4582177

Including Plots

You can also embed plots, for example:

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