我有一个 xts 对象 X
dput(x)
structure(c(111.09, 110.95, 110.71, 110.79, 110.72, 110.83, 110.75,
110.74, 110.85, 110.95, 110.98, 120.99, 121.27, 120.61, 120.55,
120.76, 120.86, 120.82, 120.88, 121.1, 120.86, 120.76, 111.29,
111.08, 110.95, 110.88, 111.05, 111.17, 111.17, 111.17, 111.2,
111.19, 111.28), .Dim = c(11L, 3L), .Dimnames = list(NULL, c("val1",
"val2", "val3")), index = structure(c(1356987540, 1357160340,
1357246740, 1357333140, 1357592340, 1357678740, 1357765140, 1357851540,
1357937940, 1358197140, 1358283540), tzone = "GMT", tclass = c("POSIXct",
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "GMT", tclass = c("POSIXct",
"POSIXt"), tzone = "GMT", class = c("xts", "zoo"))
它的数据看起来像这样
val1 val2 val3
2012-12-31 20:59:00 111.09 120.99 111.29
2013-01-02 20:59:00 110.95 121.27 111.08
2013-01-03 20:59:00 110.71 120.61 110.95
2013-01-04 20:59:00 110.79 120.55 110.88
2013-01-07 20:59:00 110.72 120.76 111.05
2013-01-08 20:59:00 110.83 120.86 111.17
2013-01-09 20:59:00 110.75 120.82 111.17
2013-01-10 20:59:00 110.74 120.88 111.17
2013-01-11 20:59:00 110.85 121.10 111.20
2013-01-14 20:59:00 110.95 120.86 111.19
2013-01-15 20:59:00 110.98 120.76 111.28
我可以通过使用得到每个 val(val1、val2、val3)cor(x, method="pearson")
之间的相关性,我想要一个类似的矩阵,但我想要线性回归系数而不是三个 val 之间的相关性。我创建了一个完全符合我要求的函数,但 R 对我来说是新的,我觉得我是以非常 C/C++ 的方式完成的。我希望也许有人可以帮助我创建一个更像 R 的解决方案(也许使用其中一种 apply 方法,或者甚至是指向可以帮助我理解如何进行此类操作的东西的链接)。
我创建的函数如下所示
getLmFromValues <- function( vals ) {
#create an empty data.frame
df <- data.frame( matrix(0, nrow = ncol(vals), ncol = ncol(vals) ) )
colnames(df) <- colnames(vals)
rownames(df) <- colnames(vals)
for( outer in 1:ncol(vals) ) {
for ( inner in 1:ncol(vals) ) {
if ( inner == outer ) {
df[outer,inner] <- NA #could also be 1
}
else {
#get linear model of vectors and store the slope coeficient
df[outer, inner] <- coef( lm( vals[,outer] ~ vals[,inner] ) )[2]
}
}
}
return( df )
}
我的函数的输出如下所示
val1 val2 val3
val1 NA 0.3015123 0.6071429
val2 0.8122685 NA 0.7978208
val3 0.6384838 0.3114367 NA