diff --git a/R/make_dsem_ram.R b/R/make_dsem_ram.R index 0df2bf0..e926e1e 100644 --- a/R/make_dsem_ram.R +++ b/R/make_dsem_ram.R @@ -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)) @@ -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" @@ -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") # diff --git a/vignettes/dynamic_factor_analysis.Rmd b/vignettes/dynamic_factor_analysis.Rmd index 737822f..51c4897 100644 --- a/vignettes/dynamic_factor_analysis.Rmd +++ b/vignettes/dynamic_factor_analysis.Rmd @@ -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,