Drawing 3D eigenvectors in R - r

I'm currently attempting to construct a 3D confidence region based on the following quadratic form;
confidence region for mean mu
while I have the ellipsoid part figured out; I can't seem to figure out a way to include the major and minor axis. As per the textbook I'm using, the major axis is
length of major axis units in the direction of $e_i$--the ith eigenvector of the estimate of the covariance matrix S. For plotting the ellipsoid, I used the following code that required the rgl package:
plot3d(ellipsoid(mu_1,S_1,(((n-1)*p)/(n-p))*(qf(0.05,df1=p,df2=n-p,lower.tail = FALSE)),segments=100),alpha=0.5,color='blue')
But I believe the function creates its own 3D mesh object within which it produces the ellipse, so I'm having a lot of trouble constructing the 3D eigenvectors on the same 3D plane. In fact, even if I completely abandon the ellipsoid part, I can't seem to find a way to even just draw out the 3D eigenvectors centered at $\bar{x}$. I've loaded packages like plotly and matlib and even tried something as dumb as defining a 3D parametric function of a line that went as follows:
lines <- function(x0,a){ t <- seq(0,1,length=10000) return(matrix(x0+c(a[1]*t,a[2]*t,a[3]*t),nrow=10000,ncol=3,byrow=TRUE)) }
I then went ahead with the plot3d function, to no avail. Any help would be appreciated!

Related

How to compute a 2-D embedding from a distance matrix?

I would like to draw a map of NYC where the distances between neighborhoods are scaled by transit time. I computed a distance matrix using the Google Maps Distance Matrix API for public transit.
To visualize it, I tried embedding using sklearn.manifold.MDS (metric MDS) and then making a scatter plot. This works kind of ok. Unfortunately, the axes are rescaled as part of this process, and I would like to provide a scale that reflects the raw transit time. In addition, the orientation of the points does not influence the fit, but for drawing a map, it would be nice if I could specify the orientation.
Does anyone have other methods that would be a better fit for my problem? I thought about fixing some reference points and using them to triangulate the remaining points on a map, but I don't know of an off-the-shelf way to do this.

scatterplot3d package; how do you re-size a regression plane

I am interested in generating a series of simple 3d scatterplots which include regression planes without interactions using the scatterplot3d function in R. The following code generates almost what I am after with one problem- in many cases the regression plane extends outside of the bounding box (e.g. in this case, the corner nearest x, y and z =0). I tried changing the axis limits to increase the box size, but this does not alter the axis ranges as specified (which, according to the package documentation is an unfixed bug). Is there a way to either 1) re-draw the box to include the entire plane or 2) shrink the plane to include only the portion within the box?
example data
bugs<-c(335.20,8.68,1.94,3.22,21.79,11.16,1618.00,108.76,250.59,400.81,233.86,15.05,274.62,419.21)
max_dq<-c(0.015,0.001,0.001,0.001,0.002,0.007,0.04,0.001,0.014,0.003,0.002,0.006,0.004,0.013)
since_dist<-c(21,58,5,1,1,19,42,33,22,300,240,79,327,42)
library(scatterplot3d)
3 d plot
reg_plt<-scatterplot3d(max_dq,since_dist,bugs,angle=50)
regression plane
reg_plt$plane3d(lm(bugs~max_dq+since_dist))

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.

Slerp with more than two points

The correct way to interpolate between two points on a sphere is using slerp.
How would one interpolate between more than two points on a sphere? So summing a set of points with different weights on the surface of a sphere?
Simply summing the points multiplied by their weights and then normalising the result is not accurate enough when the angles are large. We need 'true' spherical interpolation.
I asked this question on math.stackexchange.com, and someone found a paper that describes exactly this. Here it is: Spherical Averages and Applications to Spherical Splines and Interpolation
The problem I see is:
Slerp gives constant velocity. That is, a given increment in your interpolation parameter gives you the same distance on the sphere, regardless of where you are on the [0,1] range.
Unfortunately, because the sphere is curved, you can't do this for more than one interpolation parameter. Either you need to give up constant velocity, or give up interpolating with more than one parameter.
You may be able to find an interpolation function that isn't constant velocity that nonetheless satisfies your requirements. But because of the above problem, I don't think it will correspond directly and symmetrically to the 1-D slerp.

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