Information Technology Reference
In-Depth Information
The righthand panel of Figure 1-6 shows a histogram for the same data, but with more
bins and with replacements for the default title and x -axis label. It was created like this:
> hist(Cars93$MPG.city, 20, main="City MPG (1993)", xlab="MPG")
See Also
The histogram function of the lattice package is an alternative to hist .
1.20 Performing Simple Linear Regression
Problem
You have two vectors, x and y , that hold paired observations: ( x 1 , y 1 ), ( x 2 , y 2 ), ..., ( x n ,
y n ). You believe there is a linear relationship between x and y , and you want to create
a regression model of the relationship.
Solution
The lm function performs a linear regression and reports the coefficients:
> lm(y ~ x)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
17.72 3.25
Discussion
Simple linear regression involves two variables: a predictor variable , often called x ; and
a response variable , often called y . The regression uses the ordinary least-squares (OLS)
algorithm to fit the linear model:
y i = β 0 + β 1 x i + ε i
where β 0 and β 1 are the regression coefficients and ε i represents the error terms.
The lm function can perform linear regression. The main argument is a model formu-
la , such as y ~ x . The formula has the response variable on the left of the tilde character
( ~ ) and the predictor variable on the right. The function estimates the regression coef-
ficients, β 0 and β 1 , and reports them as the intercept and the coefficient of x ,
respectively:
Coefficients:
(Intercept) x
17.72 3.25
 
Search WWH ::




Custom Search