Model a 3D Point Pattern using spatstat - r

Is there any way to fit a spatial statistical model to 3d data in spatstat?
I have tried using the functions such as ppm and kppm but they are not working for pp3

This is not yet supported in spatstat (that is, the model-fitting functions ppm, kppm and mppm do not accept three-dimensional point pattern data).
We are working on it.

Related

How to get covariate data from a geographic raster for `ppm`?

I want to fit a Poisson point-process model with spatstat::ppm and I'm unsure what is the best way to feed covariate data to the function. I understand that spatstat expects planar coordinates, so I have transformed my point location data to a planar crs before creating a ppp point pattern object. The covariate data are in a raster stack with unprojected geographic coordinates and I understand that projecting rasters is generally ill-advised. I extracted covariate values for the point locations from the raster using the points' original geographic coordinates and raster::extract. So far so good. The issue is ...
it is not sufficient to have observed the covariate only at the points
of the data point pattern; the covariate must also have been observed
at other locations in the window. -ppm helpfile
I appear to have two options for providing the covariate data to the data argument.
A pixel image; seems ill-advised because of raster projection issues.
A list of functions (one per covariate) that can be evaluated at any location (x,y) to obtain corresponding covariate values. This seems like the way to go, but my attempt at writing such a function turns out to be ridiculously slow. It calls raster::extract for each coordinate pair after transforming the coordinates to the raster's crs. While raster::extract is reasonably fast when given a large number of points, there appears to be a substantial overhead for each call. According to microbenchmark, the coordinate transformation takes about 4ms and the extraction takes about 582ms for a single covariate, or about 4 seconds for each point to get all 7 covariates. I don't know how many times ppm will want to call this, but if it's even once per point in the pattern, it'll take too long.
Is there some way I can find out what is the complete set of points that ppm will query for covariate data so that I can extract those beforehand with a single call?
It seems like my use case (covariates in a geographic raster) should be pretty common, so I'm guessing there's an established way to do this right. What is it?
Thanks for a well written question clearly identifying you need. It would have been even better with a simple reproducible example using e.g. built-in data from raster and spatstat or artificially generated data. In lack of the reproducible example my answer will not contain any code but outline what you could do.
First step in ppm is to make a quadrature scheme or class quad or logiquad depending on which maximum likelihood approximation is used in ppm. These can be generated directly by the user via quadscheme or quadscheme.logi. The quadrature scheme contains all the points where ppm will evaluate the covariates. You can extract the coordinates of the quadrature scheme using the function coords. If you construct a data.frame with all covariates evaluated at these points you can supply that as the data argument to ppm while the quadrature scheme is the first argument. To understand things better try to read the Details section of help(ppm.quad).
Another approach which may give you the optimal use of your data is to extract the grid points of you current raster stack together with all the covariate values and project this point data. Then convert it to a simple data.frame with columns x, y, covar1, covar2, etc. Then you can use x and y together with your point observations of interest to create a quadrature scheme manually and the remaining columns can be supplied as data to ppm.
It would be interesting to compare the results from both these approaches as well as the results from just projecting the raster stack and converting it to a list of im objects.

How do I find the corner points of the feasible region for a system of linear inequalities [R]?

My system has only two variables, and I want to plot the polygon in a custom 2D plot.
I suppose it's very tightly connected with linear programming, so I checked the lpSolve package, but there is no way:
not to enter the objective function
not to run the optimization
to retrieve the corner points. I'm not sure if the solver calculates them all anyways.
There is also a method solve2dlp in the package intpoint that actually plots something alike, but somehow I cannot get through the code to check what exactly is it doing..

RANSAC plane fitting coefficients

I am trying to fit a plane to a set of point cloud. I tried using Point Cloud Library (PCL) & it works well. What I need to know is that how can I obtain the coefficients a,b,c of the fitted plane (ax+by+cz+1=0). Is there any straightforward way? I got some insights from here: 3D Least Squares Plane
See the following planar segmentation tutorial:
http://pointclouds.org/documentation/tutorials/planar_segmentation.php
Note in particular the use of the pcl::ModelCoefficients data structure.
Allocation:
pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
Use:
seg.segment (*inliers, *coefficients);
Meaning:
coefficients->values[0]/coefficients->values[3] is your a.
coefficients->values[1]/coefficients->values[3] is your b.
coefficients->values[2]/coefficients->values[3] is your c.
See also:
http://docs.pointclouds.org/1.7.0/structpcl_1_1_model_coefficients.html

R packages to fit three dimensional data using radial basis functions

I'm interested in fitting a three dimensional surface to some spatial data (x, y, z) using a radial basis function approach. I have found that radial basis functions apppear in the R package 'fields' but would like to find an example where it has been used to fit a surface to points in three dimensions. I would thankful if somebody could point me towards or provide a simple example that I could use as a start.

Generating the function of the plane/surface that a given set of coordinates lie on

This is something related with Mathematics as well. But this is useful in computing as well.
Lets say you have 10 coordinates. (x1,y1)(x2,y2)..... in 2D Space. (i.e on a X-Y Plane). Can we find a single smooth curve going across the each coordinate.
While expanding the question, If the space is 3D, then can we find an equation of a smooth surface that going across a given set of spacial coordinates?
Are there any Libraries (Any language) \ tools to perform such calculations?
What you should be looking for is some library implementing NURBS (or Non Uniform Rational B-Splines). This will solve your problem in both 2d and 3d, since 2d is just a special case of the 3d.
Roughly speaking, you are not interested in the actual equation, you are only interested in getting the points approximated with smooth curves or surfaces. This is done by finding "control points" in 2d or 3d space, which are multiplied with B-spline base functions. A NURBS library will do this for you.
Cheers !
Edit:
Have a look at this one
you can always fit an order-10 polynomial through the points. that's not necessarily what you want to do, though - fitting a smooth curve via a series of splines will give you a better-looking result. the curve-fitting article on wikipedia gives you a good overview of the various options.
In the 2D case you are asking for curve fitting. This actually exists in excel, where you plot your points (I usually use XY scatter if you have x and y listed) and then right-click on the curve. Select Add Trendline. There you can choose which kind of function you want to fit to and you can ask excel to display it in the image (Tab named Options, check the box "Display equation on chart"). Nice and quick.
Otherwise you can use matlab and use the lsqr (least square method). If you want to find the polynomial closest that best describes your data you could use the polyfit function. It uses the least square method, but returns coefficients. Matlab has a whole set of other algorithms for solving/finding "best" approximations to systems of linear equations. I mention lsqr because it is one of the simplest to implement yourself if you don't have matlab. On the other hand it is for solving sets of linear equations - I don't know anything about your data.
Have a look at splines
in wiki
an interactive introduction
Searching for 'spline interpolation library' might give some useful hints for implementations.

Resources