反转rollsum函数

mpbci0fu  于 2023-05-26  发布在  其他
关注(0)|答案(2)|浏览(170)

sum_last_3包含使用函数zoo::rollsum的列x的最后3个值的和。
有没有办法只使用变量sum_last_3来重新创建变量x

library(dplyr)
library(zoo)

df <- data.frame(x = c(2,3,5,6,8,1,0,2,3,5,1,9,5,3))
df$sum_last_3 <- rollsum(df$x, k = 3, fill = NA, align = "right")
a64a0gku

a64a0gku1#

运算不可逆;然而,如果不是用NA填充结果的前两个元素,我们使用partial=TRUE来获得部分和,那么它就是。下面的rollapplyr行与问题中的rollsum相同,除了我们对前两个元素使用部分和。M是该运算的矩阵表示,我们通过对单位矩阵进行运算而获得。最后,我们反转M并将其乘以df$sum_last_3以恢复原始输入。

library(zoo)

df <- data.frame(x = c(2,3,5,6,8,1,0,2,3,5,1,9,5,3))
df$sum_last_3 <- rollapplyr(df$x, 3, sum, partial = TRUE) 

M <- rollapplyr(diag(nrow(df)), 3, sum, partial = TRUE)
solve(M, df$sum_last_3)
## [1] 2 3 5 6 8 1 0 2 3 5 1 9 5 3
dba5bblo

dba5bblo2#

长话短说,在给定sum_last_3的情况下,您不会得到完全相同的x的解,您可以从矩阵的Angular 来查看它。
为了简化,xy分别是输入和输出

x <- df$x
y <- na.omit(df$sum_last_3)

并且对于rollsum过程存在矩阵表示m,使得y = m %*% x,其中m可以写为

> m <- matrix(0, length(x) - 2, length(x))
> m <- replace(m, col(m) - row(m) >= 0 & col(m) - row(m) <= 2, 1)

> m
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
 [1,]    1    1    1    0    0    0    0    0    0     0     0     0     0
 [2,]    0    1    1    1    0    0    0    0    0     0     0     0     0
 [3,]    0    0    1    1    1    0    0    0    0     0     0     0     0
 [4,]    0    0    0    1    1    1    0    0    0     0     0     0     0
 [5,]    0    0    0    0    1    1    1    0    0     0     0     0     0
 [6,]    0    0    0    0    0    1    1    1    0     0     0     0     0
 [7,]    0    0    0    0    0    0    1    1    1     0     0     0     0
 [8,]    0    0    0    0    0    0    0    1    1     1     0     0     0
 [9,]    0    0    0    0    0    0    0    0    1     1     1     0     0
[10,]    0    0    0    0    0    0    0    0    0     1     1     1     0
[11,]    0    0    0    0    0    0    0    0    0     0     1     1     1
[12,]    0    0    0    0    0    0    0    0    0     0     0     1     1
      [,14]
 [1,]     0
 [2,]     0
 [3,]     0
 [4,]     0
 [5,]     0
 [6,]     0
 [7,]     0
 [8,]     0
 [9,]     0
[10,]     0
[11,]     0
[12,]     1

我们可以检查一下

> m %*% x - y
      [,1]
 [1,]    0
 [2,]    0
 [3,]    0
 [4,]    0
 [5,]    0
 [6,]    0
 [7,]    0
 [8,]    0
 [9,]    0
[10,]    0
[11,]    0
[12,]    0
attr(,"na.action")
[1] 1 2
attr(,"class")
[1] "omit"

然而,我们应该知道m是一个奇异矩阵,因此结果y的长度比x短。对于这个线性系统,我们可以用MASS::ginv来求解xhat,但这与x并不完全相同

> (xhat <- MASS::ginv(m) %*% y)
             [,1]
 [1,]  1.93846154
 [2,]  3.13846154
 [3,]  4.92307692
 [4,]  5.93846154
 [5,]  8.13846154
 [6,]  0.92307692
 [7,] -0.06153846
 [8,]  2.13846154
 [9,]  2.92307692
[10,]  4.93846154
[11,]  1.13846154
[12,]  8.92307692
[13,]  4.93846154
[14,]  3.13846154

相关问题