朱莉娅线性回归

use*_*272 5 julia

我试图在Julia中进行线性回归.我有一个10列的数据框.前9列是我称之为X的预测变量,最后一列是我称之为Y的响应变量

我输入linreg(X, Y)但是我收到一条错误消息,说linreg没有匹配DataFrame和DataArray Float的方法.

我想知道如何解决这个问题.我在考虑将X转换为数据阵列

我试过convert(X, Array)但是也犯了一个错误:'转换没有方法匹配转换'有没有人有任何建议

spe*_*on2 15

如果您已经拥有数据DataFrame,则应该查看GLM.jl包.

具体来说,如果你是R用户,该lm功能应该做你想做的事情并且非常熟悉.

如果您发布更多的代码(也许这列中的DataFrame商店XY),我们可以进一步帮助您.


小智 6

更新:dot在对数组执行标量加法时,必须在 Julia 1.0 中使用运算符。IEy = m*x .+ b

您还可以使用简单的线性代数进行线性回归。下面是一个例子:

# Linear Algebra style
# For single linear regresion y= mx .+ b
m = 3.3; b = 2; x = rand(100,1)
y = m * x .+ b
# add noise
yn= y + randn(size(y)) * 0.5

# regression
X = zeros(100,2); X[:,1] = x;  X[:,2] = 1.0
coeff_pred = X\yn

slope =  round(coeff_pred[1], 2)
intercept = round(coeff_pred[2], 2)

println("The real slope is $m, and the predicted slope is $slope")
println("The real intercept is $b, and the predicted slope is $intercept")
Run Code Online (Sandbox Code Playgroud)