0
$\begingroup$

I am looking into making a regression of a bunch of data that is contained on some range of real numbers. In my case, x is between 0 and 1 and y is between 0 and 10. If I have 150 data points on this plane, and want to best model the original data with N data points (not polynomial, just points), how can I do this? How can I then analyse how good the fit is? Any help is appreciated. Sorry if my notation is not perfect, I am from a physics background so please bear with me. Cheers.

Data example:

x.vector <- cumsum(rnorm(150)^2) y.vector <- 10*rnorm(150)^2

I want to fit 2 points, 3, 4, ... and check fit at each one.

$\endgroup$
6
  • $\begingroup$ what do you mean by "model the data with points"? $\endgroup$
    – Glen_b
    Commented Aug 28, 2014 at 0:58
  • $\begingroup$ Good question. If I wish to sample something with ~1000 points (the 150 is just for this example, doesnt matter as long as points >> output model points) and best represent it with points that can be linearly interpolated and that represent the underlying process. Intuitively/visually , I think this means minimizing some sort of error function, like square of difference from model point, but with few model points and many points in actual data, I am not sure what is best way to represent. $\endgroup$
    – Vaishak Id
    Commented Aug 28, 2014 at 1:23
  • $\begingroup$ To be clear however, I would like to select (X,Y) pairs that best 'ftt' the data, meaning they do not have to be spaced in anyway but can be anywhere on the plane of 0 to 1 and 0 to 10. $\endgroup$
    – Vaishak Id
    Commented Aug 28, 2014 at 2:40
  • 2
    $\begingroup$ So essentially you want to fit a piecewise linear function to the data? I.e., have intervals $(x_0,x_1), (x_1,x_2), \ldots, (x_{N-1},x_N)$ such that it is linear in each interval but the slope changes at each boundary? $\endgroup$
    – Russ Lenth
    Commented Aug 28, 2014 at 2:43
  • $\begingroup$ Yes, that is correct (along with assessing some metric of 'error' produced by this fit -- presumably one that is statistically sound/supported by common practice!). $\endgroup$
    – Vaishak Id
    Commented Aug 28, 2014 at 3:44

1 Answer 1

2
$\begingroup$

The easiest way to do this in R is to use the bs (B-spline) function. Specifying a degree of 1 makes it piecewise linear. Here's an example, using different data since in your example, there's no dependence between x and y.

Generate some reproducible data

> set.seed(1234)
> x <- runif(150, -2, 3)
> y <- 7 * pnorm(x, 1, 0.7)^2 + rnorm(150, 0, .15)

Set up points where you want the slope to change

> knots <- c(-1,0,1,2)  # interior ones only

Fit a model and plot the fitted values

> piecel.lm <- lm(y ~ bs(x, degree = 1, knots = knots, Boundary.knots = c(-2,3)))
> plot(x, predict(piecel.lm))

piecewise-linear predictions

(This shows that it is indeed piecewise linear. But to get it in more useful form, just figure out the abscissae and ordinates at the knots (including the end ones):

> absc <- (-2):3
> ord <- predict(piecel.lm, newdata = data.frame(x = absc))
> plot(y ~ x)
> lines(ord ~ absc, col = "red")
> points(ord ~ absc, col = "red")

data and piecewise-linear fit

Additional comment:

If your goal is simply to have something that'll fit the data better than a polynomial, I'd suggest a natural cubic spline, which you can get in much the same was using the ns function. You can evaluate it at any $x$ value using the predict function with newdata. Here is the natural spline fit to these data, using the same knots:

> spline.lm <- lm(y ~ ns(x, knots = knots, Boundary.knots = c(-2,3)))
> plot(y ~ x)
> xnew <- seq(-2, 3, by = .2)
> lines(xnew, predict(spline.lm, newdata = data.frame(x = xnew)), col = "red")

data and spline fit

$\endgroup$
2
  • $\begingroup$ PS it is just a regression model, so you get the usual fit statistics - residuals, $MS_E$, $R^2$, etc. $\endgroup$
    – Russ Lenth
    Commented Aug 29, 2014 at 0:55
  • $\begingroup$ @VaishakId -- I spent some time preparing an answer. Is it useful to you? Any comments? $\endgroup$
    – Russ Lenth
    Commented Aug 29, 2014 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.