是把factor_value作为Y, 其他列作为X,然后取他的计算残差再重新赋值给factor_value,请问怎么处理


attachments-2022-09-PZadr9wb631fea744f8c5.png

select ols(factor_value, clean_factor.colNames()[3:], true, 2)['Residual'] from clean_factor context by date  我是想对clean_factor在相同日期内做一个OLS的操作,具体的是把factor_value作为Y, 其他列作为X,然后取他的计算残差再重新赋值给factor_value

报错 The dimension of dependent doesn't match the dimension of independent factors.' script: '

请先 登录 后评论

1 个回答

mhxiang


生成模拟数据:

x1=1 3 5 7 11 16 23 1 3 5 7 11 16 23
x2=2 8 11 34 56 54 100 2 8 11 34 56 54 100
y=0.1 4.2 5.6 8.8 22.1 35.6 77.2 0.1 4.2 5.6 8.8 22.1 35.6 77.2;
date=take(2022.01.01..2022.01.03,14)
code=take(`600000`6000001`600002,14)
clean_factor=table(date,code,y,x1 as `11,x2 as `12)

可以直接通过元编程的方式实现,使用makeCall和makeUnifiedCall函数,指定参数分别调用member、ols和matrix函数生成脚本:

yColName = "y"
xColName = clean_factor.colNames()[3:]
residual = makeCall(member, makeCall(ols, sqlCol(yColName), makeUnifiedCall(matrix, sqlCol(xColName)), 1, 2), "Residual")
selects = [<date>, <code>, sqlColAlias(residual, "residual")]
sql(selects, clean_factor, groupBy=<date>, groupFlag=0).eval()

当列数不多时,也可以直接写sql的方式实现。

由于因子列名是数字开头的,包含特殊符号或以数字开头的列名在 SQL 中引用时,需将列名用双引号引用,并在其之前使用下划线作为标识,例如:_"11",_"12",所以SQL可以写成如下形式:

select date,code,ols(y,matrix(_"11",_"12"),1,2)[`Residual] as Residual from clean_factor context by date



请先 登录 后评论