block by armollica 56217d01ddf1370773da

Multivariate linear regression

Full Screen

Multivariate linear regression

Click on the chart to change the model prediction data point (orange line) and see how the independent variables impact one another’s marginal effect on the dependent variable.

Shown are the model predictions (solid line) and 95% confidence regression interval (dashed line) for the following model:

mpg = B0 + B1 wt + B2 hp + B3 (wt x hp) + B4 wt2 + B5 hp2

where

mph = Miles/Gallon

wt = Weight (1000 lb)

hp = Gross horsepower

Because this model is linear in the parameters, Bj, it can be estimated using least-squares. The independent variables, weight and horsepower, enter the model nonlinearly and so the partial derivative of weight and horsepower will vary as weight and horsepower change. In other words, the “marginal effect” on fuel efficiency from an increase in a car’s weight is different depending upon whether the car was intially light or heavy.

The slopes on the above graphs show this marginal effect. The downward sloping functions confirm what would be expected; cars that weigh more and have more horsepower are predicted to be less fuel efficient (have lower miles/gallon).

The (slight) nonlinearity in the regression equation shows that an increase in a car’s horsepower has a varying impact on fuel efficiency depending on whether the car initially had a lot of horsepower or just a little. The slope is steeper at lower horsepower levels and flatter at higher horsepower levels. This implies that an increasing a car’s horsepower by 20 is predicted to decrease fuel efficiency more for a low-horsepower car than it is for a high-horsepower car.

Because of the cross-product term, (wt x hp), the marginal effect of a car’s weight on miles/gallon depends upon the horsepower of the car and vice versa. Clicking on the chart changes the weight and horsepower. Changing the weight changes the marginal effect for horsepower and changing the horsepower changes the marginal effect for weight. Increasing a car’s weight makes the regression equation for horsepower flatter and lower. This implies that for heavier cars, horsepower plays a smaller role in determining fuel efficiency. Note that the orange indicator line cross the model prediction line at the same place.

Here are the equations for the marginal effects (i.e., partial derivatives of mpg):

mpg’wt = B1 + B3 hp + 2 B4 wt

mpg’hp = B2 + B3 wt + 2 B5 hp

Notes: The model and data used here are for demonstration purposes and the model predictions are very weak approximations to a complex system. To see the model results open up the console log in the browser (F12 on Chrome). The data source is mtcars from R’s dataset package. Formulas for regression interval and White’s scaled variance-covariance matrix were found here: http://www.ssc.wisc.edu/~bhansen/econometrics/ Using science.js for linear algebra functions.

index.html

cars.json

line-chart.js

lm.js