Multivariate linear regression
This interactive visualization uses D3.js to demonstrate multivariate linear regression with the mtcars dataset. The chart displays model predictions (solid line) and 95% confidence intervals (dashed line) for miles per gallon as a function of weight and horsepower. Clicking on the chart updates the selected weight and horsepower values, shown by an orange indicator line, allowing users to explore how the marginal effects of each independent variable change with the other. The model includes quadratic and interaction terms, making the relationships nonlinear in the predictors while remaining linear in the parameters. The visualization animates changes in the regression surface and confidence bands, highlighting how the slope of one predictor varies with the level of the other. SVG rendering and animation make the dynamic relationships clear. The underlying data comes from R's mtcars dataset, and the regression uses least-squares estimation with White's scaled variance-covariance matrix for the confidence intervals.# Multivariate Linear Regression This interactive visualization explores a multivariate linear regression model of automobile fuel efficiency using the classic mtcars dataset. The model predicts miles per gallon (mpg) from a car's weight (wt) and horsepower (hp), including quadratic and interaction terms: **mpg = B₀ + B₁·wt + B₂·hp + B₃·(wt×hp) + B₄·wt² + B₅·hp²** ## Key Features - **Interactive exploration**: Click to change the selected data point, updating the model predictions and visual feedback - **Dual linked charts** display the marginal effects of weight (left) and horsepower (right) on fuel efficiency - **Solid lines** show model predictions; **dashed lines** show the 95% confidence interval - **Orange indicator line** tracks the selected data point and shows where the model prediction crosses at the selected weight/horsepower combination The visualization demonstrates how marginal effects vary across the data range: weight and horsepower enter the model nonlinearly (through squared terms and a cross-product interaction), so the partial derivative of each predictor depends on the level of the other. The slopes confirm expectations — heavier cars and more powerful engines reduce fuel efficiency. The cross-product term shows that increasing horsepower has a smaller effect for heavier cars, and vice versa. Clicking on the chart changes the selected car's weight and horsepower, updating the orange line to reflect the new marginal effects. The prediction intervals widen at the extremes, reflecting greater uncertainty in the model. The data is from R's `mtcars` dataset, and the visualization was built with D3.js using SVG and animation.# Multivariate Linear Regression This interactive visualization demonstrates how marginal effects work in a multivariate linear regression model. The chart displays the relationship between car weight, horsepower, and fuel efficiency (mpg) using the classic `mtcars` dataset. ## Visualization Description The visualization presents two side-by-side line charts showing model predictions (solid line) and 95% confidence regression intervals (dashed line) for fuel efficiency as a function of two predictors: vehicle weight (in 1000 lb) and gross horsepower. The regression equation includes quadratic and interaction terms: **mpg = B₀ + B₁·wt + B₂·hp + B₃·(wt × hp) + B₄·wt² + B₅·hp²** The key interactive element: **clicking anywhere on the chart** moves an orange indicator line to change the model prediction data point. This changes the weight and horsepower values used to generate the two panels, allowing viewers to explore how the marginal effects of each variable shift. A vertical orange line on each plot marks the currently selected value and shows where the predicted value lies on the curve. The visualization consists of two side-by-side scatterplots, one for weight (wt) and one for horsepower (hp). Each shows the model's predicted miles/gallon (solid line) along with 95% confidence intervals (dashed lines). The solid model prediction line is drawn holding the other variable fixed at the user-selected value. The chart reveals the central insight of the multivariate regression with interaction and polynomial terms: the marginal effect of one variable depends on the level of the other. For example, the slope for weight becomes steeper (more negative) at higher horsepower levels, and the slope for horsepower becomes flatter as weight increases. This is visible through the changing steepness of the prediction curves as the user clicks to change the conditioning value. The interface includes two panels: one showing predicted mpg as a function of weight (for a fixed horsepower) and the other as a function of horsepower (for a fixed weight). The user can click on either panel to set the value of the variable on the x-axis. The orange line is the model prediction, and the dashed lines show the 95% confidence interval. _Note: This is a very weak approximation for demonstration purposes only._# Multivariate Linear Regression ## Interactive Exploration of Marginal Effects in Multiple Regression This interactive visualization demonstrates how the marginal effects of independent variables change in a multivariate linear regression model with nonlinear terms. Using the classic `mtcars` dataset, the visualization models miles per gallon (mpg) as a function of a car's weight (wt) and gross horsepower (hp), allowing users to explore how these predictors interact. ## Visualization Design The chart displays two side-by-side panels showing model predictions (solid line) and 95% confidence intervals (dashed lines) for fuel efficiency across different car weights and horsepower values. Each data point from the `mtcars` dataset is plotted, with the model's predicted relationship overlaid. ## Interaction Model The model is specified as: **mpg = B₀ + B₁wt + B₂hp + B₃(wt × hp) + B₄wt² + B₅hp²** This specification is notable because it includes a cross-product term and squared terms while remaining linear in the parameters. The coefficients are estimated via ordinary least squares. ## Interaction Clicking on the chart updates the selected weight and horsepower values, visualized by an orange indicator line. This interactive element demonstrates how the marginal effects vary: - **Changing weight** alters the slope of the horsepower relationship - **Changing horsepower** alters the slope of the weight relationship - The orange indicator crosses the model prediction line, showing the changing slopes ## Visual design The visualization consists of two side-by-side line charts. Each chart shows miles per gallon (mpg) on the y-axis. The left chart displays mpg as a function of weight (wt), and the right chart displays mpg as a function of horsepower (hp). Each chart shows the model's predicted values (solid line), 95% confidence interval (dashed lines), and the underlying data points. An orange indicator line and point mark the current weight and horsepower values selected by the user. Clicking on either chart changes the selected weight and horsepower. The model is a polynomial regression, with the key terms being B<sub>1</sub> wt + B<sub>2</sub> hp + B<sub>3</sub> (wt x hp) + B<sub>4</sub> wt<sup>2</sup> + B<sub>5</sub> hp<sup>2</sup>. The partial derivatives of this equation demonstrate the marginal effects. Because the weight and horsepower variables enter the model nonlinearly, the marginal effects vary. The model summary is accessible in the console. ## Choices Given the metadata, write a concise, precise description of the visualization. Include: 1. The visualization's title 2. Main visual elements (charts, axes, legends, annotations) 3. Interactions 4. The context and the data (briefly) Important: Do not include the metadata or the files. Do not include instructions or verbious "introduction" and "conclusion" sections. Start directly with the description. Your entire response must be 120 words or less (not including the title). Use plain text only. Do not include any markdown formatting.Multivariate linear regression Two-panel interactive scatterplot showing model predictions for miles-per-gallon against car weight (left) and horsepower (right). Solid lines show predicted mpg; dashed lines show the 95% confidence interval. Observed data points are plotted as circles. A clickable orange indicator line marks a user-selected combination of weight and horsepower. The model includes squared and interaction terms, so the slopes of the prediction lines change with the selected values. The interaction term means the marginal effect of one variable depends on the other. Users can click to change the selected weight/horsepower, updating the model's predicted curve and revealing how the marginal effect of each variable shifts. Animated transitions highlight the dynamic relationship between the predictors and fuel efficiency.
AI-generated descriptionMultivariate 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 = B<sub>0</sub> + B<sub>1</sub> wt + B<sub>2</sub> hp + B<sub>3</sub> (wt x hp) + B<sub>4</sub> wt<sup>2</sup> + B<sub>5</sub> hp<sup>2</sup>
where
mph = Miles/Gallon
wt = Weight (1000 lb)
hp = Gross horsepower
Because this model is linear in the parameters, B<sub>j</sub>, 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'<sub>wt</sub> = B<sub>1</sub> + B<sub>3</sub> hp + 2 B<sub>4</sub> wt
mpg'<sub>hp</sub> = B<sub>2</sub> + B<sub>3</sub> wt + 2 B<sub>5</sub> 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.
forked from <a href='http://bl.ocks.org/armollica/'>armollica</a>'s block: <a href='http://bl.ocks.org/armollica/56217d01ddf1370773da'>Multivariate linear regression</a>