Skip to content

Commit

Permalink
another fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Oct 25, 2024
1 parent f882084 commit 66c66d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
10 changes: 6 additions & 4 deletions R/make_dsem_ram.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function( sem,
}

# Loop through paths
P_kk = drop0(sparseMatrix( i=1, j=1, x=0, dims=rep(length(variables)*length(times),2) )) # Make with a zero
G_kk = P_kk = drop0(sparseMatrix( i=1, j=1, x=0, dims=rep(length(variables)*length(times),2) )) # Make with a zero
#P_kk = new("dgCMatrix")
#P_kk = Matrix()
#P_kk@Dim <- as.integer(rep(length(variables)*length(times),2))
Expand All @@ -350,9 +350,9 @@ function( sem,
dims = rep(length(variables),2) )
tmp_kk = kronecker(P_jj, L_tt)
if(abs(as.numeric(model[i,'direction']))==1){
P_kk = P_kk + tmp_kk * par.nos[i]
P_kk = P_kk + tmp_kk * i
}else{
G_kk = G_kk + tmp_kk * par.nos[i]
G_kk = G_kk + tmp_kk * i
}
#for( t in seq_along(times) ){
# # Get index for "from"
Expand All @@ -372,7 +372,9 @@ function( sem,
f = \(x) matrix(unlist(mat2triplet(x)),ncol=3)
ram = rbind( cbind(1, f(P_kk)),
cbind(2, f(G_kk)) )
ram = data.frame( ram, startvalues[ram[,4]] )
ram = data.frame( ram[,1:3,drop=FALSE],
as.numeric(par.nos)[ram[,4]],
as.numeric(startvalues)[ram[,4]] )
colnames(ram) = c("heads", "to", "from", "parameter", "start")

#
Expand Down
52 changes: 26 additions & 26 deletions vignettes/dynamic_factor_analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,33 @@ tsdata = ts( cbind(tsdata, newcols), start=1978)
tsdata = scale( tsdata, center=TRUE, scale=TRUE )
# Automated version
sem = make_dfa( variables = c("SJI","EBays","SJF","PSnd","HC"),
n_factors = n_factors )
#sem = make_dfa( variables = c("SJI","EBays","SJF","PSnd","HC"),
# n_factors = n_factors )
# Manual specification to show structure, using equations-and-lags interface
#equations = "
# # Loadings of variables onto factors
# SJI = L11(0.1) * F1
# EBays = L12(0.1) * F1 + L22(0.1) * F2
# SJF = L13(0.1) * F1 + L23(0.1) * F2
# PSnd = L14(0.1) * F1 + L24(0.1) * F2
# HC = L15(0.1) * F1 + L25(0.1) * F2
#
# # random walk for factors
# F1 = NA(1) * lag[F1,1]
# F2 = NA(1) * lag[F2,1]
#
# # Unit variance for factors
# V(F1) = NA(1)
# V(F2) = NA(1)
#
# # Zero residual variance for variables
# V(SJI) = NA(0)
# V(EBays) = NA(0)
# V(SJF) = NA(0)
# V(PSnd) = NA(0)
# V(HC) = NA(0)
#"
#sem = convert_equations(equations)
equations = "
# Loadings of variables onto factors
SJI = L11(0.1) * F1
EBays = L12(0.1) * F1 + L22(0.1) * F2
SJF = L13(0.1) * F1 + L23(0.1) * F2
PSnd = L14(0.1) * F1 + L24(0.1) * F2
HC = L15(0.1) * F1 + L25(0.1) * F2
# random walk for factors
F1 = NA(1) * lag[F1,1]
F2 = NA(1) * lag[F2,1]
# Unit variance for factors
V(F1) = NA(1)
V(F2) = NA(1)
# Zero residual variance for variables
V(SJI) = NA(0)
V(EBays) = NA(0)
V(SJF) = NA(0)
V(PSnd) = NA(0)
V(HC) = NA(0)
"
sem = convert_equations(equations)
# Initial fit
mydsem0 = dsem( tsdata = tsdata,
Expand Down

0 comments on commit 66c66d7

Please sign in to comment.