1

I want to do a cubic regression on five data points: (1024, 1), (4096, 8), (16384, 16), (65536, 16), (262144, 48). I am not a regular R user, so I've done some research. First what I do is this (source):

df <- read.table(text="GRID Value 1024   1 4096   8 16384   16 65536  
16 262144   48", header=TRUE)

Then

plot(q, df,type='b',col='navy',main='Nonlinear relationship',lwd=3)

But the plot that I get is just a "connecting the dots" plot, not an actual cubic regression (please see below). I would appreciate some help on how to resolve this issue.

enter image description here

sequence
  • 744
  • 1
  • 8
  • 16

1 Answers1

3
df <- read.table(text="
GRID Value
1024   1
4096   8
16384  16
65536  16
262144  48", header=TRUE)

mod=lm(Value~poly(GRID,3),data=df)

plot(df$Value~df$GRID)
lines(predict(mod,data.frame(GRID=seq(0,max(df$GRID),1)))~seq(0,max(df$GRID),1))

Here is an example how to estimate a cubic function from your data, notice the poor fit.

user2974951
  • 9,535
  • 1
  • 17
  • 24
  • 1
    Much better to do this: curve(predict(mod, newdata = data.frame(GRID = x)), add = TRUE) – Roland Apr 17 '20 at 07:47
  • When I try cubic regression with the same values at https://planetcalc.com/5992/ I get a very good fit. Why is this discrepancy between R and that link? – sequence Apr 17 '20 at 20:21
  • @sequence I don't know how that tool works so I can't answer that. Are you sure the fit is good? And that it is a cubic function that you are seeing? – user2974951 Apr 18 '20 at 08:17
  • @user2974951 The picture that is given by that service gives a pretty good fit. This function should not go into the negative territory as it does with R. – sequence Apr 20 '20 at 00:27