Hi! I am Mike Marin and in this video we'll talk about producing numeric summaries
for both categorical and numerical variables using R.
It is often of interest to quantify the center
as well as the spread of the distribution of a variable.
we'll be working with the Lung Capacity Data
that was introduced earlier in this series of videos. I've already gone ahead
and imported the data into R and attached it. we will summarize the
categorical variable
Smoke and the numeric variable Lung Capacity
to access the Help menu
for any of these commands simply type "help"
and in brackets the name of the command or throw a question mark (?)
in front of the command name. We will begin
with a categorical variable Smoke; Categorical variables
are summarized using a frequency or a proportion. We can use the "table" command
to produce a frequency table
for a categorical variable. we can see
that there are 725 observations in our sample.
To express the table using a proportion
we can divide this table by 725.
Earlier in this series of videos
we also learned about the "length" command: this can be used to let us know
how many observations we have for a particular variable.
So, we may also produce a table of proportions
by dividing our table by the length of the variable Smoke
which is 725. doing this can often be a good practice
as it guards against typos and/or
changes that may be made to the dataset such as removal of certain observations.
To produce a "2 way table"
or a "contingency table", we may simply enter both variables
into the "table" command. Here we can see the
"2 way table" or "contingency table" produced
for the variables Smoke and Gender.
Now, let's move into discussing a numeric variable such as Lung Capacity
we can calculate the arithmetic "mean"
using the "mean" command. as well
if we would like to produce a trimmed mean we can achieve this
by using the "trim" argument. here
we will remove the top 10 percent and bottom 10 percent of observations
to produce the trimmed mean. we can calculate the "median"
using the "median" command.
To calculate the variance of a
variable we can use the "var" command.
to calculate the standard deviation
we can use the "sd" command
Or, we can also calculate the standard deviation by taking the square root
of the variance.
Conversely we can find the variance by taking the standard deviation
and squaring this. we can calculate the minimum
observation using the "min" command. We can find the maximum
using the "max" command. as well
we can calculate the range for the variable using the "range" command.
Specific quantiles
or percentiles may be calculated using the
"quantile" command. Here we'll calculate
quantile for Lung Capacity. the "probs" argument
is where we let R know which specific percentile
or quantiles we would like calculated. here we can calculate the
90th percentile; for the "probs"" argument
we may also specify multiple values
to obtain multiple percentiles
let's go ahead and ask for the 20th percentile,
the 50th percentile which is also the median,
the 90th percentile, as well as the 100th percentile
or maximum value.
While often of less interest, one may also
sum all of the observed values for a variable using the
"sum" command.
while unnecessary to do so, we can also calculate the mean
using the "sum" command.
one may calculate "Pearson's correlation" using the "cor" command.
Here we can calculate the correlation between Lung Capacity
and Age. It is worth noting that
"Pearson's correlation" is the default for the "cor" command.
Instead if you would like to calculate "Spearman's correlation"
this can be achieved by using the "method" argument
and setting this equal to "spearman".
While often of less interest, the covariance
may be calculated using the "cov" command
Here we can calculate the covariance between LungCapacity and Age
this may also be calculated using the variance ("var") command
it should be mentioned here that the "summary" command
may be used to produce
most of the summeries. we can see when asking for summaries of the variable
LungCap, R returns to us the minimum
first quartile, median, mean
third quartile, and maximum.
The "summary" command can also be used for categorical variables.
here we are returned the "frequency table".
The "summary" command is a generic function that will produce
appropriate summaries for all sorts of objects in R.
we may even ask R for a summary of the entire dataset
Here we can see R returns to us appropriate numerical summaries
for all of the variables contained within the object LungCapData
we will see the use of the "summary" command come up repeatedly in later videos.
thanks for watching this video and make sure to check out my other instructional videos.