Regression Weighting

Types of weighting

  • Analytic Weights : Useful when dealing with averages in data by providing a proportional weight given number of obs.
  • Sampling Weights (Inverse Probability Weights) : Useful when dealing with data that has missing values.

Stata makes this very easy by just attaching aweight or pweight to the end of the regression line. However, in R, it requires a bit of understanding for each packages. The lm() function only does analytic weighting, but for sampling weights, the survey package is used to to build a survey design object and run glm(). By default, the survey package uses sampling weights.

Sample data.frame (from dput)

Analytic Weights


Sampling Weights (IPW)

Output :

Note that the coefficients are the same, but the standard errors have been reduced from the analytic weighting.

Spatial Visualization

Interpolating Contour Maps

Some prelimimary research has lead me into the world of Spatial Plotting in R. Below is a small example of average precipitation in California during 2000. The code consists of building spatial objects, interpolation of data points, and then plotting with ggplot2. For simplicity, the data has already been manipulated, tidied and provided below. Source for data is from PRISM

Data set (164 mb) : CA_2000_appt.csv

R Code : California.R


Creating Spatial Object

The first step is to build a spatial object consisting of California latitude and longitude coordinates for the entire state. This will allow the object to be plotted correctly with the precipitation spatial points built below.

Spline Interpolation with akima package

Next, additional data points need to be interpolated from the given values in the data.frame in order to increase the clarity on the map. The steps for this section of code include interpolating the data, melting each lat/long for each interpolated appt, building the data.frame, and merging the California Spatial data.frame from above, with the original appt and interpolated points.

Plot Spatial Objects with ggplot2

And finally, build the aesthetics for ggplot2, overlay appt, contour appt concentrations, title plot, and apply border.

image: California-2000


Thanks to @kdauria on Stack Exchange for helping with the code for interpolation and countour plots.

CRAN Task View: Analysis of Spatial Data

Introduction to Visualising Spatial Data in R

The R Book - Michael J. Crawley

ggplot2 Help Topics


Mapping Seattle Crime

Mapping San Francisco Crime

this is how i did it…mapping in r with ggplot2