Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error calculating total indirect effects #309

Closed
NicholasDanks opened this issue Aug 25, 2022 · 0 comments
Closed

Error calculating total indirect effects #309

NicholasDanks opened this issue Aug 25, 2022 · 0 comments

Comments

@NicholasDanks
Copy link
Collaborator

NicholasDanks commented Aug 25, 2022

Thank you very much for spotting this bug in my code. Without the help and support of our community, we would not be able to find and test all possible code. I identified the problem.
The function for calculating the total_effects had some faulty logic.
specifically, the function is as follows:

function(path_coef) {
  output <- path_coef
  paths <- path_coef
  while (sum(paths) > 0) {
    paths <- paths %*% path_coef
    output <- output + paths
  }
  return(output)
}

to calculate total effects, we simply need to multiply the path coefficients matrix with iteself and add the product. This continues until the product of the path coef matrix is equal to zero (no more indirect effects).
In my logic I had used a while() statement which said to continue calculating the product of the path coef matrix and adding it up until the paths matrix sums to a non-positive. This makes sense because the product of a number with itself cannot be negative.
However I did not consider that the initial starting point could sum to a non-neg. Thus, the function was never executing.

My fix is as followS:

function(path_coef) {
  output <- path_coef
  paths <- path_coef
  while (sum(paths) != 0) {
    paths <- paths %*% path_coef
    output <- output + paths
  }
  return(output)
}

Nevertheless, you have brought this glitch to our attention and I can now deploy the bug-fix in our next version. In the meantime, you can download the bugfix version (temp) using:

devtools::install_github(repo = "https://github.com/sem-in-r/seminr/", ref = "bugfixes_aug_2022")
NicholasDanks added a commit that referenced this issue Oct 12, 2022
#308)

* changes to indexing in get_sm_nodes() to fix a bug that arises from using a single letter construct name. Issue #305

* Attend issue #309 regarding bug in total_effects() function

* Addresses issue #289 - bug when calculating fSquared on HOC models

* Addreswses issue #310

* refactor to use smMatrix helper function

* Remove browser() from code

* final changes to bugfixes

Co-authored-by: Nicholas Danks <nicholasdanks@Nicholass-MacBook-Pro-2.local>
Co-authored-by: Nicholas Danks <you@example.com>
Co-authored-by: Soumya Ray <soumya.ray@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant