Can't plot smooth spline in R - r

I am trying to use a smoothing spline on my dataset. I use smooth.spline function. And want to plot my fit next. However, for some reason it won't plot my model. It doesn't even give any error. I only get a error message after running smooth.spline function that 'cross-validation with non-unique 'x' values seems doubtful'. But I don't think it shouldn't make too much of a difference to the practical result.
My code is:
library('splines')
fit_spline <- smooth.spline(data.train$age,data.train$effect,cv = TRUE)
plot(data$effect,data$age,col="grey")
lines(fit_spline,lwd=2,col="purple")
legend("topright",("Smoothing Splines with 5.048163 df selected by CV"),col="purple",lwd=2)
What I get is:
Can someone tell me what I am doing wrong here?

Two issues:
Number 1. If you do smooth.spline(x, y), plot your data with plot(x, y) not plot(y, x).
Number 2. Don’t pass in data.train for fitting then a different dataset data for plotting. If you want to see how the spline looks like at new data points, use predict.smooth.spline first. See ?predict.smooth.spline.

Related

Plotting smoothspline [duplicate]

I am trying to use a smoothing spline on my dataset. I use smooth.spline function. And want to plot my fit next. However, for some reason it won't plot my model. It doesn't even give any error. I only get a error message after running smooth.spline function that 'cross-validation with non-unique 'x' values seems doubtful'. But I don't think it shouldn't make too much of a difference to the practical result.
My code is:
library('splines')
fit_spline <- smooth.spline(data.train$age,data.train$effect,cv = TRUE)
plot(data$effect,data$age,col="grey")
lines(fit_spline,lwd=2,col="purple")
legend("topright",("Smoothing Splines with 5.048163 df selected by CV"),col="purple",lwd=2)
What I get is:
Can someone tell me what I am doing wrong here?
Two issues:
Number 1. If you do smooth.spline(x, y), plot your data with plot(x, y) not plot(y, x).
Number 2. Don’t pass in data.train for fitting then a different dataset data for plotting. If you want to see how the spline looks like at new data points, use predict.smooth.spline first. See ?predict.smooth.spline.

Subtracting a fitted polynomial from a dataset in R

I have one curve, a scatterplot, which is the plot of the data set I am working with (named 'mydata') and the other curve which is the fitted 2nd degree polynomial curve that I obtained from the data set.
The scatterplot was obtained with a simple plot function:
plot(mydata)
The code I used for the fitting is:
fit<-lm(mydata$Volts ~ poly(mydata$Frequency, 2, raw=TRUE),data=mydata)
#summary(fit)
lines(mydata$Frequency, predict(fit))
Now, I would like to subtract the fitted polynomial from the dataset. Following was my approach:
given<-plot(mydata)
fit<-lm(mydata$Volts ~ poly(mydata$Frequency, 2, raw=TRUE),data=mydata)
new<-lines(mydata$Frequency, predict(fit))
corrected<-given-new
plot(corrected)
The error I received was:
Error in plot(corrected) : object 'corrected' not found
How do I correct this?
Looks like you are trying to subtract graphical elements. You should perform any math/operations on your data before trying to plot it. Something like the following may work. However without sample data this is just an educated guess.
given <- mydata$Volts
fit <- lm(mydata$Volts ~ poly(mydata$Frequency, 2, raw=TRUE),data=mydata)
new <- predict(fit)
corrected <- given-new
plot(mydata$Frequency, corrected)
I ran a reprex (although technically, I need a random seed for a true reprex, but because of the actual issue with the code, that doesn't matter here) on nonsense data.
volts=rnorm(50,mean=220,sd=5)
frequency=runif(50,min=30,max=90)
mydata=data.frame(Volts=volts,Frequency=frequency)
given<-plot(mydata)
fit<-lm(mydata$Volts ~ poly(mydata$Frequency, 2, raw=TRUE),data=mydata)
new<-lines(mydata$Frequency, predict(fit))
corrected<-given-new
plot(corrected)
The scope of my answer is strictly to explain why the not found error showed up. Daniel's code shows you the fix.
I'm not sure why the response of Daniel O was not chosen, because it worked. I know it is frustrating when you clearly defined something and your source code is right in front of you, yet the interpreter says NOT FOUND. The lesson learned here when you get this situation, to check for NULL. It's a good habit in general for R.

Messy graph when plotting fitted values from flexmix

I am trying to plot 3 regression lines for 3 components in the data estimated via flexmix package.
However, when I try to plot predicted values for the first component, the result is a messy graph with lines connecting to each other.
These are my codes:
m_1 <- flexmix(x ~ y + z, data=set2, cluster=clstr)
yhat <-fitted(m_1)
plot(x, y, options=...)
lines(x, yhat[,1], options=...)
Online I found some hints about > order() with no result
reorder <- order(yhat[,1])
lines(x[reorder], yhat[,1][reorder], options=...)
It results in a continuous line that looks like a time series with high volatility.
The other two components are working fine. Any idea on how to solve this?
The solution is here I think :
http://pages.mtu.edu/~shanem/psy5220/daily/Day19/Mixture_of_regressions.html

exponential regression with R ( and negative values)

I am trying to fit a curve to a set of data points but did not succeed. So I ask you.
plot(time,val) # look at data
exponential.model <- lm(log(val)~ a) # compute model
fit <- exp(predict(exponential.model,list(Time=time))) # create the fitted curve
plot(time,val)#plot it again
lines(time, fit,lwd=2) # show the fitted line
My only problem is, that my data contains negative values and so log(val) produces a lot of NA making the model computation crash.
I know that my data does not necessarily look like exponential , but I want to see the fit anyway. I also used another program which shows me val=27.1331*exp(-time/2.88031) is a nice fit but I do not know, what I am doing wrong.
I want to compute it with R.
I had the idea to shift data so no negative values remain, but result is poor and quite sure wrong.
plot(time,val+20) # look at data
exponential.model <- lm(log(val+20)~ a) # compute model
fit <- exp(predict(exponential.model,list(Time=time))) # create the fitted curve
plot(time,val)#plot it again
lines(time, fit-20,lwd=2) # show the (BAD) fitted line
Thank you!
I figured some things out and have a satisfying solution.
exponential.model <- lm(log(val)~ a) # compute model
The log(val) term is trying to rescale the values, so a linear model can be applied. Since this not possible to my values, you have to use a non-linear model (nls).
exponential.model <- nls(val ~ a*exp(b*time), start=c(b=-0.1,h=30))
This worked fine for me.
satisfying fit

analytical derivative of splinefun()

I'm trying to fit a natural cubit spline to probabilistic data (probabilities that a random variable is smaller than certain values) to obtain a cumulative distribution function, which works well enough using splinefun():
cutoffs <- c(-90,-60,-30,0,30,60,90,120)
probs <- c(0,0,0.05,0.25,0.5,0.75,0.9,1)
CDF.spline <- splinefun(cutoffs,probs, method="natural")
plot(cutoffs,probs)
curve(CDF.spline(x), add=TRUE, col=2, n=1001)
I would then, however, like to use the density function, i.e. the derivative of the spline, to perform various calculations (e.g. to obtain the expected value of the random variable).
Is there any way of obtaining this derivative as a function rather than just evaluated at a discrete number of points via splinefun(x, deriv=1)?
This is pretty close to what I'm looking for, but alas the example doesn't seem to work in R version 2.15.0.
Barring an analytical solution, what's the cleanest numerical way of going about this?
If you change the environment assignment line for g in the code the Berwin Turlach provided on R-help to this:
environment(g) <- environment(f)
... you succeed in R 2.15.1.

Resources