在R中绘制三次回归

问题描述 投票:0回答:1

我想对五个数据点进行三次回归:(1024,1),(4096,8),(16384,16),(65536,16),(262144,48)。我不是普通的R用户,因此我做了一些研究。首先,我要做的是这个(source):

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

然后

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

但是我得到的图只是一个“连接点”图,而不是实际的三次回归(请参见下文)。我将为您提供一些有关如何解决此问题的帮助。

enter image description here

r plot non-linear-regression
1个回答
1
投票
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))

这是一个示例,该示例如何根据您的数据估算三次函数,请注意拟合度较差。

© www.soinside.com 2019 - 2024. All rights reserved.