数据线性插值 [英] Linear interpolation of data

查看:50
本文介绍了数据线性插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有年份(x)和相关百分比(y)的数据框

I have a data frame with the year (x) and an associated percentage (y)

data.frame(x = c(1997,2000,2003,2006,2009,2010,2013,2014),
           y = c(.02,.023,.025,.024,.026,.027,.029,.031)

这是此数据框的折线图:

Here is a line chart of this data frame :

我想对数据进行插值,以基于线性回归获得缺失年份的百分比.

I would like to interpolate my data to get the percentage of missing years based on a linear regression.

我可以为每条曲线建立线性模型,但这会很乏味.

I could make a linear model of each piece of curve but it would be tedious.

有没有一种简单的方法可以用R做到这一点?

Is there a simple way to do it with R?

输入:

df = data.frame(
  year=c(1997,2000,2003,2006,2009,2010,2013,2014),
  percent=c(0.020, 0.023, 0.025, 0.024, 0.026, 0.027, 0.029, 0.031)
)

输出(对于功能f):

f(2006)==0.024
f(2007)==0.024.666
f(2008)==0.025.333
f(2009)==0.026

推荐答案

一种方法是对 zoo 使用线性 inter 定位:

One way is to use linear inter polation with zoo:

library(tidyr)
library(zoo)

df_complete <- complete(df, year = full_seq(year, 1))
df_complete$percent <- na.approx(df_complete$percent)


plot(df_complete)

这篇关于数据线性插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆