Previous: Example 2: Creating dummy Up: For-Loops Next: Writing New Models

Example 3: Weighted regression with subsets

Selecting the by option in zelig() partitions the data frame and then automatically loops the specified model through each partition. Suppose that mydata is a data frame with variables y, x1, x2, x3, and state, with state a factor variable with 50 unique values. Let's say that you would like to run a weighted regression where each observation is weighted by the inverse of the standard error on x1, estimated for that observation's state. In other words, we need to first estimate the model for each of the 50 states, calculate 1 / SE(x151#51 ) for each state 52#52 , and then assign these weights to each observation in mydata.



Gary King 2011-11-29