Calibration (inverse prediction) from LOESS object in R - r

I have fit a LOESS local regression to some data and I want to be able to find the X value associated with a given Y value.
plot(cars, main = "Stopping Distance versus Speed")
car_loess <- loess(cars$dist~cars$speed,span=.5)
lines(1:50, predict(car_loess,data.frame(speed=1:50)))
I was hoping that I could use teh inverse.predict function from the chemCal package, but that does not work for LOESS objects.
Does anyone have any idea how I might be able to do this calibrationa in a better way than predicticting Y values from a long vector of X values and looking through the resulting fitted Y for the Y value of interest and taking its corresponding X value?
Practically speaking in the above example, let's say I wanted to find the speed at which the stopping distance is 15.
Thanks!

The predicted line that you added to the plot is not quite right. Use code like this instead:
# plot the loess line
lines(cars$speed, car_loess$fitted, col="red")
You can use the approx() function to get a linear approximation from the loess line at a give y value. It works just fine for the example that you give:
# define a given y value at which you wish to approximate x from the loess line
givenY <- 15
estX <- approx(x=car_loess$fitted, y=car_loess$x, xout=givenY)$y
# add corresponding lines to the plot
abline(h=givenY, lty=2)
abline(v=estX, lty=2)
But, with a loess fit, there may be more than one x for a given y. The approach I am suggesting does not provide you with ALL of the x values for the given y. For example ...
# example with non-monotonic x-y relation
y <- c(1:20, 19:1, 2:20)
x <- seq(y)
plot(x, y)
fit <- loess(y ~ x)
# plot the loess line
lines(x, fit$fitted, col="red")
# define a given y value at which you wish to approximate x from the loess line
givenY <- 15
estX <- approx(x=fit$fitted, y=fit$x, xout=givenY)$y
# add corresponding lines to the plot
abline(h=givenY, lty=2)
abline(v=estX, lty=2)

Related

Understanding the Local Polynomial Regression

Could someone explain me why I get different lines when I plot? Somehow I thought the line should be the same
data(aircraft)
help(aircraft)
attach(aircraft)
lgWeight <- log(Weight)
library(KernSmooth)
# a) Fit a nonparametric regression to data (xi,yi) and save the estimated values mˆ (xi).
# Regression of degree 2 polynomial of lgWeight against Yr
op <- par(mfrow=c(2,1))
lpr1 <- locpoly(Yr,lgWeight, bandwidth=7, degree = 2, gridsize = length(Yr))
plot(Yr,lgWeight,col="grey", ylab="Log(Weight)", xlab = "Year")
lines(lpr1,lwd=2, col="blue")
lines(lpr1$y, col="black")
How can I get the values from the model? If I print the model, it gives me the values on $x and $y, but somehow if I plot them, is not the same as the blue line. I need the values of the fitted model (blue) for every x, could someone help me?
The fitted model (blue curve) is correctly in lpr1. As you said, the correct y-values are in lpr1$y and the correct x-values are in lpr1$x.
The reason the second plot looks like a straight line is because you are only giving the plot function one variable, lpr1$y. Since you don't specify the x-coordinates, R will automatically plot them along an index, from 1 to the length of the y variable.
The following are two explicit and equivalent ways to plot the curve and line:
lines(x = lpr1$x, y = lpr1$y,lwd=2, col="blue") # plots curve
lines(x = 1:length(lpr1$y), y = lpr1$y, col="black") # plot line

Plot 3d density

I want to create a 3d plot with densities.
I use the function density to first create a 2d dimensional plot for specific x values, the function then creates the density and puts them into a y variable. Now I have a second set of x values and put it again into the density function and I get a second set of y variables and so on....
I want to put those sets into a 3d plot, I hope you know what I mean. So I have a surface of densities....
E.g. I have:
x1<-c(1:10)
x2<-c(2:11)
y1<-c(1,1,2,1,3,4,2,3,2,2)
y2<-c(1,2,3,1,3,6,2,8,2,2)
.
.
.
.
Now I want to put on the x axis for the first value 1 the first set , on the y axis the corresponding x values and on the z axis the densities. So I have a "disk" for x=1, for x=2 I have the second "disk" and so on, so I get a density "mountain".
I hope I am understandable, if you have a better idea to realize it then you are welcome!
I want to do it with the persp function, would be nice if you make an example with that function,
Thanks a lot for your help.
I'm afraid I can't make head or tail out of your question. But here is how you draw a plot of the sort I think you are looking for from a two dimensional dataset for which you first estimate the bivariate density:
x <- rnorm(1000)
y <- 2 + x*rnorm(1000,1,.1) + rnorm(1000)
library(MASS)
den3d <- kde2d(x, y)
persp(den3d, box=FALSE)
Then there are many options for persp, check out
?persp
Building on Peter answer. The plot can now be more interesting, prettier and interactive with the plotly library.
x <- rnorm(1000)
y <- 2 + x*rnorm(1000,1,.1) + rnorm(1000)
library(MASS)
den3d <- kde2d(x, y)
# the new part:
library(plotly)
plot_ly(x=den3d$x, y=den3d$y, z=den3d$z) %>% add_surface()
which gives:

Only plotting the fitted spline line and not the data points

I have checked my references, it seems to me that to fit a dataset with x and y, many tutorial need to first plot the x and y, then the fitted line is plot. The normal procedure is like below:
## Calculate the fitted line
smoothingSpline = smooth.spline(tree_number[2:100], jaccard[1:99], spar=0.35)
plot(tree_number[2:100],jaccard[1:99]) #plot the data points
lines(smoothingSpline) # add the fitted spline line.
However, I do not want to plot the tree_number and jaccard, but rather, I only want to plot the fitted spline line in the plot, how should I do?
You can use the associcated plot function:
plot(smoothingSpline, type="l")
Or you can extract the x and y values explicitly and plot them
plot(smoothingSpline$x, smoothingSpline$y, type="l")
Why not just plot(smoothingSpline, type = "l")? That should allow you to add the fitted spline line without having to first plot the data points.

Trouble plotting predict line in R

I have a simple data set with two columns of data- K and SwStr.
K = c(.259, .215, .224, .223, .262, .233)
SwStr = c(.130, .117, .117, .114, .113, .111)
I plotted the data using:
plot(res$K, res$SwStr)
I want to plot the result of a linear model, using SwStr to predict K. I try to do that using:
graphic<-lm(K~SwStr-1, data=res)
P=predict(graphic)
plot(res$K, res$SwStr)
lines(P, lty="dashed", col="green", lwd=3)
But when I do this, I don't get any line plotted. What am I doing wrong?
(1) You are inverting the axes of the original plot. If you want SwStr on the x axis and K on the y axis you need
plot(res$SwStr, res$K)
or
with(res,plot(K~SwStr))
If you check the actual values of the plotted points on the graph, this might be obvious (especially if K and SwStr have different magnitudes) ...
For lm fits you can also use abline(graphic,...)
edit: (2) You also have to realize that predict gives just the predicted y values, not the x values. So you want something like this:
K=c(.259, .215, .224, .223, .262, .233)
SwStr=c(.130, .117, .117, .114, .113, .111)
g <- lm(K~SwStr-1)
par(las=1,bty="l") ## my favourites
plot(K~SwStr)
P <- predict(g)
lines(SwStr,P)
Depending on the situation, you may also want to use the newdata argument to predict to specify a set of evenly spaced x values ...

Scatter plot in R

I want a scatterplot in R for points (x,y).
where 500 sample points of x,y are drawn from Normal distribution N(0,1) and N(0,16). Also mark these points as red from distribution N(0,1) and blue for N(0,16).
I am new to R and know only basic plotting. Anyone please help me with this.
Thanks
As the comments suggest, you have two possibilities with data from
x <- rnorm(500, mean=0, sd=sqrt(1))
y <- rnorm(500, mean=0, sd=sqrt(16))
One way would be to plot y against x, but your colour suggestions do not mean anything
plot(y ~ x)
Alternatively you can show the two sets of data with colours, remembering that y probably has a wider range than x.
plot(y, col="blue")
points(x, col="red")
The plots you get are

Resources