user162381
user162381

Reputation: 101

Drawing a line through given points (spline) with R

I am given x = c(50, 37, 25, 0) and y = c(30, 52, 65, 70). These are vectors containing the x and y coordinates of four points. I need to draw a smooth curve through all four points, and get the actual equation for this function.

How do I accomplish this with R?

I was reading the documentation for splines but given my lack of familiarity with the mathematics behind this, I got pretty confused.

Upvotes: 0

Views: 629

Answers (1)

Mateusz1981
Mateusz1981

Reputation: 1867

x = c(50, 37, 25, 0) 
y = c(30, 52, 65, 70)
plot(x, y)

You have to do a third degree polynom and than add a curve to the plot

  exe <- lm(y ~ I(x^3) + I(x^2) + x)
    curve(predict(exe, newdata = data.frame(x = x)), 0, 50, add = T)

Upvotes: 1

Related Questions