且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在具有R的面板中插值高于最大值的数据

更新时间:2023-02-26 18:44:14

使用lm,您可以获取此简单插值所使用的斜率,然后使用该斜率通过predict生成新值.但是也许有一个更简单的解决方案

With lm you can get the slope that this simple interpolation is using, and then use that slope to generate new values with predict. But maybe there's a simpler solution

mod <- lm(index ~ year, a)

a[,2] <- predict(mod, newdata=data.frame(year=a$year))

编辑1

否,对于每个id,我们将运行不同的lm.为此,我们在循环内选择a中具有唯一id的部分,然后仅对该部分运行lm:

EDIT 1

No, for each id we will run a different lm. To do that we select the part of a with a unique id inside a loop, and run the lm only with that part:

for(i in unique(a$id)){
  ai = a[a$id==i,]
  mod = lm(index ~ year, ai)
  a[a$id==i,3] = predict(mod, newdata=data.frame(year=ai$year))}