Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给定矩阵中所有可能的向量组合-R_R - Fatal编程技术网

给定矩阵中所有可能的向量组合-R

给定矩阵中所有可能的向量组合-R,r,R,与前面的问题类似。我收到一条错误消息,上面写着“数据中出错[,j]:下标超出范围”,我不知道为什么 我只是尝试将给定矩阵中所有向量的乘积存储在三维数组中,以测试多元回归中交互项的显著性。我已经提供了一段示例代码,如有任何帮助,将不胜感激 a <- array( 1 , c( 3 ,1 ) ) b <- array( 2 , c( 3 ,1 ) ) d <- array( 3 , c( 3 ,1 ) ) Data <- array( c( a ,

与前面的问题类似。我收到一条错误消息,上面写着“数据中出错[,j]:下标超出范围”,我不知道为什么

我只是尝试将给定矩阵中所有向量的乘积存储在三维数组中,以测试多元回归中交互项的显著性。我已经提供了一段示例代码,如有任何帮助,将不胜感激

a     <- array( 1 , c( 3 ,1 ) )
b     <- array( 2 , c( 3 ,1 ) )
d     <- array( 3 , c( 3 ,1 ) )
Data  <- array( c( a , b , d ) , c( 3 , 3 ) )
#
Sdata   <-  array( dim( Data ) , c( 2 , 1 ) )
    Q       <-  ( Sdata[ 2 , 1 ] * ( Sdata[ 2 , 1 ] - 1 ) ) / 2
    #
    Combos  <-  array( 0 , c( Sdata[ 1 , 1 ] , Sdata[ 2 , 1 ] , Sdata[ 2 , 1 ] - 1 ) )
    #
    Scombos <-  array( dim( Combos ) , c( 3 , 1 ) )
    #
   for( k in 1 : Scombos[ 2 , 1 ] - 1 ){
      for( j in k + 1 : Scombos[ 2 , 1 ]  ){
        Combos[ , j , k ]   <-  Data[ , j ] * Data[ , k ]                 
      }
   }

a@Seth是正确的,您应该使用公式接口
lm(…)
来生成交互项。但是,既然您询问了如何成对创建列的每个可能乘积,那么这里有一种方法

df     <- data.frame(Data)
comb   <- data.frame(combn(colnames(df),2),stringsAsFactors=F)
result <- do.call(cbind,lapply(comb,function(x)df[,x[1]]*df[,x[2]]))
colnames(result) <- sapply(comb,paste,collapse=".")
result
#      X1.X2 X1.X3 X2.X3
# [1,]     2     3     6
# [2,]     2     3     6
# [3,]     2     3     6

df请不要在示例代码中包含
rm(list=ls())
!为什么不让你的回归函数为你做这项工作呢。尤其是
*
。您可以在
?公式
中了解它。请