{"action":"created","issue":{"url":"https://api.github.com/repos/stefvanbuuren/mice/issues/102","repository_url":"https://api.github.com/repos/stefvanbuuren/mice","labels_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/102/labels{/name}","comments_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/102/comments","events_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/102/events","html_url":"https://github.com/stefvanbuuren/mice/issues/102","id":331984191,"node_id":"MDU6SXNzdWUzMzE5ODQxOTE=","number":102,"title":"Does \"flux()\" get the average in- and outbounds right?","user":{"login":"JoergMBeyer","id":40236837,"node_id":"MDQ6VXNlcjQwMjM2ODM3","avatar_url":"https://avatars1.githubusercontent.com/u/40236837?v=4","gravatar_id":"","url":"https://api.github.com/users/JoergMBeyer","html_url":"https://github.com/JoergMBeyer","followers_url":"https://api.github.com/users/JoergMBeyer/followers","following_url":"https://api.github.com/users/JoergMBeyer/following{/other_user}","gists_url":"https://api.github.com/users/JoergMBeyer/gists{/gist_id}","starred_url":"https://api.github.com/users/JoergMBeyer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JoergMBeyer/subscriptions","organizations_url":"https://api.github.com/users/JoergMBeyer/orgs","repos_url":"https://api.github.com/users/JoergMBeyer/repos","events_url":"https://api.github.com/users/JoergMBeyer/events{/privacy}","received_events_url":"https://api.github.com/users/JoergMBeyer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-13T13:00:42Z","updated_at":"2018-06-15T10:01:19Z","closed_at":null,"author_association":"NONE","body":"Dear Stef et al., \r\n\r\nI'd like to report what arguably looks like a conceptual bug in \"flux()\" to me. The way both the average inbound (\"ainb\") and the average outbound (\"aout\") are calculated, doesn't seem entirely sound. \r\n\r\nIn both cases, \"flux()\" first calculates a full matrix of indices from a given set of variables. \"Full\" meaning here \"bivariate _and univariate_ inbound (or outbound) indices -- that is, explicitely including those of all variables with themselves\". After that, row-wise means for said matrix are calculated. In consequence, each row-wise average carries the variable's inbound (or outbound) influence with itself. \r\n\r\nIMHO, this should not be considered correct for the following reasons: As we know, a variable can neither impute nor otherwise impact itself. Hence, univariate inbound or outbound measures are not really a thing; any in- or outbound index of a variable _influenced by itself_ should be considered nonsensical. Violating this constraint, like \"flux()\" currently does, leads to systematically underestimating the row-wise averages under \"ainb\" and \"aout\". \r\n\r\nSo, in essence, the problem's root is that the algorithm is being mislead to divide each row-wise sum by its full length (with \"mean()\"), instead of chipping one off to compensate for the bogus univariate index contained in each row. Luckily, fixing this problem is straightforward by first inserting a small inline function into \"flux()\" \r\n\r\n    .avg <- function(row) sum(row, na.rm = TRUE)/(length(row) - 1)\r\n\r\nand then changing the averaged in-/outbound calculations to \r\n\r\n    ainb <- apply(pat$mr/(pat$mr + pat$mm), 1, .avg)\r\n    aout <- apply(pat$rm/(pat$rm + pat$rr), 1, .avg)\r\n\r\n(One could of course speed up the averaging part by factoring out the denominator, so that \".avg()\" doesn't have to calculate it over and over again. For now I consider that a non-substantial, minor implementation detail.) \r\n\r\nThoughts? \r\n\r\n-- Jörg"},"comment":{"url":"https://api.github.com/repos/stefvanbuuren/mice/issues/comments/397574713","html_url":"https://github.com/stefvanbuuren/mice/issues/102#issuecomment-397574713","issue_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/102","id":397574713,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NzU3NDcxMw==","user":{"login":"stefvanbuuren","id":3919919,"node_id":"MDQ6VXNlcjM5MTk5MTk=","avatar_url":"https://avatars3.githubusercontent.com/u/3919919?v=4","gravatar_id":"","url":"https://api.github.com/users/stefvanbuuren","html_url":"https://github.com/stefvanbuuren","followers_url":"https://api.github.com/users/stefvanbuuren/followers","following_url":"https://api.github.com/users/stefvanbuuren/following{/other_user}","gists_url":"https://api.github.com/users/stefvanbuuren/gists{/gist_id}","starred_url":"https://api.github.com/users/stefvanbuuren/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stefvanbuuren/subscriptions","organizations_url":"https://api.github.com/users/stefvanbuuren/orgs","repos_url":"https://api.github.com/users/stefvanbuuren/repos","events_url":"https://api.github.com/users/stefvanbuuren/events{/privacy}","received_events_url":"https://api.github.com/users/stefvanbuuren/received_events","type":"User","site_admin":false},"created_at":"2018-06-15T10:01:19Z","updated_at":"2018-06-15T10:01:19Z","author_association":"OWNER","body":"Dear Jörg, I think your suggestion makes sense. Below is the result of `flux()` in `mice 3.0.0`.\r\n\r\n```\r\n> flux(pattern1)\r\n   pobs    influx outflux      ainb  aout  fico\r\nA 1.000 0.0000000       1       NaN 0.125 0.375\r\nB 1.000 0.0000000       1       NaN 0.125 0.375\r\nC 0.625 0.2857143       0 0.6666667 0.000 0.000\r\n\r\n> flux(pattern2)\r\n   pobs    influx   outflux      ainb      aout  fico\r\nA 1.000 0.0000000 1.0000000       NaN 0.2916667 0.625\r\nB 0.750 0.1176471 0.4285714 0.3333333 0.1666667 0.500\r\nC 0.375 0.4705882 0.0000000 0.5333333 0.0000000 0.000\r\n\r\n> flux(pattern3)\r\n   pobs influx outflux      ainb      aout fico\r\nA 1.000  0.000   1.000       NaN 0.3333333    1\r\nB 0.625  0.375   0.625 0.6666667 0.3333333    1\r\nC 0.375  0.625   0.375 0.6666667 0.3333333    1\r\n\r\n> flux(pattern4)\r\n   pobs influx outflux      ainb      aout      fico\r\nA 0.750  0.125   0.500 0.3333333 0.2222222 0.6666667\r\nB 0.625  0.250   0.375 0.4444444 0.2000000 0.6000000\r\nC 0.625  0.375   0.625 0.6666667 0.3333333 0.6000000\r\n```\r\n\r\nwhich has some `NaN` and uses all three columns. After your correction, the output is\r\n\r\n```\r\n> flux(pattern1)\r\n   pobs    influx outflux ainb   aout  fico\r\nA 1.000 0.0000000       1    0 0.1875 0.375\r\nB 1.000 0.0000000       1    0 0.1875 0.375\r\nC 0.625 0.2857143       0    1 0.0000 0.000\r\n\r\n> flux(pattern2)\r\n   pobs    influx   outflux ainb   aout  fico\r\nA 1.000 0.0000000 1.0000000  0.0 0.4375 0.625\r\nB 0.750 0.1176471 0.4285714  0.5 0.2500 0.500\r\nC 0.375 0.4705882 0.0000000  0.8 0.0000 0.000\r\n\r\n> flux(pattern3)\r\n   pobs influx outflux ainb aout fico\r\nA 1.000  0.000   1.000    0  0.5    1\r\nB 0.625  0.375   0.625    1  0.5    1\r\nC 0.375  0.625   0.375    1  0.5    1\r\n\r\n> flux(pattern4)\r\n   pobs influx outflux      ainb      aout      fico\r\nA 0.750  0.125   0.500 0.5000000 0.3333333 0.6666667\r\nB 0.625  0.250   0.375 0.6666667 0.3000000 0.6000000\r\nC 0.625  0.375   0.625 1.0000000 0.5000000 0.6000000\r\n```\r\n\r\nI agree with you that `ainb` and `aout` are easier to interpret. As a bonus, the `NaN`'s have gone. \r\n\r\nI will incorporate your suggestion into `mice`. Thanks!\r\n"}}
{"actor":{"display_login":"stefvanbuuren"}}