简体   繁体   中英

How to convert graph to data points in R

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM