# How to specify weights (wts) in MixedModels.jl?

**URL:** <https://discourse.julialang.org/t/how-to-specify-weights-wts-in-mixedmodels-jl/98707>\
**Category:** Statistics\
**Tags:** mixed-models\
**Created:** [May 11, 2023, 11:22pm UTC](https://discourse.julialang.org/t/how-to-specify-weights-wts-in-mixedmodels-jl/98707 "2023-05-11T23:22:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rscho314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rscho314/32/49704_2.png) [@Rscho314](https://discourse.julialang.org/u/Rscho314)\
**Post date:** [May 11, 2023, 11:22pm UTC](https://discourse.julialang.org/t/how-to-specify-weights-wts-in-mixedmodels-jl/98707/1 "2023-05-11T23:22:44Z")

</div>

Hi,

I specified a multilevel longitudinal model in MixedModels.jl. I am having a heteroscedasticity issue, for which I’d like to try to weight observations.

So, the unweighted model:

```julia
fit(MixedModel,@formula(maxtrop~1+time*glucose+(time|mr/anest)),d,REML=false)

```

Unit weighting works (3577 is the row count of the dataset):

```julia
fit(MixedModel,@formula(maxtrop~1+time*glucose+(time|mr/anest)),d,wts=uweights(3577),REML=false)

```

… but, I can’t make it further than that. Any other spec results in the following error

> Initial objective evaluation failed, rescaling initial guess and trying again.  
> Failure of the initial evaluation is often indicative of a model specification that is not well supported by the data and/or a poorly scaled model.

I tried, among others:

```julia
wts=aweights(float.(rand([1,2,3],3577)))

```

```julia
wts=aweights(rand(Float64,3577))

```

I don’t know if it might be of importance, but there is almost no signal in the data. The unweighted model results:

```julia
Linear mixed model fit by maximum likelihood
 maxtrop ~ 1 + time + glucose + time & glucose + (1 + time | mr) + (1 + time | mr & anest)
    logLik -2 logLik AIC AICc BIC     
  23762.1125 -47524.2251 -47502.2251 -47502.1510 -47434.2200

Variance components:
              Column Variance Std.Dev. Corr.
mr & anest (Intercept) 0.05686690 0.23846781
           time 0.00000000 0.00000000 -1.00
mr (Intercept) 0.07487753 0.27363758
           time 0.00000000 0.00000000 +1.00
Residual 0.00000001 0.00009071
 Number of obs: 3577; levels of grouping factors: 482, 477

  Fixed-effects parameters:
─────────────────────────────────────────────────────────
                       Coef. Std. Error z Pr(>|z|)
─────────────────────────────────────────────────────────
(Intercept) -0.150825 0.0165952 -9.09 <1e-18
time -2.40061e-9 5.61142e-6 -0.00 0.9997
glucose 8.79342e-10 8.81333e-7 0.00 0.9992
time & glucose 2.37489e-10 6.75191e-7 0.00 0.9997
─────────────────────────────────────────────────────────

```

Any help would be greatly appreciated. Thanks!
