R中带扩展窗口的滚动回归

R中带扩展窗口的滚动回归,r,regression,rolling-computation,R,Regression,Rolling Computation,我想做一个滚动线性回归,通过扩展窗口,在数据框中的两个变量之间,通过第三个分类列进行分组 例如,在下面的玩具数据框中,我想使用所有行提取由z分组的lm(y~x)的系数,直到感兴趣的行。因此,对于第2行,回归的数据集将是第1:2行,第3行将是第1:3行,第4行将只是第4行,因为它是分类变量z=b的第一行 dframe<-data.frame(x=c(1:10),y=c(8:17), z=c("a","a","a","b","b","b","b","b","b","b")) dframe这

我想做一个滚动线性回归,通过扩展窗口,在数据框中的两个变量之间,通过第三个分类列进行分组

例如,在下面的玩具数据框中,我想使用所有行提取由z分组的lm(y~x)的系数,直到感兴趣的行。因此,对于第2行,回归的数据集将是第1:2行,第3行将是第1:3行,第4行将只是第4行,因为它是分类变量z=b的第一行

dframe<-data.frame(x=c(1:10),y=c(8:17), z=c("a","a","a","b","b","b","b","b","b","b"))


dframe这里有一种方法可以对您所询问的数据帧进行滚动遍历:

sapply(2:nrow(dframe), function(crt.row) {
    df = dframe[1:crt.row,]
    ## compute the statistics of interest on df (e.g. run the linear model),
    ## which is the subset of the original data frame that consists of rows 1 to
    ## current
    ##
    ## for example mean of x+y
    c(crt.row=crt.row, mystat=mean(df$x + df$y))
})

        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
crt.row    2    3    4    5    6    7    8    9   10
mystat    10   11   12   13   14   15   16   17   18

下面是一种方法,可以对您询问的数据帧进行滚动遍历:

sapply(2:nrow(dframe), function(crt.row) {
    df = dframe[1:crt.row,]
    ## compute the statistics of interest on df (e.g. run the linear model),
    ## which is the subset of the original data frame that consists of rows 1 to
    ## current
    ##
    ## for example mean of x+y
    c(crt.row=crt.row, mystat=mean(df$x + df$y))
})

        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
crt.row    2    3    4    5    6    7    8    9   10
mystat    10   11   12   13   14   15   16   17   18
1)rollapplyr首先拆分数据帧,然后在拆分的每个组件上运行
rollapplyr
。请注意,
rollappyr
可以将宽度向量作为第二个参数

library(zoo)

roll <- function(data, n = nrow(data)) {
  rollapplyr(1:n, 1:n, function(ix) coef(lm(y ~ x+0, data, subset = ix))[[1]])
}

L <- split(dframe[-3], dframe[[3]])
transform(dframe, roll = unlist(lapply(L, roll)))
1a)一种变体是使用
ave
而不是
split

n <- nrow(dframe)
transform(dframe, roll = ave(1:n, z, FUN = function(ix) roll(dframe[ix, ]))
给予:

    x  y z     roll
a1  1  8 a 8.000000
a2  2  9 a 5.200000
a3  3 10 a 4.000000
b1  4 11 b 2.750000
b2  5 12 b 2.536585
b3  6 13 b 2.363636
b4  7 14 b 2.222222
b5  8 15 b 2.105263
b6  9 16 b 2.007380
b7 10 17 b 1.924528
# A tibble: 10 x 4
# Groups:   z [2]
       x     y z      roll
   <int> <int> <fct> <dbl>
 1     1     8 a      8   
 2     2     9 a      5.20
 3     3    10 a      4.00
 4     4    11 b      2.75
 5     5    12 b      2.54
 6     6    13 b      2.36
 7     7    14 b      2.22
 8     8    15 b      2.11
 9     9    16 b      2.01
10    10    17 b      1.92
3a)
roll
在(1)中可以替换为
roll2
在以下情况下,它不使用包,甚至不使用
lm
为我们提供另一个基本的R解决方案。同样,
L
来自(1)

roll21)rollappyr首先拆分数据帧,然后在拆分的每个组件上运行
rollappyr
。请注意,
rollappyr
可以将宽度向量作为第二个参数

library(zoo)

roll <- function(data, n = nrow(data)) {
  rollapplyr(1:n, 1:n, function(ix) coef(lm(y ~ x+0, data, subset = ix))[[1]])
}

L <- split(dframe[-3], dframe[[3]])
transform(dframe, roll = unlist(lapply(L, roll)))
1a)一种变体是使用
ave
而不是
split

n <- nrow(dframe)
transform(dframe, roll = ave(1:n, z, FUN = function(ix) roll(dframe[ix, ]))
给予:

    x  y z     roll
a1  1  8 a 8.000000
a2  2  9 a 5.200000
a3  3 10 a 4.000000
b1  4 11 b 2.750000
b2  5 12 b 2.536585
b3  6 13 b 2.363636
b4  7 14 b 2.222222
b5  8 15 b 2.105263
b6  9 16 b 2.007380
b7 10 17 b 1.924528
# A tibble: 10 x 4
# Groups:   z [2]
       x     y z      roll
   <int> <int> <fct> <dbl>
 1     1     8 a      8   
 2     2     9 a      5.20
 3     3    10 a      4.00
 4     4    11 b      2.75
 5     5    12 b      2.54
 6     6    13 b      2.36
 7     7    14 b      2.22
 8     8    15 b      2.11
 9     9    16 b      2.01
10    10    17 b      1.92
3a)
roll
在(1)中可以替换为
roll2
在以下情况下,它不使用包,甚至不使用
lm
为我们提供另一个基本的R解决方案。同样,
L
来自(1)


roll2谢谢-分组如何?只需为函数提供另一个参数?我可能误解了你的问题。如果要单独分析由z值定义的每个组,可以首先使用split函数,然后在每个split:split(dframe,dframe$z)上运行上述代码。谢谢-分组如何?只需为函数提供另一个参数?我可能误解了你的问题。如果要单独分析由z值定义的每个组,可以首先使用split函数,然后在每个split上运行上述代码:split(dframe,dframe$z)。太棒了!1+2有效!就我的理解而言,为什么参数1:n提供给rollapplyr两次?其次,参数ix的值如何传递给函数(ix)?有关参数定义,请参见
?rollapply
。其效果是依次将1、1:2、1:3等传递给匿名函数。太棒了!1+2有效!就我的理解而言,为什么参数1:n提供给rollapplyr两次?其次,参数ix的值如何传递给函数(ix)?有关参数定义,请参见
?rollapply
。其效果是依次将1、1:2、1:3等传递给匿名函数。