2013-04-22 30 views
2

我想爲50x30矩陣生成多個正交多項式。r中是否有任何多項正交多項式函數? (除poly以外)

結果應該有30 + 30 + 30C2 = 30 + 30 + 435 = 505列和50行。 我厭倦polyR基本軟件包中,即使對於第一順序,它也會耗盡內存。 R中是否有任何函數可以做多個正交多項式?(試過orthopolynom ,但它只適用於單變量)或者它太難實現了?由於

這裏是我的代碼

n=50 
k=30 
x=matrix(rnorm(n*k),nrow=n,ncol=k) 
poly(x,degree=1) 
Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : cannot allocate vector of length 1073741824 

回答

3

爲了您30C2條款你不想正交多項式,而是所有的雙向互動方面:

paste(combn(paste0("X", 1:30), 2, FUN=paste, collapse="*"), collapse="+") 

對於正交多項式你可能想要的度數2:

paste("poly(", paste0("X", 1:30), ", degree=2)", collapse="+") 

你需要用012構造一個R公式如果你想用它來回歸。

我不知道這是否會爲您的正交多項式或沒有,但它會給你所需的複雜的公式表達:

as.formula(paste(" ~ (", paste0("X", 1:30 , collapse="+"), ")^2", collapse="")) 
#-------------- 
~(X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 + X12 + 
X13 + X14 + X15 + X16 + X17 + X18 + X19 + X20 + X21 + X22 + 
X23 + X24 + X25 + X26 + X27 + X28 + X29 + X30)^2 

這將擴大到所有的線性方面,所有的平方項和所有的雙向組合。見help(formula)。並看看擴展後的用途:

terms(as.formula( 
     paste(" ~ (", paste0("X", 1:30 , collapse="+"), ")^2", collapse="") 
     ))