2

I have read the documentation for this but still unsure of what the function spsample does.

The documentation is here:

https://www.rdocumentation.org/packages/sp/versions/1.3-1/topics/spsample

I came across this while trying to do interpolation on a dataset, referencing this page: https://mgimond.github.io/Spatial/interpolation-in-r.html

Just to clarify, is this function trying to split an area of interest using a grid, and trying to put a value for each cell on the grid using the data that we feed into the function? But I am not sure why the idw function would require a grid that has values. I thought the inputs to idw would just be the points we are trying to interpolate.

Can someone clarify?

3

Yes, gstat::idw can predict at any x,y location, but if you want to show that as a continuous map then you need a raster. The example does:

# Interpolate the grid cells using a power value of 2 (idp=2.0)
P.idw <- gstat::idw(Precip_in ~ 1, P, newdata=grd, idp=2.0)

to predict at a regular grid of x,y coordinates generated by spsample

It then converts the output from idw to a raster:

# Convert to raster object then clip to Texas
r       <- raster(P.idw)

then plotting r will show it as a continuous raster surface (although its really only a grid of point samples).

Equivalently you could create an empty raster object first, and use coordinates(r) to generate a matrix of X,Y coordinates to pass to idw, and then put the returned values into the raster.

  • so spsample generates a grid of x, y coordinates? Sorry but what is it sampling from and why do we need to sample them? I dont quite get that part. How is it possible to randomly sample coordinates? arent coordinates fixed? – shibaducks Mar 15 at 21:45
  • 1
    Its being fed P which is a polygon, and spsample in this instance is creating a set of grid points within P. spsample can do regular, random or other patterns of points. – Spacedman Mar 15 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.