Rob Donnelly
Rob Donnelly

Reputation: 2326

Can Julia's GLM do weighted least squares?

I'd like to run weighted least squares regression. The documentation for the GLM package seems to allude to this being an option, but I can't figure out exactly what the syntax should be.

test = DataFrames.DataFrame(x = float([1:12]), y = randn(12), w)
lm(y~x, test)

Suppose I wanted to weight each observation by some weighting vector

I tried

fit(LinearModel, y~x, data, wts=[rep(.5,6), rep(.7,6)])

but it's not able to find a matching method.

Are there any documents with more examples of how to use the GLM package?

Upvotes: 2

Views: 1169

Answers (1)

spencerlyon2
spencerlyon2

Reputation: 9686

I think @rickhg12hs had it right:

julia> using DataFrames

julia> using GLM

julia> test = DataFrames.DataFrame(x = float([1:12]), y = randn(12));

julia> glm(y ~ x,test, Normal(), IdentityLink(), wts=[rep(.5,6), rep(.2,6)])
DataFrames.DataFrameRegressionModel{GLM.GeneralizedLinearModel{GLM.GlmResp{Array{Float64,1},Distributions.Normal,GLM.IdentityLink},GLM.DensePredChol{Float64}},Float64}:

Coefficients:
              Estimate Std.Error z value Pr(>|z|)
(Intercept)   0.715555  0.506611 1.41243   0.1578
x            -0.137865 0.0827818 -1.6654   0.0958


julia> glm(y ~ x,test, Normal(), IdentityLink(), wts=[rep(.5,6), rep(.7,6)])
DataFrames.DataFrameRegressionModel{GLM.GeneralizedLinearModel{GLM.GlmResp{Array{Float64,1},Distributions.Normal,GLM.IdentityLink},GLM.DensePredChol{Float64}},Float64}:

Coefficients:
              Estimate Std.Error z value Pr(>|z|)
(Intercept)   0.914117   0.59612 1.53345   0.1252
x            -0.187296 0.0765347 -2.4472   0.0144

Upvotes: 4

Related Questions