{"action":"created","issue":{"url":"https://api.github.com/repos/stefvanbuuren/mice/issues/131","repository_url":"https://api.github.com/repos/stefvanbuuren/mice","labels_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/131/labels{/name}","comments_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/131/comments","events_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/131/events","html_url":"https://github.com/stefvanbuuren/mice/issues/131","id":344758264,"node_id":"MDU6SXNzdWUzNDQ3NTgyNjQ=","number":131,"title":"Return probabilities instead of values with an ordinal variable","user":{"login":"ThomasWilli","id":7429228,"node_id":"MDQ6VXNlcjc0MjkyMjg=","avatar_url":"https://avatars0.githubusercontent.com/u/7429228?v=4","gravatar_id":"","url":"https://api.github.com/users/ThomasWilli","html_url":"https://github.com/ThomasWilli","followers_url":"https://api.github.com/users/ThomasWilli/followers","following_url":"https://api.github.com/users/ThomasWilli/following{/other_user}","gists_url":"https://api.github.com/users/ThomasWilli/gists{/gist_id}","starred_url":"https://api.github.com/users/ThomasWilli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ThomasWilli/subscriptions","organizations_url":"https://api.github.com/users/ThomasWilli/orgs","repos_url":"https://api.github.com/users/ThomasWilli/repos","events_url":"https://api.github.com/users/ThomasWilli/events{/privacy}","received_events_url":"https://api.github.com/users/ThomasWilli/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-26T09:08:28Z","updated_at":"2018-07-26T10:12:49Z","closed_at":null,"author_association":"NONE","body":"Hi!\r\n\r\nThank you for your great package, it is of great use! I do have a question though. I'm adding a method for imputation to mice (or am trying to). I'd like it to return the probabilities for each answer category (there are five). I thought that I'd just modify your function, however, things seem more complicated... \r\n\r\nFirst, some example data:\r\n    \r\n    df1 <- data.frame(x1=rnorm(100, 0, 1),\r\n                  x2=as.factor(sample( letters[1:5], 100, replace=T)),\r\n                  x3=runif(100, 0, 1),\r\n                  x4=as.factor(sample(letters[1:5], 100, replace=T)),\r\n                  u=factor(sample( letters[1:5], 100, replace=T), levels=c(\"a\", \"b\", \"c\", \"d\", \"e\"), ordered=T))\r\n    set.seed(1)\r\n    create.na <- sample( 1:nrow(df1), 25)\r\n    df1$u[create.na] <- NA\r\n\r\nSecond, I changed the code from the package accordingly: \r\n\r\n    mice.impute.rfown <- function(y, ry, x, wy = NULL, ntree = 10, ...){\r\n         if (is.null(wy)) wy <- !ry\r\n         if (!requireNamespace(\"randomForest\", quietly = TRUE))\r\n         stop(\"Package 'randomForest' needed fo this function \r\n         to work. Please install it.\",  call. = FALSE)\r\n  \r\n      onetree <- function(xobs, xmis, yobs, ...){\r\n           fit <- randomForest::randomForest(x = xobs, y = yobs, ntree = 1, ...)\r\n    \r\n         probs <- predict(object = fit, newdata = xmis, type=\"prob\")\r\n         return(probs)\r\n          }\r\n\r\n    ntree <- max(1, ntree)  # safety\r\n    nmis <- sum(wy)\r\n    xobs <- x[ry, , drop = FALSE]\r\n    xmis <- x[wy, , drop = FALSE]\r\n    yobs <- y[ry]\r\n    \r\n    forest <- sapply(seq_len(ntree), FUN = function(s) onetree(xobs, xmis, yobs))\r\n    pr.probs <- Reduce(\"+\", forest)/10\r\n    return(pr.probs)\r\n    }\r\n\r\n\r\n    micerf <- mice(df1, method=\"rfown\")\r\n\r\n    container <- as.data.frame(micerf$imp$u)\r\n    head(container)\r\n\r\n           1    2    3    4    5\r\n     4   <NA> <NA> <NA> <NA> <NA>\r\n     5   <NA> <NA> <NA> <NA> <NA>\r\n     7   <NA> <NA> <NA> <NA> <NA>\r\n     10  <NA> <NA> <NA> <NA> <NA>\r\n\r\n\r\nIf I print steps from my imputation function to the console, it prints correctly (meaning not `NA`s but values). It seems that `mice` is not able to return more data than observed missings? How can I work around this?\r\n\r\nThanks! \r\n"},"comment":{"url":"https://api.github.com/repos/stefvanbuuren/mice/issues/comments/408048748","html_url":"https://github.com/stefvanbuuren/mice/issues/131#issuecomment-408048748","issue_url":"https://api.github.com/repos/stefvanbuuren/mice/issues/131","id":408048748,"node_id":"MDEyOklzc3VlQ29tbWVudDQwODA0ODc0OA==","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-07-26T10:12:49Z","updated_at":"2018-07-26T10:12:49Z","author_association":"OWNER","body":"`mice` expects that the type of data returned by the `mice.impute.rfown()` function is the same as the type of the `y` variable. Here you are returning a numeric variable, and `mice` tries to stuff that into an ordered factor. `mice` cannot do that."}}
{"actor":{"display_login":"stefvanbuuren"}}