

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


The dataset we are using has several numeric variables and several categorical variables.
A data frame with 32 observations on 11 variables.
[, 1] mpg Miles/(US) gallon [, 2] cyl Number of cylinders [, 3] disp Displacement (cu.in.) [, 4] hp Gross horsepower [, 5] drat Rear axle ratio [, 6] wt Weight (1000 lbs) [, 7] qsec 1/4 mile time [, 8] vs V/S [, 9] am Transmission (0 = automatic, 1 = manual) [,10] gear Number of forward gears [,11] carb Number of carburetors
We will look at mpg by cyl
data (mtcars) head (mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mpg4cy= subset (mtcars,cyl==4,select=mpg:cyl) mpg6cy= subset (mtcars,cyl==6,select=mpg:cyl) mpg8cy= subset (mtcars,cyl==8,select=mpg:cyl) head (mpg4cy); head (mpg6cy); head (mpg8cy)
mpg cyl Datsun 710 22.8 4 Merc 240D 24.4 4 Merc 230 22.8 4 Fiat 128 32.4 4 Honda Civic 30.4 4 Toyota Corolla 33.9 4
mpg cyl Mazda RX4 21.0 6 Mazda RX4 Wag 21.0 6 Hornet 4 Drive 21.4 6 Valiant 18.1 6 Merc 280 19.2 6 Merc 280C 17.8 6
mpg cyl Hornet Sportabout 18.7 8 Duster 360 14.3 8 Merc 450SE 16.4 8 Merc 450SL 17.3 8 Merc 450SLC 15.2 8 Cadillac Fleetwood 10.4 8
The stargazer() command can sometimes be a pain. It does not like single variables in the syntax; rather it wants a dataset. By keeping the two variables of interest (mpg and cyl), stargazer() will work with each new “dataset” and not output a jumble of useless characters.
library (stargazer) stargazer (mpg4cy,type='text')
mpg 11 26.664 4.510 21.400 33. cyl 11 4.000 0.000 4 4
stargazer (mpg6cy,type='text')
mpg 7 19.743 1.454 17.800 21. cyl 7 6.000 0.000 6 6
stargazer (mpg8cy,type='text')
mpg 14 15.100 2.560 10.400 19. cyl 14 8.000 0.000 8 8
Rather than subset data, you can use describeBy() in the psych package. Install the psych package first, then load it. The describeBy() command allows the use of a grouping (categorical) variable. Using attach() may make coding less tedious.
# install.packages("psych") library (psych) describeBy (mtcars$mpg,mtcars$cyl)
Descriptive statistics by group group: 4 vars n mean sd median trimmed mad min max range skew kurtosis X1 1 11 26.66 4.51 26 26.44 6.52 21.4 33.9 12.5 0.26 -1. se