这就是我构建线性回归的方法,但是当我包含两个以上的列时,我的代码就不能工作。
// Load relevant columns into memory
//
t:?[`data;enlist(=;`date;dat);0b;(ind,dep)!ind,dep];
X:9h$enlist[count[t]#1],t ind;
beta:inv[X mmu flip X] mmu X mmu 9h$t dep;
res:(`yInt,ind)!beta;ind是symbol中独立的字段名,dep是symbol中的依赖字段名。此部件inv[X mmu flip X]不起作用。如果我为ind包含了两个以上的字段,并且我不能执行inv,那么将有一行为空。我真的不确定如何才能包含更多用于独立变量的列/字段。
发布于 2016-11-03 06:47:49
只需稍作修改,您的代码就能为我工作:
/ Generate some random data
n:10;t:([]x1:n?1f;x2:n?1f;x3:n?1f)
/ Compute y = 1 + 2*x1 + 3*x2 + 4*x3 + small random noise
update y:1+(2*x1)+(3*x2)+(4*x3)+n?1e-3 from `t;
/ Define ind and dep
ind:`x1`x2`x3
dep:enlist`y
/ Build the design matrix
X:enlist[count[t]#1f],t ind
/ Compute the regression parameters
inv[X mmu flip X] mmu X mmu flip t dep
1.00052
1.99973
2.999979
4.000045唯一可能棘手的部分是记住登记和转置dep。
从您的描述来看,您的数据可能有问题。它要么包含NaNs,要么您的ind变量不是独立的。
发布于 2016-11-01 19:12:09
我发现了一个谷歌群组的问题,询问线性回归:
https://groups.google.com/forum/#!topic/personal-kdbplus/CRf-kOx-v4I
他们在这篇文章中提到了如何使用lsq命令:
http://code.kx.com/q/ref/matrixes/#lsq
另一个答案给出了示例代码,希望这个信息能帮到你。
发布于 2016-11-02 19:31:47
您能提供您正在使用的数据的子集吗?通常,inv失败会向我暗示您的矩阵中有一些空值。通过在尝试查找矩阵的逆矩阵之前使用0f^可以修复此问题。(用浮点零填充矩阵的空元素。)
http://code.kx.com/q/ref/lists/#fill
快速示例:
inv 3 cut 1.1 2.2 3.1 4.5 0n 0n 1.2 7 8f 返回:
(0n 0n 0n;0n 0n 0n;0n 0n 0n)然而..。
inv 0f^3 cut 1.1 2.2 3.1 4.5 0n 0n 1.2 7 8f返回:
(0 0.2222222 -0;-1.9512195 0.2753388 0.7560976;1.7073171 -0.2742547 -0.5365854) https://stackoverflow.com/questions/40349477
复制相似问题