更新时间: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))
否,对于每个id
,我们将运行不同的lm.为此,我们在循环内选择a
中具有唯一id
的部分,然后仅对该部分运行lm:
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))}