(flash enabled browser required)
For the simplest visualization, deselect 'trails' and select (checkbox) the variables DEFICIT, INCOME_TAX, TOTAL_REVENUE.
For the best visualization, deselect 'trails' under color select 'unique colors' for Size select 'IN_BILLIONS' and change the X-axis to PCT_GDP and the y-axis to IN_BILIONS. Select variables DEFICIT, INCOME_TAX, TOTAL_REVENUE
In any case, notice how early on, in the years following cuts in marginal income taxes, total revenues are increasing, revenues from income taxes are increasing, and the DEFICIT IS PLUNGING. All along spending is steadily incresing. Then about 2008 the deficit literally takes off running, both in billions of dollars and as a percentage of GDP as tax revenues start to plunge. Spending finds itself trapped as far as it can go in the northeast corner of the graph.
This is consistent with my previous graphical visualization here.
For more info on the code and references for this visualization see here.
The data source is the CBO Budget/Historical Tables. I'd provide a link but it moves around constantly. Just Google it and did for it and you can find the data. (or see below)
This is the format required for the R googleVis package. (I saved it as a csv file)
BUDGET_ITEM YEAR IN_BILLIONS PCT_GDP
CORP_TAX 2003 131.8 34.90604764
CORP_TAX 2004 189.4 45.88989817
CORP_TAX 2005 278.3 87.42060525
CORP_TAX 2006 353.9 142.5975397
CORP_TAX 2007 370.2 230.3657102
CORP_TAX 2008 304.3 667.9837559
CORP_TAX 2009 138.2 9.782782586
DEFICIT 2003 377.585 3.381560093
DEFICIT 2004 412.727 3.507495538
DEFICIT 2005 318.346 2.556790619
DEFICIT 2006 248.181 1.881156674
DEFICIT 2007 160.701 1.15429536
DEFICIT 2008 45.555 0.311849671
DEFICIT 2009 1412.686 9.313594409
INCOME_TAX 2003 793.7 7.108185563
INCOME_TAX 2004 809 6.875159344
INCOME_TAX 2005 927.2 7.446791422
INCOME_TAX 2006 1043.9 7.912529372
INCOME_TAX 2007 1163.5 8.357276253
INCOME_TAX 2008 1145.7 7.84296276
INCOME_TAX 2009 915.3 6.034414557
SPENDING 2003 2159.906 19.34359663
SPENDING 2004 2292.853 19.48545084
SPENDING 2005 2471.971 19.85359409
SPENDING 2006 2655.057 20.12474039
SPENDING 2007 2728.702 19.59992817
SPENDING 2008 2982.554 20.41726451
SPENDING 2009 3517.681 23.19146229
TOTAL_REVENUE 2003 131.8 1.180368977
TOTAL_REVENUE 2004 189.4 1.609586131
TOTAL_REVENUE 2005 278.3 2.235161834
TOTAL_REVENUE 2006 353.9 2.682483135
TOTAL_REVENUE 2007 370.2 2.659100704
TOTAL_REVENUE 2008 304.3 2.083105148
TOTAL_REVENUE 2009 138.2 0.911128692
An attempt to make sense of econometrics, biostatistics, machine learning, experimental design, bioinformatics, ....
Thursday, January 13, 2011
Wednesday, January 12, 2011
Using R to Generate Motion Charts via Google's Visualization API
Just press the play button to get the full effect.(requires a flash enabled browser) Notice the ability to select variables and follow particular variables, the label options etc. are pretty robust. This utilizes Google's visualization tools but my local data.
The R code I used can be found here.
I figured out how to do this as a result of a recent the Revolution Analytics blog post demonstrating how to create motion charts with the new GoogleVis package. They link to an easy to follow tutorial on stack-overflow that walks you through the code.
Apparently the GoogleVis package generates script that accesses google’s Visualization API and allows you to use their data and visualizations. Or you can visualize your own data using their motion charts as I have done using the well known iris data set (adding a time variable as did the blogger kohske).
I found the Hans Rosling data visualization video inspiring and back in December I tried to create a similar visualization of agricultural subsidies, but it was a one time period static view. See below
or see here for the original post)
My next project will be to revisit the agricultural subsidy data, which is available across time, and convert my original bubble chart to a motion chart.
The R code I used can be found here.
I figured out how to do this as a result of a recent the Revolution Analytics blog post demonstrating how to create motion charts with the new GoogleVis package. They link to an easy to follow tutorial on stack-overflow that walks you through the code.
Apparently the GoogleVis package generates script that accesses google’s Visualization API and allows you to use their data and visualizations. Or you can visualize your own data using their motion charts as I have done using the well known iris data set (adding a time variable as did the blogger kohske).
I found the Hans Rosling data visualization video inspiring and back in December I tried to create a similar visualization of agricultural subsidies, but it was a one time period static view. See below
or see here for the original post)
My next project will be to revisit the agricultural subsidy data, which is available across time, and convert my original bubble chart to a motion chart.
R Code for googleVis Demo
# ------------------------------------------------------------------
# | PROGRAM NAME: googleVis_R
# | DATE: 1/12/11
# | CREATED BY: Matt Bogard
# | PROJECT FILE:
# |----------------------------------------------------------------
# | PURPOSE: Tutorial for creating Motion Charts in R with the GoogleVis package
# |
# |
# |
# |------------------------------------------------------------------
# | COMMENTS: See the following references for more details
# |
# | 1: http://blog.revolutionanalytics.com/2011/01/create-motion-charts-in-r-with-the-googlevis-package.html
# | 2: http://stackoverflow.com/questions/4646779/embedding-googlevis-charts-into-a-web-site/4649753#4649753
# | 3: http://cran.r-project.org/web/packages/googleVis/googleVis.pdf
# | 4: for more info on accessing the google API and data format requirements:
# | http://code.google.com/apis/visualization/documentation/gallery/motionchart.html#Data_Format
# |
# |
# |------------------------------------------------------------------
# | DATA USED: via google & iris data set
# |
# |
# |------------------------------------------------------------------
# | CONTENTS:
# |
# | PART 1: motion chart using googleVis data
# | PART 2: motion chart using your own data- this case
# | the well know iris data set (a default R data set)
# | PART 3:
# | PART 4:
# | PART 5:
# |
# |-----------------------------------------------------------------
# | UPDATES:
# |
# |
# ------------------------------------------------------------------
# *------------------------------------------------------------------*
# | set R working directory- this is where your data file will go
# | with the script for creating the visualization
# *------------------------------------------------------------------*
setwd("C:\\your directory\\R Data")
# *------------------------------------------------------------------*
# | install the googleVis package (as with any package, this only has
# | to be done forthe initial first use
# *------------------------------------------------------------------*
install.packages('googleVis')
# *------------------------------------------------------------------*
# | call the googleVis library
# *------------------------------------------------------------------*
library(googleVis)
# *-----------------------------------------------------------------*
# |
# |
# |
# | PART 1: motion chart using googleVis data
# |
# |
# |
# *------------------------------------------------------------------*
# *------------------------------------------------------------------*
# | create googelVis data object
# *------------------------------------------------------------------*
M <- gvisMotionChart(Fruits, "Fruit", "Year")
# *------------------------------------------------------------------*
# | look at data object- this includes the script that
# | will be used if you want to publish on your web page/blog
# *------------------------------------------------------------------*
print(M)
# *------------------------------------------------------------------*
# | plot the visualization-this command will open your default browser
# | and produce the visualization - this may not work depending on your
# | security and browser settings
# *------------------------------------------------------------------* plot(M)
# *------------------------------------------------------------------*
# | create the data object that contains everything necessary to create the
# | chart on your web site/blog
# *------------------------------------------------------------------*
M$html$chart
# *------------------------------------------------------------------*
# | save the data object, which is an html file in your R
# | data directory
# *------------------------------------------------------------------*
cat(M$html$chart, file="tmp.html")
# from this point you can open the file in say, notepad++ and copy the
# script into your blog or web page and the motion chare will be functional
# *-----------------------------------------------------------------*
# |
# |
# |
# | PART 2: motion chart using your own data- this case
# | the well know iris data set (a default R data set)
# |
# |
# |
# *------------------------------------------------------------------*
# *------------------------------------------------------------------*
# | take a look at the data
# *------------------------------------------------------------------*
names(iris)
print(iris)
# simulate a time variable and add it to the data set
iris$time <- rep(1:50, 3)
names(iris)
# *------------------------------------------------------------------*
# | create googelVis data object
# *------------------------------------------------------------------*
r <- gvisMotionChart(iris, "Species", "time")
# *------------------------------------------------------------------*
# | look at data object- this includes the script that
# | will be used if you want to publish on your web page/blog
# *------------------------------------------------------------------*
names(r)
print(r)
# *------------------------------------------------------------------*
# | plot the visualization-this command will open your default browser
# | and produce the visualization - this may not work depending on your
# | security and browser settings
# *------------------------------------------------------------------*
plot(r)
# *------------------------------------------------------------------*
# | create the data object that contains everything necessary to create the
# | chart on your web site/blog
# *------------------------------------------------------------------*
r$html$chart
# *------------------------------------------------------------------*
# | save the data object, which is an html file in your R
# | data directory
# *------------------------------------------------------------------*
cat(r$html$chart, file="tmp2.html")
Monday, January 10, 2011
Comparing OLS, Ridge Regression, LAR, and LASSO
The following penalized residual sums of squares differentiate Ridge Regression, LAR and LASSO from OLS:
min{e'e + λβ'β) Ridge Regression
min{e'e + λβ) Least Angle Regression
min{e'e + λ|β|) Least Absolute Shrinkage and Selection Operator
min(e'e) Ordinary Least Squares
min{e'e + λβ'β) Ridge Regression
min{e'e + λβ) Least Angle Regression
min{e'e + λ|β|) Least Absolute Shrinkage and Selection Operator
min(e'e) Ordinary Least Squares
Least Angle Regression and LASSO
Least Angle Regression (LAR)
Similar to ridge regression, LAR is a shrinkage estimator. It is similar to forward selection, but only enters 'as much' of the β estimate as necessary. The β estimate is increased with each iteration of the algorithm, approaching the least squares estimate of β.
1) Start with predictors near 0, let r = yi- yest
2) Find Xi most correlated with r.
3) Move βest --> βls until corr (Xi, r) = corr(Xj, r) for some Xj
4) Continue until all X's have been entered
If μk = Xβ, then LAR finds μk that makes the smallest angle between each of the predictors and r.
LAR, as a shrinkage estimator, minimizes a penalized residual sum of squares:
Similar to ridge regression, LAR is a shrinkage estimator. It is similar to forward selection, but only enters 'as much' of the β estimate as necessary. The β estimate is increased with each iteration of the algorithm, approaching the least squares estimate of β.
1) Start with predictors near 0, let r = yi- yest
2) Find Xi most correlated with r.
3) Move βest --> βls until corr (Xi, r) = corr(Xj, r) for some Xj
4) Continue until all X's have been entered
If μk = Xβ, then LAR finds μk that makes the smallest angle between each of the predictors and r.
LAR, as a shrinkage estimator, minimizes a penalized residual sum of squares:
min{e'e + λβ)
where λ is a penalty or shrinkage factor.
Least Absolute Shrinkage and Selection Operator (LASSO)
In essence, with LASSO we have a LAR estimator that is penalized such that |β| < c where c is some constant.
If we minimize the following penalized residual sum of squares:
If we minimize the following penalized residual sum of squares:
min{e'e + λ|β|)
References:
Ridge Regression
Ridge Regression projects Y onto principle components, or fits a linear surface over the domain of the PC's. Ridge regression shrinks regression co-efficients with respect to the orthonormal basis formed by the principle components. It is a shrinkage method. In producing the coefficient estimates, a 'penalized' residual sum of squares is minimized.
By its nature, ridge regression addresses multicollinearity. The estimates have increased bias, but lower variance. Ridge regression is an example of a case where a biased estimator can outperform an unbiased estimator given small enough variance (or large enough improvements in efficiency.
By its nature, ridge regression addresses multicollinearity. The estimates have increased bias, but lower variance. Ridge regression is an example of a case where a biased estimator can outperform an unbiased estimator given small enough variance (or large enough improvements in efficiency.
min{e'e + λβ'β) 'penalized residual sum of squares' where λ is a penalty or shrinkage factor
References:
Tuesday, January 4, 2011
Analysis of the Logistic Funtion
In my post on logistic regression and maximum likelihood, I note the following in relation to the logit model:
Ln ((Di /(1 – Di)) = βX [equation 1a]
Di = probability y = 1 = eXβ / ( 1 + eXβ ) [equation 1b ]
Where : Di / (1 – Di) = ’odds ratio’ or ’odds’
E[y|X] = Prob(y = 1|X) = p = eXβ / (1 + eXβ ) [equation 1c]
In other words, the probability that y = 1 can be represented by the logistic function:
eXβ / ( 1 + eXβ ) [ equation 1d] or substituting t for XB:
et / ( 1 + et ) [equation 1e]
Often, we will see the logistic function represented as:
1/ ( 1 + e-t ) [equation 2]
Algebraically, one could show that these are equivalent, or alternatively, you could evaluate the functions numerically and graphically using R.
Graphing the formulation in equations 1b-1e or et / ( 1 + et ) in R gives the following:
Graphing 1/ ( 1 + e-t ) gives the following:

You can also show, numerically, that both functions give the same value evaluated at the same level of t:
t = 4 ( or XB = 4)
e4 / ( 1 + e4 ) = 0.9820138
1 / ( 1 + e-4 ) = 0.9820138
I also defined the likelihood function as follows:
L(β) = ∏f(y, β) = ∏ eXβ / (1 + eXβ ) ∏ 1/(1 + eXβ) [equation 3]
Which is (probability y=1)*(probability y = 0) or the joint density of y.
We've already worked with probability y = 1; et / ( 1 + et ). If 'p' is the probability y = 1, then 1-p would give the probability that y = 0. This would be 1 - [et / ( 1 + et )]. This turns out to be the term in the second product above in equation 3.
(prob y =0) = 1/ ( 1 + et ) [equation 4]
Note, this is different from equation 2 in that the 't' is positive and not negative. We can check that these equations give the correct results numerically as follows:
For t = 3, p(y = 1) :
e4 / ( 1 + e4 ) = 0.9820138
p(y = 0) = 1/ ( 1 + e4 ) = 0.01798621
Note, the simpler calculation (1-p) gives us the same result: 1-0.9820138 = 0.0179862
t <-4
1/(1+exp(t))
This can be graphed as well:
y0 <- 1/(1 + exp(t))
plot(t, y0, type="l", lty=2, xlab="t value",
ylab="Density", main="Sigmoid/Logistic Distribution 1/(1 + exp(t))")
Ln ((Di /(1 – Di)) = βX [equation 1a]
Di = probability y = 1 = eXβ / ( 1 + eXβ ) [equation 1b ]
Where : Di / (1 – Di) = ’odds ratio’ or ’odds’
E[y|X] = Prob(y = 1|X) = p = eXβ / (1 + eXβ ) [equation 1c]
In other words, the probability that y = 1 can be represented by the logistic function:
eXβ / ( 1 + eXβ ) [ equation 1d] or substituting t for XB:
et / ( 1 + et ) [equation 1e]
Often, we will see the logistic function represented as:
1/ ( 1 + e-t ) [equation 2]
Algebraically, one could show that these are equivalent, or alternatively, you could evaluate the functions numerically and graphically using R.
Graphing the formulation in equations 1b-1e or et / ( 1 + et ) in R gives the following:
{code}
# define t-values
t <- seq(-4, 4, length=100)
# sigmoid/logistic = exp(t)/(1 + exp(t))= probability y =1
y <- exp(t)/(1 + exp(t))
plot(t, y, type="l", lty=2, xlab="t value",
ylab="Density", main="Sigmoid/Logistic Distribution exp(t)/(1 + exp(t))")
Graphing 1/ ( 1 + e-t ) gives the following:

{code}
# sigmoid/logistic = 1/(1 + exp(-t))= probability y =1
y <- 1/(1 + exp(-t))
plot(t, y, type="l", lty=2, xlab="t value",
ylab="Density", main="Sigmoid/Logistic Distribution 1/(1 + exp(-t))")
You can also show, numerically, that both functions give the same value evaluated at the same level of t:
t = 4 ( or XB = 4)
e4 / ( 1 + e4 ) = 0.9820138
1 / ( 1 + e-4 ) = 0.9820138
{ code}
t <-4
exp(t)/(1+ exp(t))
1/(1+exp(-t))
I also defined the likelihood function as follows:
L(β) = ∏f(y, β) = ∏ eXβ / (1 + eXβ ) ∏ 1/(1 + eXβ) [equation 3]
Which is (probability y=1)*(probability y = 0) or the joint density of y.
We've already worked with probability y = 1; et / ( 1 + et ). If 'p' is the probability y = 1, then 1-p would give the probability that y = 0. This would be 1 - [et / ( 1 + et )]. This turns out to be the term in the second product above in equation 3.
(prob y =0) = 1/ ( 1 + et ) [equation 4]
Note, this is different from equation 2 in that the 't' is positive and not negative. We can check that these equations give the correct results numerically as follows:
For t = 3, p(y = 1) :
e4 / ( 1 + e4 ) = 0.9820138
p(y = 0) = 1/ ( 1 + e4 ) = 0.01798621
Note, the simpler calculation (1-p) gives us the same result: 1-0.9820138 = 0.0179862
{code}
1/(1+exp(t))
This can be graphed as well:
{code}
y0 <- 1/(1 + exp(t))
plot(t, y0, type="l", lty=2, xlab="t value",
ylab="Density", main="Sigmoid/Logistic Distribution 1/(1 + exp(t))")
Subscribe to:
Posts (Atom)



