Variance in the Outcome: The Black Box

Regression models engage an exercise in variance accounting. How much of the outcome is explained by the inputs, individually (slope divided by standard error is t) and collectively (Average explained/Average unexplained with averaging over degrees of freedom is F). This, of course, assumes normal errors. The sandwich will at least allow t when nonnormal errors prevail. This document provides a function for making use of the black box.

black.box.maker <- function(mod1) {
            d1 <- dim(mod1$model)[[1]]
            sumsq1 <- var(mod1$model[,1], na.rm=TRUE)*(d1-1)
            rt1 <- sqrt(sumsq1)
            sumsq2 <- var(mod1$fitted.values, na.rm=TRUE)*(d1-1)
            rsquare <- round(sumsq2/sumsq1, digits=4)
            rt2 <- sqrt(sumsq2)
            plot(x=NA, y=NA, xlim=c(0,rt1), ylim=c(0,rt1), main=paste("R-squared:",rsquare), xlab="", ylab="", bty="n", cex=0.5)
            polygon(x=c(0,0,rt1,rt1), y=c(0,rt1,rt1,0), col="black")
            polygon(x=c(0,0,rt2,rt2), y=c(0,rt2,rt2,0), col="white")
            }

Invoking the Function

First, a regression model. I will estimate the following regression:

\[ Demand = \alpha + \beta_{1}*Price.Difference + \beta_{2}*Advertising.Spending + \epsilon \]

fresh.reg <- lm(Fresh.Demand ~ Price.Difference+Advertising.Spending, data=fresh.data)
summary(fresh.reg)
## 
## Call:
## lm(formula = Fresh.Demand ~ Price.Difference + Advertising.Spending, 
##     data = fresh.data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.49779 -0.12031 -0.00867  0.11084  0.58106 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            4.4075     0.7223   6.102 1.62e-06 ***
## Price.Difference       1.5883     0.2994   5.304 1.35e-05 ***
## Advertising.Spending   0.5635     0.1191   4.733 6.25e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2383 on 27 degrees of freedom
## Multiple R-squared:  0.886,  Adjusted R-squared:  0.8776 
## F-statistic:   105 on 2 and 27 DF,  p-value: 1.845e-13

Now to the plot.

black.box.maker(fresh.reg)

Voila. How to change it up. Well, three things are required in a copy and paste from the R Commander. First, you will need to import data of some form, obviously. Second, we will need a regression model. Finally, we will need to execute the function black.box.maker on the model with black.box.maker(model.name) just as the code chunk above illustrates.