首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不使用Hmisc在R中外推

不使用Hmisc在R中外推
EN

Stack Overflow用户
提问于 2020-09-14 18:53:25
回答 1查看 123关注 0票数 0

对前一个问题的回答是:由于R版本,extrapolate in R for a time-series data对我不起作用。

我有一个与以下类似的dataframe NEI_othertier1_long

代码语言:javascript
运行
复制
state    pollutant    Sector       Fuel      description    year     value
AK       Ammonia      Refining     Diesel     industrial    2008      1.18
AK       Ammonia      Refining     Diesel     industrial    2009      NA
AK       Ammonia      Refining     Diesel     industrial    2010      NA
AK       Ammonia      Refining     Diesel     industrial    2011      5.76
AK       Ammonia      Refining     Diesel     industrial    2012      NA
AK       Ammonia      Refining     Diesel     industrial    2013      NA
AK       Ammonia      Refining     Diesel     industrial    2014      5.83
AK       Ammonia      Refining     Diesel     industrial    2015      NA
AK       Ammonia      Refining     Diesel     industrial    2016      NA
AK       Ammonia      Refining     Diesel     industrial    2017      8.96
AK       Ammonia      Refining     Diesel     industrial    2018      NA
AK       Ammonia      Refining     Diesel     industrial    2019      NA

我有2008、2011、2014和2017年的价值。我成功地使用以下代码对2009-2016年进行了线性插值:

代码语言:javascript
运行
复制
    NEI_othertier1_long %>%
  dplyr::mutate( value = na.approx(value, na.rm = FALSE, rule = 2) ) -> NEI_othertier1_interpolated

但插补值将延续2018年和2019年的2017年数值。我想线性推断2018年和2019年的数值。

我有RVersion3.5.2(不能更新),所以不能安装latticeExtraHmisc依赖它来使用approxExtrap函数。

任何和所有的帮助都是感激的!

dput(头(NEI_othertier1_long))结构(状态为c("AK","AK"),污染物=c(“氨”,“氨”),“氨”、“氨”、“氨”),CEDS_Sector =c(“1A1b_宠物精炼”、“1A1b_宠物精炼”),CEDS_Fuel = c( "diesel_oil“,”diesel_oil“),tier1_description =c(”燃料梳“)。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。工业“,”燃料梳。),单位=c(“吨”,“吨”),年份=2008年:2019年,排放量= c(1.18,NA,NA,5.76,NA,NA,5.83,NA,NA,8.96,NA,NA),row.names = c(NA,-12 L),类= c("grouped_df“、"tbl_df”、"tbl“、"data.frame")、组=结构(list= "AK”、污染物=“氨”、CEDS_Sector =“1A1b_Pet-精馏”、CEDS_Fuel = "diesel_oil“、tier1_description =”燃料梳“。.rows =list(1:12),row.names = c(NA,-1L),class = c("tbl_df","tbl","data.frame"),.drop =真)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-14 19:37:22

approxExtrap只是approx的包装器,所以您可以复制函数定义并使用它。

代码语言:javascript
运行
复制
NEI_othertier1_long %>% dplyr::mutate(x = approxExtrap(year, value, year, na.rm = TRUE)$y)

如果您找不到它,这里是approxExtrap

代码语言:javascript
运行
复制
approxExtrap <- function (x, y, xout, method = "linear", n = 50, rule = 2, f = 0, 
  ties = "ordered", na.rm = FALSE) 
{
  if (is.list(x)) {
    y <- x[[2]]
    x <- x[[1]]
  }
  if (na.rm) {
    d <- !is.na(x + y)
    x <- x[d]
    y <- y[d]
  }
  d <- !duplicated(x)
  x <- x[d]
  y <- y[d]
  d <- order(x)
  x <- x[d]
  y <- y[d]
  w <- approx(x, y, xout = xout, method = method, n = n, rule = 2, 
    f = f, ties = ties)$y
  r <- range(x)
  d <- xout < r[1]
  if (any(is.na(d))) 
    stop("NAs not allowed in xout")
  if (any(d)) 
    w[d] <- (y[2] - y[1])/(x[2] - x[1]) * (xout[d] - x[1]) + 
    y[1]
  d <- xout > r[2]
  n <- length(y)
  if (any(d)) 
    w[d] <- (y[n] - y[n - 1])/(x[n] - x[n - 1]) * (xout[d] - 
        x[n - 1]) + y[n - 1]
  list(x = xout, y = w)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63890300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档