How to get inverse prediction from a bspline smooth fit in R - r

I was using functional data analysis techniques to smooth a discrete dataset. I was using the fda package and the function smooth.basis. It gives me a fitted smooth object, yet now I need to get the inverse of it. The codes are something like the following:
basisobj<-create.bspline.basis(c(0,1),7)
fit=smooth.basis(argvals=Data$Time,y=Data$Score,fdParobj=basisobj)$fd
I wish to know what is the value of Data$Time when Data$Score is, say 20.
I did try the inverse.predict function but it could only give the result for lm function.
Thank you!

Related

When predicting using R ARIMA object, how to declare the time series' history?

Suppose I fit AR(p) model using R arima function from stats package. I fit it using a sample x_1,...,x_n. In theory, when predicting x_{n+1} using this model, it needs an access x_n,...x_{n-p}.
How does the model know which observation I want to predict? What if I wanted to actually predict x_n based on x_{n-1},...,x_{n-p-1} and how my code would differ in this case? Can I make in-sample forecasts, similar to Python's functionality?
If my questions imply that I think about forecasting in a wrong way, please kindly correct my understanding of the subject.

Inverse prediction using drm package in R

I've fit a model using a 5 parameter logistic fit using the drm library. I apologize if this is a dumb question; I'm just getting started with r.
If dose in on my x-axis and response is on my y-axis, it is very easy to use this model to predict my response based on a given dose. You can either use the function PR or predict. However, I want to estimate a dose for a given response. I can't find a function to do this. For my assay, I fit my data to a standard curve and now I have measured a response from my unknowns. I would like to estimate concentration (dose) based on this response. I could fit the data in the opposite direction (flip x and y) but the fit differs slightly and that's not a very conventional strategy. If anyone has any suggestions I would greatly appreciate them. thank you
In case any one comes across this, the easiest way to do this is to use the ED function with the argument type = "absolute".

Trouble with abline

I'm having trouble plotting 2 abline()s on a graph of the log10 Brain mass and log10 body mass. I'm following someone else's script but it just doesn't work for me. This is what I have:
This is the graph produced:
Why are the lines off there like that? Are the values I'm getting for the intercept and slope incorrect, or am I using the wrong ones? I've done other examples of this and its worked OK, but I've always ended up using the first model, never the second one so I'm not sure if I'm using the right values.
If you want to represent a linear regression of the log of the body mass compared to the log of the brain mass, the code is:
model <- lm(log10(brain)~log10(body))
then
abline(model$coefficients[2], model$coefficients[1])
When you don't know which parameter to enter in a function, use the help of that function. For abline(), the first parameter is the slope and the second one is the intercept.
Currently, your model use log10(brain), log10(body) and class.
If you want to assess the quality of your model, look at the residuals.
plot(model)
You can also just use the result of your lm like this:
model <- lm(log10(brain)~log10(body))
plot(log10(brain)~log10(body))
abline(model,col=2)

fourier() vs fourierf() function in R

I'm using the fourier() and fourierf() functions in Ron Hyndman's excellent forecast package in R. Looking to verify whether the same terms are selected and used in fourier() and fourierf(), I plotted a few of the output terms.
Below is the original data using ts.plot(data). There's a frequency of 364 in the time series, FYI.
Below is the plot of the terms using fourier(data,3). Basically, it looks like mirror images of the existing data.
Looking at just the sin1 term of the output, again, we get some variation that shows similar 364-day seasonality in line with the data above.
However, when I plot the results of the Fourier forecast using fourierf(data,3, 410) I see the below data. It appears far more smooth than the terms provided by the original fourier function.
So, I wonder how the results of fourier() and fourierf() are related. Is it possible to just see one consolidated Fourier result, so that you can see the sin or cosine result moving through existing data and then through the forecasting period? If not, how can I confirm that the terms created by fourierf() fit the in-sample data?
I want to use it in an auto.arima or glm function with other external regressors like this:
trainFourier<-fourier(data,3)
trainFourier<-as.data.frame(trainFourier)
trainFourier$exogenous<-exogenousData
arima.object<-auto.arima(data, xreg=trainFourier)
futureFourier<-fourierf(data,3, 410)
fourierForecast<-forecast(arima.object, xreg=futureFourier, h=410)
and want to be completely sure that the auto.arima has the proper fitting (using the terms from fourier()) to what I'll put in under xreg for forecast (which has terms from a different function, i.e. ffourier()).
Figured out the problem. I was using both the fda and forecast packages. fda, which is for functional data analysis and regression, has its own fourier() function. If I detach fda, my S1 term from fourier(data,3) looks like this:
which lines up nicely with the Fourier forecast if I use ts.plot(c(trainFourier$S1,futureFourier$S1))
Moral of the story -- watch what your packages supress, folks!

How can I estimate the logarithmic form of data points using R?

I have data points that represent a logarithmic function.
Is there an approach where I can just estimate the function that describes this data using R?
Thanks.
I assume that you mean that you have vectors y and x and you try do fit a function y(x)=Alog(x).
First of all, fitting log is a bad idea, because it doesn't behave well. Luckily we have x(y)=exp(y/A), so we can fit an exponential function which is much more convenient. We can do it using nonlinear least squares:
nls(x~exp(y/A),start=list(A=1.),algorithm="port")
where start is an initial guess for A. This approach is a numerical optimization, so it may fail.
The more stable way is to transform it to a linear function, log(x(y))=y/A and fit a straight line using lm:
lm(log(x)~y)
If I understand right you want to estimate a function given some (x,y) values of it. If yes check the following links.
Read about this:
http://en.wikipedia.org/wiki/Spline_%28mathematics%29
http://en.wikipedia.org/wiki/Polynomial_interpolation
http://en.wikipedia.org/wiki/Newton_polynomial
http://en.wikipedia.org/wiki/Lagrange_polynomial
Googled it:
http://www.stat.wisc.edu/~xie/smooth_spline_tutorial.html
http://stat.ethz.ch/R-manual/R-patched/library/stats/html/smooth.spline.html
http://www.image.ucar.edu/GSP/Software/Fields/Help/splint.html
I never used R so I am not sure if that works or not, but if you have Matlab i can explain you more.

Resources