Course Outline

segmentGetting Started (Don't Skip This Part)

segmentIntroduction to Statistics: A Modeling Approach

segmentPART I: EXPLORING VARIATION

segmentChapter 1  Welcome to Statistics: A Modeling Approach

segmentChapter 2  Understanding Data

segmentChapter 3  Examining Distributions

segmentChapter 4  Explaining Variation

segmentPART II: MODELING VARIATION

segmentChapter 5  A Simple Model

segmentChapter 6  Quantifying Error

segmentChapter 7  Adding an Explanatory Variable to the Model

7.2 Fitting a Model with an Explanatory Variable

segmentChapter 8  Models with a Quantitative Explanatory Variable

segmentPART III: EVALUATING MODELS

segmentChapter 9  Distributions of Estimates

segmentChapter 10  Confidence Intervals and Their Uses

segmentChapter 11  Model Comparison with the F Ratio

segmentChapter 12  What You Have Learned

segmentResources
list Introduction to Statistics: A Modeling Approach
Fitting a Model With an Explanatory Variable
Now that you have learned how to specify a model with an explanatory variable, let’s learn how to fit the model using R.
Fitting a model, as a reminder, simply means calculating the parameter estimates. We use the word “fitting” because we want to calculate the best estimate, the one that will result in the least amount of error. For the tiny data set, we could calculate the parameter estimates in our head—it’s just a matter of calculating the mean for males and the mean for females. But when the data set is larger, it is much easier to use R.
Using R, we will first fit the Sex model to the tiny data set, just so you can see that R gives you the same parameter estimates you got before. After that we will fit it to the complete data set.
L_Ch7_Fitting_1
Note that the parts that are going to be different for each person (\(X_{i}\) and \(Y_{i}\) and \(e_{i}\)) are called variables (because they vary)! The parts that are going to be the same for each person (\(b_{0}\) and \(b_{1}\)) are called parameters.
We do not need to estimate the variables. Each student in the data set already has a score for the outcome variable (\(Y_{i}\)) and the explanatory variable (\(X_{i}\)), and these scores vary across students. Notice that the subscript \(i\) is attached to the parts that are different for each person. And also note that \(e_{i}\) is not a measured variable, but a function of how far each individual’s thumb length is from their predicted thumb length based on the model.
We do need to estimate the parameters because, as discussed previously, they are features of the population, and thus are unknown. The parameter estimates we calculate are those that best fit our particular sample of data. But we would have gotten different estimates if we had a different sample. Thus, it is important to keep in mind that these estimates are only that, and they are undoubtedly wrong. Calling them estimates keeps us humble!
Parameter estimates don’t vary from person to person, so they don’t carry the subscript \(i\).
Fitting the Sex Model to the Tiny Data Set
We will refer to this more complex model (more complex than the empty model, that is) as the Sex model. It has one explanatory variable, Sex. We will fit the model using R’s lm()
(linear model) function.
To fit the model we run this R code, and get the results below:
lm(Thumb ~ Sex, data=TinyFingers)
Note that the estimates are exactly what you should have expected: the first estimate, for \(b_{0},\) is 59 (the mean for females); the second, \(b_{1}\) is 6, which is the number of millimeters you need to add to the female average thumb length to get average male thumb length.
Notice that the estimate for \(b_{0}\) is labeled “intercept” in the output. You have encountered the concept of intercept before, when you studied the concept of a line in algebra. Remember the equation for a line? \(y=mx+b\). \(m\) represents the slope of the line, and \(b\), the yintercept. The General Linear Model notation is similar to this, though it includes error, whereas the equation for a line does not.
The reason the estimate for \(b_{0}\) is called Intercept is because it is the estimate for thumb length when \(X_{i}\) is equal to 0—in other words, when sex is female. The estimate that R called “Sexmale,” by this line of reasoning, is kind of like the slope of a line. It is the increment in thumb length for a unit increase in \(X_{i}\).
L_Ch7_Fitting_2
If you want—and it’s a good idea—you can save the results of this model fit in an R object. Here’s the code to save the model fit in an object called TinySex.model:
TinySex.model < lm(Thumb ~ Sex, data=TinyFingers)
Once you’ve saved the model, If you want to see what the model estimates are you can just type the name of the model and you will get the same output as above:
TinySex.model
Now that we have estimates for the two parameters, we can put them in our model statement to yield: \(Y_{i} = 59 + 6 X_{i}+e_{i}\).
L_Ch7_Fitting_3
Fitting the Sex Model to the Complete Data Set
Now that you have looked in detail at the tiny set of data, find the best estimates for our bigger set of data (found in the data frame called Fingers) by modifying the code below.
require(mosaic)
require(ggformula)
Fingers < read.csv(file="https://raw.githubusercontent.com/UCLATALL/introstatsmodeling/master/datasets/fingers.csv", header=TRUE, sep=",")
Sex.model <
# this prints out the model estimates
Sex.model
Sex.model < lm(Thumb ~ Sex, data = Fingers)
# this prints out the model estimates
Sex.model
test_object("Sex.model")
test_error()
L_Ch7_Fitting_4