对多列数据使用rollapply和lm
我有一个类似于以下的数据框,共有500列:对多列数据使用rollapply和lm,r,zoo,lm,rollapply,R,Zoo,Lm,Rollapply,我有一个类似于以下的数据框,共有500列: Probes <- data.frame(Days=seq(0.01, 4.91, 0.01), B1=5:495,B2=-100:390, B3=10:500,B4=-200:290) probe试试这个: # here are the xmin values you wanted xmins <- Probes$Days[seq(1,nrow(Probes),6)] # here we build a function that w
Probes <- data.frame(Days=seq(0.01, 4.91, 0.01), B1=5:495,B2=-100:390, B3=10:500,B4=-200:290)
probe试试这个:
# here are the xmin values you wanted
xmins <- Probes$Days[seq(1,nrow(Probes),6)]
# here we build a function that will run regressions across the columns
# y1 vs x, y2 vs x, y3 vs x...
# you enter the window and by (12/6) in order to limit the interval being
# regressed. this is later called in do.call
runreg <- function(Probes,m,window=12,by=6){
# beg,end are used to specify the interval
beg <- seq(1,nrow(Probes),by)[m]
end <- beg+window-1
# this is used to go through all the columns
N <- ncol(Probes)-1
tmp <- numeric(N)
# go through each column and store the coefficients in tmp
for(i in 1:N){
y <- Probes[[i+1]][beg:end]
x <- Probes$Days[beg:end]
tmp[i] <- coef(lm(y~x))[2][[1]]
}
# put all our column regressions into a dataframe
res <- rbind('coeff'=tmp)
colnames(res) <- colnames(Probes)[-1]
return(res)
}
# now that we've built the function to do the column regressions
# we just need to go through all the window-ed regressions (row regressions)
res <- do.call(rbind,lapply(1:length(xmins),function(m) runreg(Probes,m)))
# these rownames are the index of the xmin values
rownames(res) <- seq(1,nrow(Probes),6)
res <- data.frame(res,xmins)
#以下是您想要的xmin值
xmins1)定义一个zoo对象z
,其数据包含探测
,其索引取自探测的第一列,即天
。注意,lm
允许y
作为矩阵定义计算回归系数的coefs
函数。最后,rollapply
overz
。注意,返回对象的索引给出了xmin
library(zoo)
z <- zoo(Probes, Probes[[1]])
coefs <- function(z) c(unlist(as.data.frame(coef(lm(z[,-1] ~ z[,1])))))
rz <- rollapply(z, 12, by = 6, coefs, by.column = FALSE, align = "left")
请注意,DF您还可以使用rollRegres
包,如下所示
#设置数据
如果我正确理解了你的问题,你需要在两个方向上“循环”。来自zoo
的rollapply
将为您提供窗口方向(向下移动您的行)。但是,如果要进行多个y
回归,还需要循环使用这些可能性中的每一个(每列都是另一个回归),好的,谢谢。你是说除了rollapply函数之外,我还需要循环它?我不太确定当把它们放在一起的时候会是什么样子。你认为plyr的colwise函数也可以吗?这取决于你想要的是什么。你想回归y1对x、y2对x、y3对x……等等吗。他们自己。或者,除了滚动窗口y1对x(仅前12个,即1-12),y1对x(仅18-30),…,etcI还需要第二个…除滚动窗口外的按列回归。几乎和你写的一样,除了滚动窗口会每6个单元格移动一次,像这样:Y1[1:12]~x,Y1[6:18]~x,Y1[12:24],等等。所以我基本上会得到一个矩阵,它的值是我原来的1/6。我们将为这做一个示例矩阵,这样就清楚了。是的,这很有效。我在Excel中仔细检查一些斜率时得到的值略有不同,但我猜这可能是因为两个不同的程序在拟合线性模型方面存在差异,而不是因为编码问题。谢谢你的帮助。这可能也是索引的问题。也许我差一个单位?但这很容易解决。总体结构应该会有所帮助!尝试更改结束注意,此代码已修改为较短。谢谢,此代码也有效,并且值与我在Excel中测试了一些代码后得到的值相匹配。我的数据帧中不需要截取,所以我猜我只需要添加一个[-1]如果我想消除这些列,可以在coef函数中的某个地方?在这种情况下,coefs
函数可以简化为:coefs@user507我想知道您是否可以运行这两个代码,看看哪一个比上面两个类似的代码更有效,第二个(rollover row number)就我的目的而言,是两者中的较好者,也是我最终使用的。下面的代码起作用了,但我无法解决值稍微偏离的问题。正如您所建议的,我最初也认为这可能是一个索引问题,但尝试多个不同的索引更改并没有解决这个问题。
library(zoo)
z <- zoo(Probes, Probes[[1]])
coefs <- function(z) c(unlist(as.data.frame(coef(lm(z[,-1] ~ z[,1])))))
rz <- rollapply(z, 12, by = 6, coefs, by.column = FALSE, align = "left")
> head(rz)
B11 B12 B21 B22 B31 B32 B41 B42
0.01 4 100 -101 100 9 100 -201 100
0.07 4 100 -101 100 9 100 -201 100
0.13 4 100 -101 100 9 100 -201 100
0.19 4 100 -101 100 9 100 -201 100
0.25 4 100 -101 100 9 100 -201 100
0.31 4 100 -101 100 9 100 -201 100
library(zoo)
y <- as.matrix(Probes[-1])
Days <- Probes$Days
n <- nrow(Probes)
coefs <- function(ix) c(unlist(as.data.frame(coef(lm(y ~ Days, subset = ix)))),
xmins = Days[ix][1])
r <- rollapply(1:n, 12, by = 6, coefs)
devtools::install_github("boennecd/rollRegres", upgrade_dependencies = FALSE,
build_vignettes = TRUE)