title: Confining an IDW interpolation to a polygon in R{Spatstat}
body: <p>I'm a fairly new R user working with spatial data for places embedded within US metro areas.  I'm working on generating IDW maps for a number of variables and am having trouble defining the boundaries of the IDW.  I would like to interpolate data only within the confines of the metro area boundary, while my current approach fills the entire plot.</p>
  
  <p>I begin by creating a <code>ppp</code> object from a <code>dataframe</code> as such:</p>
  
  <pre><code>x.points&lt;-SpatialPointsDataFrame(coords =x.data[,c("LON10","LAT10")],
    data=x.data[,c("v1","v2")],
    proj4string=CRS("+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs"))
  
  x.proj&lt;-spTransform(x.points,CRSobj = CRS("+proj=aea
    +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 
    +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))    
  
  x.ppp&lt;-as.ppp(x.proj)
  </code></pre>
  
  <p>Projecting this <code>ppp</code> object  using <code>plot(x.ppp)</code> displays a plot for each of the variables ("v1" and "v2" in the code above).  However, the plot fills the entire rectangular frame.  I limit this frame when plotting my <code>ppp</code> object by assigning an <code>owin</code> object to it, as follows:</p>
  
  <pre><code>y.spdf&lt;-spTransform(y.spdf,CRSobj = CRS("+proj=aea 
    +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 
    +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))
  
  y.owin&lt;-as.owin(y.spdf)
  
  x.ppp&lt;-x.ppp[y.owin]
  </code></pre>
  
  <p>Plotting this using <code>plot(x.ppp)</code> now gives me a plot of my data points correctly framed within the polygon of their metro area.  While this is a good start, I want to continue to pass this spatial frame onto an IDW map.  Consulting the spatstat handbook leads me to believe that I could pass my <code>owin</code> object as an <code>as.mask</code> argument, but whenever I do, I get an error.  Example code follows:</p>
  
  <pre><code>idw&lt;-idw(x.ppp, power=4, as.mask(y.owin))
  </code></pre>
  
  <p>This gives me the error "Error in switch(at, pixels = { : EXPR must be a length 1 vector".  Using <code>at="points"</code> does not work, as I instead get a resulting <code>dataframe</code> object rather than an <code>imlist</code>.  If I leave out the <code>as.mask</code> argument I get a correct idw, but it interpolates beyond the metro area boundaries that I was previously able to set using the <code>owin</code> object.</p>
  
  <p>I figure this is a fairly common task, but consuting the spatstat documentation left me at a bit of a loss.  The <code>as.mask</code> argument seems like it would work, and I'm not sure where I'm going wrong.  Any input or advice on how to solve this problem, or a source to which I could turn to try and better understand the issue would be a great help.  Thanks!</p>