Greg Kawchuk
Greg Kawchuk

Reputation: 11

R - polynomial regression issue - model limited to finite number of output values

I'm trying to calculate point slopes from a series of x,y data. Because some of the x data repeats (...8, 12, 12, 16...) there will be a division by zero issue when using slope = (y2-y1/x2-x1).

My solution is to create a polynomial regression equation of the data, then plug a new set of x values (xx) into the equation that monotonically increase between the limits of x. This eliminates the problem of equal x data points. As a result, (x) and (xx) have the same limits, but (xx) is always longer in length.

The problem I am having is that the fitted values for xx are limited to the length of x. When I try to use the polynomial equation with (xx) that is 20 in length, the fitted yy results provide data for the first 10 points then gives NA for the next 10 points. What is wrong here?

    x <- c(1,2,2,5,8,12,12,16,17,20)
    y <- c(2,4,5,6,8,11,12,15,16,20)
    df <- data.frame(x,y)

    my_mod <- lm(y ~ poly(x,2,raw=T), data=df)     # This creates the polynomial equation 

    xx <- x[1]:x[length(x)] # Creates montonically increasing x using boundaries of original x
    yy <- fitted(my_mod)[order(xx)]

    plot(x,y)
    lines(xx,yy)

Upvotes: 0

Views: 106

Answers (1)

jpsmith
jpsmith

Reputation: 17185

If you look at

fitted(my_mod)

It outputs:

#       1         2         3         4         5         6         7         8         9        10 
#3.241032  3.846112  3.846112  5.831986  8.073808 11.461047 11.461047 15.303305 16.334967 19.600584 

Meaning the name of the output matches the position of x, not the value of x, so fitted(my_mod)[order(xx)] doesn't quite make sense.

You want to use predict here:

yy <- predict(my_mod, newdata = data.frame(x = xx))
plot(xx, yy)

#        1         2         3         4         5         6         7         8         9        10 
# 3.241032  3.846112  4.479631  5.141589  5.831986  6.550821  7.298095  8.073808  8.877959  9.710550 
#        11        12        13        14        15        16        17        18        19        20 
# 10.571579 11.461047 12.378953 13.325299 14.300083 15.303305 16.334967 17.395067 18.483606 19.600584

enter image description here

Upvotes: 1

Related Questions