Skip to content

Commit be67f5c

Browse files
kleinschmidtpalday
andauthored
use a named tuple with coefnames for β in simulate (#262)
* use a named tuple with fixefnames for β in simulate * handle rank-deficiency in parametricbootstrap and simulate! Co-authored-by: Phillip Alday <[email protected]>
1 parent 31dd5b3 commit be67f5c

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/simulate.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function parametricbootstrap(
5858
rng::AbstractRNG,
5959
n::Integer,
6060
morig::LinearMixedModel{T};
61-
β = morig.β,
61+
β = coef(morig),
6262
σ = morig.σ,
6363
θ = morig.θ,
6464
use_threads = false,
@@ -68,6 +68,14 @@ function parametricbootstrap(
6868
θ = convert(Vector{T},θ)
6969
βsc, θsc, p, k, m = similar(β), similar(θ), length(β), length(θ), deepcopy(morig)
7070
y₀ = copy(response(m))
71+
72+
β_names = (Symbol.(fixefnames(morig))..., )
73+
rank = length(β_names)
74+
# fixef! requires that we take all coefs, even for pivoted terms
75+
if rank length(βsc)
76+
resize!(βsc, rank)
77+
end
78+
7179
# we need to do for in-place operations to work across threads
7280
m_threads = [m]
7381
βsc_threads = [βsc]
@@ -93,7 +101,10 @@ function parametricbootstrap(
93101
(
94102
objective = mod.objective,
95103
σ = mod.σ,
96-
β = SVector{p,T}(fixef!(βsc, mod)),
104+
# fixef! does the pivoted, but not truncated coefs
105+
# coef does the non-pivoted
106+
# fixef does either pivoted+truncated or unpivoted
107+
β = NamedTuple{β_names}(fixef!(βsc, mod)[1:rank]),
97108
θ = SVector{k,T}(getθ!(θsc, mod)),
98109
)
99110
end
@@ -154,11 +165,22 @@ function simulate!(
154165
σ = m.σ,
155166
θ = T[],
156167
) where {T}
168+
length(β) == length(fixef(m)) ||
169+
length(β) == length(coef(m)) ||
170+
throw(ArgumentError("You must specify all (non-singular) βs"))
171+
157172
β = convert(Vector{T},β)
158173
σ = T(σ)
159174
θ = convert(Vector{T},θ)
160175
isempty(θ) || setθ!(m, θ)
161176

177+
if length(β) length(coef(m))
178+
padding = length(coef(m)) - length(β)
179+
for ii in 1:padding
180+
push!(β, -0.0)
181+
end
182+
end
183+
162184
y = randn!(rng, response(m)) # initialize y to standard normal
163185

164186
for trm in m.reterms # add the unscaled random effects
@@ -169,7 +191,7 @@ function simulate!(
169191
m
170192
end
171193

172-
function simulate!(m::LinearMixedModel{T}; β = m.β, σ = m.σ, θ = T[]) where {T}
194+
function simulate!(m::LinearMixedModel{T}; β = coef(m), σ = m.σ, θ = T[]) where {T}
173195
simulate!(Random.GLOBAL_RNG, m, β = β, σ = σ, θ = θ)
174196
end
175197

0 commit comments

Comments
 (0)