0

I have a graph that was smoothed using Scatter.Smooth function. I need to get the co-ordinates given the X-axis being the date.

The smoothed curve is generated in Knime R Node. I want the points so as to use it in a Line Plot node.

Is there any other method to get the values from the generated graph to line plot in Knime?

Update

I have added the R code that I used to generate the smooth curve in R node

 plot(x,y)
 scatter.smooth(x,y)

 //x<- Date
 //y <- Frequency
 //Basically the values are from the Data file in another node. For simplicity I have mentioned it as comments
Vini
  • 1,978
  • 8
  • 40
  • 82
  • can you provide an example of that smoothed curve code? – cory Mar 02 '16 at 14:22
  • I could add the code that I used for generating the smoothed curve. Check the Update – Vini Mar 02 '16 at 14:24
  • If you want to use the actual results from a smooth you're probably better off using a function such as locfit or a spline to generate the data. This will give you a lot more control to ensure you're developing a model that fits your data well and minimizes bias. The scatter.smooth function implements a smoothing function behind the scenes but it gives you minimal control on how the function is implemented. You can then plot the results from the the smooths on top of your scatter if you'd like. – admccurdy Mar 02 '16 at 14:37
  • I tried Spline, but for some reason i am not getting it the right way.. :( – Vini Mar 02 '16 at 15:32

2 Answers2

0

This is the function definition for R's scatter.smooth:

function (x, y = NULL, span = 2/3, degree = 1, family = c("symmetric", 
    "gaussian"), xlab = NULL, ylab = NULL, ylim = range(y, pred$y, 
    na.rm = TRUE), evaluation = 50, ..., lpars = list()) 
{
    xlabel <- if (!missing(x)) 
        deparse(substitute(x))
    ylabel <- if (!missing(y)) 
        deparse(substitute(y))
    xy <- xy.coords(x, y, xlabel, ylabel)
    x <- xy$x
    y <- xy$y
    xlab <- if (is.null(xlab)) 
        xy$xlab
    else xlab
    ylab <- if (is.null(ylab)) 
        xy$ylab
    else ylab
    pred <- loess.smooth(x, y, span, degree, family, evaluation)
    plot(x, y, ylim = ylim, xlab = xlab, ylab = ylab, ...)
    do.call(lines, c(list(pred), lpars))
    invisible()
}

It would seem that

pred <- loess.smooth(x, y, span, degree, family, evaluation)

will contain what you need.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • I suppose after the execution of `pred <- loess.smooth(x, y, span, degree, family, evaluation)` , pred will contain all the values for x and y? because after the execution of the line there are no plots generated. – Vini Mar 02 '16 at 15:24
  • When i execute the command in R studio, I get the values for x and y, but not all values of x and y are obtained. Out of 5000 input points only 50 values are obtained in `pred`. how do i use this in R node in Knime – Vini Mar 02 '16 at 15:26
0

Using either scatter.smooth() or loess.smooth() to get points from a regression line is the wrong way to go about it. Loess.smooth() is only providing enough points to plot a line it's not meant for predicting values. You want to create an equation and then use that to predict on your original x points or a new set of points. There are tons of ways to do this loess.smooth (and thus scatter.smooth) both implement a local polynomial regression. To actually implement the regression and get an equation you might do something like this:

library(locfit)
x <- rnorm(50, 20, 2)
y <- rnorm(50, x, 1)
myLoc <- locfit(x~y)
predPoints <- predict(myLoc, x)

If you'd like we can compare this to the results from loess.smooth():

mySmooth <- loess.smooth(x,y)
plot(x,y)
points(x, predPoints, col = 'red')
points(mySmooth$x, mySmooth$y, col = 'blue')

You'll notice the two methods produce slightly different results. Neither is better or worse it is very dependent on the nature of your data and what your goals are. There a number of ways to evaluate regressions to try and assess both their accuracy and their validity.

admccurdy
  • 694
  • 3
  • 11
  • I already have the predicted values from the input data. I generated a smoothed the predicted curve. now i am trying to get the y values for the smoothed curve to the corresponding x-values so that it could be used for further manipulations – Vini Mar 02 '16 at 19:21
  • How did you do the prediction? – admccurdy Mar 02 '16 at 19:46
  • Using moving average. And some further calculations. – Vini Mar 02 '16 at 19:50
  • So you're predicting based on a moving average and then trying to smooth your predictions? Without posting more of your data and what you're trying to do I don't think it's possible to offer any more help. I will say that using the `scatter.smooth` you described above is making an additional prediction based on a local polynomial regression as I described in my answer. I would encourage you to make sure you understand what is happening at each step and what uncertainty your calculations are introducing. – admccurdy Mar 02 '16 at 20:21
  • Also unless you're adding noise to your predictions (such as in an ARIMA time series) they should be smooth to begin with. – admccurdy Mar 02 '16 at 20:22
  • Yes. I am trying to smooth my predictions so that i get a decent curve as my predicted alarm curve. I will post more about the data and what i am trying to achieve. – Vini Mar 02 '16 at 20:43