【问题标题】:Creating an expanded matrix with interpolated data in R在 R 中使用插值数据创建扩展矩阵
【发布时间】:2015-01-10 22:58:09
【问题描述】:

我有一个数据框,其中包含 MSA 的年度人口数据。它们的组织如下:

 MSA    FIPS  x1969 x1970 x1971 .... x2012
Akron   123    12    14    17  ....   22
Miami   234    23    20    24  ....   29
etc.

我需要将数据重塑成

 MSA FIPS Year Data
 Akron 123 1969 12
 Akron 123 1970 14
 Akron 123 1971 17

...

我可以使用“melt”来做到这一点,但我还想插入这些年度数据以包含完整时间序列的季度数据点。那么,如何最好地动态创建季度(插值)矩阵?

我可以在上面第一个矩阵的行上使用循环来执行此操作,然后使用melt 来重塑新数据,但是每当我发现自己在构建明确编码的循环时,我都被要求打自己一巴掌。

我一直在修改“应用”,但它会创建一个列表列表——然后需要组装最终的数据框。

我觉得必须有一个简单的解决方案。

谢谢,克里斯。

【问题讨论】:

  • 您有一个包含年度数据的数据集还是两个包含季度数据的数据集?
  • 一个像上面的例子一样的年度数据集。我想插入从 1969 年到 1970 年以及从 1970 年到 1972 年等缺失的三个季度。当前数据是 n (msa) x m (years);我希望它是 n(msa)x q(四分之一)。如何创建新的矩阵或数据框?我可以在创建宿舍之前或之后融化/重塑。谢谢!
  • 我可能误解了你的问题。从您显示的数据来看,您似乎想将年度disaggregate 转为quarterly。在这种情况下,像tempdisagg 这样的专用包会更好。正如我在帖子中提到的,有一些选择。
  • 知道了。再次,谢谢。有时选择是压倒性的。这是一种词汇丰富的新语言。

标签: r matrix interpolation apply


【解决方案1】:

这建立在前面的@akrun 之上:

#His data frame build:

df <- structure(list(MSA = c("Akron", "Miami"), FIPS = c(123L, 234L),
      x1969 = c(12L, 23L), x1970 = c(14L, 20L), x1971 = c(17L, 24L)),
     .Names = c("MSA", "FIPS", "x1969", "x1970", "x1971"), class = "data.frame",
     row.names = c(NA, -2L))

#His set up:

dM <- transform(melt(df, id.var=c('MSA', 'FIPS')),
        variable=as.numeric(gsub('^x', '', variable)))

#My variation on his lapply:

res <- lapply(split(dM, dM$MSA), function(x) {
       xseq=seq(min(x$variable),max(x$variable),by=.25)
       val <- approx(x$variable,x$value,xout=xseq)
       data.frame(yearQtr=xseq,val=val$y)})

df.new <- do.call(rbind.data.frame,res)

这不是很完美,但我稍后会回来。我们很接近。谢谢@akrun

【讨论】:

    【解决方案2】:

    也许你可以从tempdisagg尝试td

    library(tempdisagg)
    library(reshape2)
    library(zoo)
    
    dM <- transform(melt(df, id.var=c('MSA', 'FIPS')), 
                variable=as.numeric(gsub('^x', '', variable)))
    
    res <- lapply(split(dM, dM$MSA), function(x) {
               val <- ts(x$value, start=x$variable[1], end=x$variable[nrow(x)])
               val2 <-predict(td(val~1, to='quarterly', method='uniform')) 
              #change the options as needed
              data.frame(yearQtr= as.yearqtr(time(val2)), val=val2)})
    

    数据

    df <- structure(list(MSA = c("Akron", "Miami"), FIPS = c(123L, 234L
     ), x1969 = c(12L, 23L), x1970 = c(14L, 20L), x1971 = c(17L, 24L
    )), .Names = c("MSA", "FIPS", "x1969", "x1970", "x1971"), class = "data.frame",
    row.names = c(NA, -2L))
    

    【讨论】:

    • 谢谢@akrun!我了解了 gsub,关于拆分,现在正在修补 ts。我已经和 R 度过了愉快的一天,谢谢。但是,输出如下(原始数据点之间没有插值,它们似乎关闭(从原始数据中的 12、14 和 17 到下面的输出。再次感谢您。我会继续努力) 。Snip这里 - >> res $ akron iverqtr Val 1 1969 Q1 3.00 2 1969 Q2 3.00 3 1969 Q3 3.00 4 1969 Q 3 3.00 5 1970 Q1 3.50 6 1970 Q1 3.50 6 1970 Q2 3.50 7 1970 Q3 3.50 8 1970 Q4 3.50 9 1971 Q1 4.25 10 1971 Q2 4.25 11 1971 Q3 4.25 12 1971 Q4 4.25
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多