Skip to content

Commit

Permalink
fix bug when varobs aren't in same order as var
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelJuillard committed Jun 23, 2024
1 parent 8e776c8 commit 47193d5
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@ function get_transposed_data!(context::Context,
else
error("needs datafile or data argument")
end
return transpose(Matrix(context.work.data))
m, n = size(context.work.data)
transposeddatamatrix = Matrix{Union{Float64, Missing}}(undef, n, m)
for i = 1:n
c = findfirst(x -> x == Symbol(variables[i]), column_labels(context.work.data))
for j = 1:m
transposeddatamatrix[i, j] = context.work.data[j, c]
end
end
return transposeddatamatrix
end

function get_detrended_data(context::Context,
Expand Down Expand Up @@ -169,31 +177,31 @@ function detrend_data!(dtdata, context::Context,
linear_trend = []
quadratic_trend = []
if !isempty(trends.endogenous_steady_state)
steady_state = Vector(AxisArrays.AxisArray(trends.endogenous_steady_state, endogenous_names)[variables])
steady_state = AxisArrays.AxisArray(trends.endogenous_steady_state, endogenous_names)[variables]
end
if !isempty(trends.endogenous_linear_trend)
linear_trend = Vector(AxisArrays.AxisArray(trends.endogenous_linear_trend, endogenous_names)[variables])
linear_trend = AxisArrays.AxisArray(trends.endogenous_linear_trend, endogenous_names)[variables]
end
if !isempty(trends.endogenous_quadratic_trend)
quadratic_trend = Vector(AxisArrays.AxisArray(trends.endogenous_quadratic_trend, endogenous_names)[variables])
quadratic_trend = AxisArrays.AxisArray(trends.endogenous_quadratic_trend, endogenous_names)[variables]
end
if context.modfileinfo.has_trends
if !isempty(quadratic_trend)
remove_quadratic_trend!(dtdata,
steady_state,
linear_trend,
quadratic_trend,
dim
variables
)
else
remove_linear_trend!(dtdata,
steady_state,
linear_trend,
dim
variables
)
end
else
remove_steady_state!(dtdata, steady_state, dim)
remove_steadystate!(dtdata, steady_state, variables)
end

return dtdata
Expand Down Expand Up @@ -320,3 +328,36 @@ function is_continuous(periods::Vector{ExtendedDates.DatePeriod})
end
end
end

function remove_quadratic_trend!(data::Matrix, steadystate::AxisArrays.AxisArray, linear_trend::AxisArrays.AxisArray, quadratic_trend::AxisArrays.AxisArray, variables)
for (c, v) in enumerate(variables)
ss = steadystate[v]
lt = linear_trend[v]
sqt = quadratic_trend[v]
for t in axes(data, 2)
data[c, t] -= ss + lt*t + qt*t^2
end
end
return data
end

function remove_linear_trend!(data::Matrix, steadystate::AxisArrays.AxisArray, linear_trend::AxisArrays.AxisArray, variables)
for (c, v) in enumerate(variables)
ss = steadystate[v]
lt = linear_trend[v]
for t = 1:axes(data, 2)
data[c, t] -= ss + lt*t
end
end
return data
end

function remove_steadystate!(data::Matrix, steadystate::AxisArrays.AxisArray, variables)
for (c, v) in enumerate(variables)
ss = steadystate[v]
for t in axes(data, 2)
data[c, t] -= ss
end
end
return data
end

0 comments on commit 47193d5

Please sign in to comment.