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

random vectors for multivariate explanatory variables #36

Open
akgandhi opened this issue Nov 25, 2020 · 8 comments
Open

random vectors for multivariate explanatory variables #36

akgandhi opened this issue Nov 25, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@akgandhi
Copy link

There does not seem to be an obvious mechanism to define random vectors in a DAG, for example, to use in a multivariate regression. Is there an option/ability to apply dag_plate to index over the X_i's in a multivariate random vector (where the underlying data object would then be a design matrix).

@flyaflya
Copy link
Owner

flyaflya commented Dec 1, 2020

Thanks Amit for the note. There is some support for this already, but it is non-obvious and under-supported with documentation at this moment. Maybe you can help guide my development with some use cases you would like to see?

In regards to current functionality, you can see a demonstration of design matrices in a DAG using the dev version of causact:

remotes::install_github("flyaflya/causact")

and looking at examples 3, 4, and 5 of this R-script: https://raw.githubusercontent.com/flyaflya/causact/master/gretaExamples.R

The DAG for example #3 is shown below.
Example DAG is shown below:
image

Let me know what you think. I can make a release from the DEV version so this capability is available to your students via CRAN.

@akgandhi
Copy link
Author

akgandhi commented Dec 1, 2020 via email

@akgandhi
Copy link
Author

akgandhi commented Dec 3, 2020

Hi
I am trying the code from "greta example #3" in the script provided above with the developer version of causact you recommended. In particular:

packageVersion("causact")
[1] ‘0.3.3.9000’

The graph is processed and generates a visual DAG (the same one you show above). However when trying to run the actual simulation the following error arises:

drawsDF = graph %>% dag_greta()
Error: objects of class function cannot be coerced to greta arrays

Does this error arise for you?

@flyaflya
Copy link
Owner

flyaflya commented Dec 3, 2020

My bad. The "int" node needs to be defined in the DAG. Without the "int" node, the code thinks the int in int + design %*% coefs is the function int. If you add the node, it works:

library(greta)
library(tidyverse)
library(causact)
### greta example #3
graph = dag_create() %>%
  dag_node("Rating","y",
           data = attitude$rating,
           rhs = normal(mu,sd)) %>%
  dag_node("Exp. Rating","mu",
           rhs = int + design %*% coefs,
           child = "y") %>%
  dag_node("Intercept","int",
           rhs = normal(0,10),
           child = "mu") %>%
  dag_node("Std Dev of Ratings","sd",
           rhs = cauchy(0,3, truncation = c(0,Inf)),
           child = "y") %>%
  dag_node("Predictor Coefficients","coefs",
           rhs = normal(0,10),
           child = "mu",
           extract = FALSE) %>%
  dag_node("Predictors","design",
           data = attitude[, 2:7],
           keepAsDF = TRUE,
           child = "mu") %>%
  dag_plate(descr = "Predictors",label = "j",
            nodeLabels = c("coefs"),
            data = names(attitude[, 2:7])) %>%
  dag_plate(descr = "Observation", label = "i",
            nodeLabels = c("mu","y"))

graph %>% dag_render()
graph %>% dag_render(shortLabel = TRUE)
drawsDF = graph %>% dag_greta()
drawsDF %>% dagp_plot()

I updated the gretaExamples file on github to reflect the change. Thanks for finding this bug.

@akgandhi
Copy link
Author

akgandhi commented Dec 4, 2020

This works! Thank you. Two quick follow up questions (happy to start a new issue if you think it more appropriate).

  1. are there any examples where the extract = TRUE option would arise (vs passing the index variable as a separate parent node to the common child).

  2. I see here you use the plate iterator to define a vector greta array for "coeffs". Could we equivalently have used an array valued greta distribution (e.g., multivariate normal) - will test out myself.

@flyaflya
Copy link
Owner

flyaflya commented Dec 5, 2020

  1. Most of my multi-level examples use extract=TRUE which is the default. This is not readily clear as I often add the index variable as a parent in these cases using dag_plate(..., addDataNode = TRUE). The dotted line in this DAG from the top of Chapter 25 (causact.com) indicates that extract=TRUE:
    image

other examples with extract = TRUE are shown in the readme file from CRAN: https://cran.r-project.org/web/packages/causact/readme/README.html

  1. not sure about this off the top of my head. let me know your findings :-)

@akgandhi
Copy link
Author

akgandhi commented Apr 1, 2021 via email

@flyaflya
Copy link
Owner

flyaflya commented Apr 1, 2021 via email

@flyaflya flyaflya added the enhancement New feature or request label Jun 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants