The wonderful package ggdag can easily make DAG like this:
However, what we really want to include in publications is something like this:
The second one can include subscript and superscript, among many others. After some tweaking, I found a solution, not perfect but usable for now.
-----------------------------------------------------------------
library(dagitty)
library(ggdag)
library(ggraph)
library(cowplot)
library(dplyr)
```{r, echo=FALSE}
dag <- dagify(Y1 ~ X + Z1 + Z0 + U + P,
Y0 ~ Z0 + U,
X ~ Y0 + Z1 + Z0 + P,
Z1 ~ Z0,
P ~ Y0 + Z1 + Z0,
exposure = "X",
outcome = "Y1")
dag %>%
tidy_dagitty(layout = "auto", seed = 12345) %>%
arrange(name) %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point() +
geom_dag_edges() +
geom_dag_text(parse = TRUE, label = c("P", "U", "X", expression(Y[0]), expression(Y[1]), expression(Z[0]), expression(Z[1]))) +
theme_dag()
-----------------------------------------------------------------
Here the trick is to sort the tidy version of the DAG data by "name", then we can assign labels by the order of the name of the nodes. I hope a more automated approach can be developed in the future.
By the way, with the package latex2exp, it is straightforward to use LaTeX instead of plotmath commands.
Showing posts with label R. Show all posts
Showing posts with label R. Show all posts
Tuesday, October 01, 2019
Sunday, March 24, 2019
Play with the cyphr package
The cyphr package seems to provide a good choice for small research group that shares sensitive data over internet (e.g., DropBox). I did some simple experiment myself and made sure it can actually serve my purpose.
I did my experiment on two computers (using openssl): I created the test data on my Linux workstation running Manjaro then I tried to access the data on a Windows 7 laptop.
For creating the data (Linux workstation):
library(cyphr)
# Create the test data
data_dir <- file.path("~/Dropbox/temp_files", "data")
dir.create(data_dir)
dir(data_dir)
# Encrypt the test data
cyphr::data_admin_init(data_dir)
key <- cyphr::data_key(data_dir)
filename <- file.path(data_dir, "iris.rds")
cyphr::encrypt(saveRDS(iris, filename), key)
dir(data_dir)
# Cannot read the data without decrypting it first
readRDS(filename)
# Read the decrypted version of the data
head(cyphr::decrypt(readRDS(filename), key))
For accessing the data (Windows laptop):
library(cyphr)
key <- data_key("C:/Users/Ssong/Dropbox/temp_files/data", path_user = "C:/Users/Ssong/.ssh")
# Make data access request
data_request_access("C:/Users/Ssong/Dropbox/temp_files/data",
path_user = "C:/Users/Ssong/.ssh")
On Windows 7, the system cannot locate the public located in "~/.ssh", which is pretty dumb.
Going back to the Linux workstation to approve the data access request:
# Review the request and approve (to share with other users)
req <- data_admin_list_requests(data_dir)
data_admin_authorise(data_dir, yes = TRUE)
data_admin_list_keys(data_dir)
Now I can access the data on my Windows laptop:
key <- data_key("C:/Users/Ssong/Dropbox/temp_files/data", path_user = "C:/Users/Ssong/.ssh")
d <- decrypt( readRDS( "C:/Users/Ssong/Dropbox/temp_files/data/iris.rds"), key)
I did my experiment on two computers (using openssl): I created the test data on my Linux workstation running Manjaro then I tried to access the data on a Windows 7 laptop.
For creating the data (Linux workstation):
library(cyphr)
# Create the test data
data_dir <- file.path("~/Dropbox/temp_files", "data")
dir.create(data_dir)
dir(data_dir)
# Encrypt the test data
cyphr::data_admin_init(data_dir)
key <- cyphr::data_key(data_dir)
filename <- file.path(data_dir, "iris.rds")
cyphr::encrypt(saveRDS(iris, filename), key)
dir(data_dir)
# Cannot read the data without decrypting it first
readRDS(filename)
# Read the decrypted version of the data
head(cyphr::decrypt(readRDS(filename), key))
library(cyphr)
key <- data_key("C:/Users/Ssong/Dropbox/temp_files/data", path_user = "C:/Users/Ssong/.ssh")
# Make data access request
data_request_access("C:/Users/Ssong/Dropbox/temp_files/data",
path_user = "C:/Users/Ssong/.ssh")
On Windows 7, the system cannot locate the public located in "~/.ssh", which is pretty dumb.
Going back to the Linux workstation to approve the data access request:
# Review the request and approve (to share with other users)
req <- data_admin_list_requests(data_dir)
data_admin_authorise(data_dir, yes = TRUE)
data_admin_list_keys(data_dir)
Now I can access the data on my Windows laptop:
key <- data_key("C:/Users/Ssong/Dropbox/temp_files/data", path_user = "C:/Users/Ssong/.ssh")
d <- decrypt( readRDS( "C:/Users/Ssong/Dropbox/temp_files/data/iris.rds"), key)
Monday, July 10, 2017
Thursday, January 14, 2016
R Users Will Now Inevitably Become Bayesians
Good post here. I would also add that the rethinking package is a third option that helps R users to become a Bayesian.
Sunday, June 14, 2015
How to use SparkR within Rstudio?
Setting up Spark and SparkR is quite easy (assume you are running v.1.4): just grab one of the pre-built binaries and unzip to a folder. There is also a shell script to start SparkR from command line. The document suggest to put the following lines
Sys.setenv(SPARK_HOME="/home/shige/bin/spark")
.libPaths(c(file.path(Sys.getenv("SPARK_HOME"), "R", "lib"), .libPaths()))
library(SparkR)
sc <- sparkR.init(master="local")
into the .Rprofile file. This, however, has the undesirable side effect of adding yet another directory to which R packages can be installed.
My solution is:
1. Create a soft link of the SparkR directory in the the directory where other R packages are installed (ln -s /home/shige/bin/spark/R/lib/ SparkR /home/shige/R/x86_64-pc-linux- gnu-library/3.2)
2. Add (Sys.setenv(SPARK_HOME="/home/shige/bin/spark")) to the .Rprofile file.
3. Add (Sys.setenv(SPARKR_SUBMIT_ARGS ='"--packages" "com.databricks:spark-csv_2.10:1.0.3" "sparkr-shell"')) to the .Rprofile.
All set.
Sys.setenv(SPARK_HOME="/home/shige/bin/spark")
.libPaths(c(file.path(Sys.getenv("SPARK_HOME"), "R", "lib"), .libPaths()))
library(SparkR)
sc <- sparkR.init(master="local")
into the .Rprofile file. This, however, has the undesirable side effect of adding yet another directory to which R packages can be installed.
My solution is:
1. Create a soft link of the SparkR directory in the the directory where other R packages are installed (ln -s /home/shige/bin/spark/R/lib/
2. Add (Sys.setenv(SPARK_HOME="/home/shige/bin/spark")) to the .Rprofile file.
3. Add (Sys.setenv(SPARKR_SUBMIT_ARGS ='"--packages" "com.databricks:spark-csv_2.10:1.0.3" "sparkr-shell"')) to the .Rprofile.
All set.
Tuesday, March 03, 2015
Sunday, November 09, 2014
R is now the #12 most popular programming language
According to the most recent TIOBE index, R is the #12 most popular programming language.
Thursday, September 13, 2012
R2MLwiN package
The new package R2MLwiN package bridges R and MLwinN software for multilevel analysis. From the examples provided, it looks very promising. It will be great if a command line version of MLwiN can be made available under Linux so the package can be useful to everybody. Also, it will be great if a similar package for Mplus can be developed in the future.
There is also a Stata package that connects MLwiN with Stata. A quick glance suggests that the two packages have similar functions.
There is also a Stata package that connects MLwiN with Stata. A quick glance suggests that the two packages have similar functions.
Monday, February 20, 2012
Sunday, January 22, 2012
Some Rcpp benchmarks
I ran the Fibonacci number example from the Rcpp package on a number of computers and operating systems. Here are the results:
test replications elapsed relative user.self sys.self
3 fibRcpp(N) 1 0.17 1.0000 0.17 0.00
1 fibR(N) 1 73.62 433.0588 73.47 0.00
2 fibRC(N) 1 74.27 436.8824 74.20 0.03
D. On the same computer running Revolution R Enterprise 5:
test replications elapsed relative user.self sys.self
2 fibRC(N) 1 72.31 1.000000 72.09 0
1 fibR(N) 1 72.99 1.009404 72.79 0
E. On my third laptop (Core 2 Duo 2.50GHz, 2 GB memory) running Mint Debian (g++ 4.6.2):
Why the faster computer performed worse, on both R and Rcpp versions?
A. On my main computer (Core 2 Extreme 3.06GHz, 8 GB memory) running Ubuntu 10.04 (g++ 4.4.3):
test replications elapsed relative user.self sys.self
3 fibRcpp(N) 1 0.148 1.0000 0.14 0.01
1 fibR(N) 1 87.078 588.3649 87.03 0.04
2 fibRC(N) 1 91.209 616.2770 91.14 0.07
B. Same computer running Windows Vista (g++ 4.5.0):
test replications elapsed relative user.self sys.self
3 fibRcpp(N) 1 0.21 1.0000 0.21 0.00
1 fibR(N) 1 92.08 438.4762 90.47 0.05
2 fibRC(N) 1 94.39 449.4762 93.13 0.03
C. On my second laptop (Core 2 Duo 2.53GHz, 4 GB memory) running Windows 7 (g++ 4.5.0):
test replications elapsed relative user.self sys.self
3 fibRcpp(N) 1 0.21 1.0000 0.21 0.00
1 fibR(N) 1 92.08 438.4762 90.47 0.05
2 fibRC(N) 1 94.39 449.4762 93.13 0.03
C. On my second laptop (Core 2 Duo 2.53GHz, 4 GB memory) running Windows 7 (g++ 4.5.0):
test replications elapsed relative user.self sys.self
3 fibRcpp(N) 1 0.17 1.0000 0.17 0.00
1 fibR(N) 1 73.62 433.0588 73.47 0.00
2 fibRC(N) 1 74.27 436.8824 74.20 0.03
D. On the same computer running Revolution R Enterprise 5:
test replications elapsed relative user.self sys.self
2 fibRC(N) 1 72.31 1.000000 72.09 0
1 fibR(N) 1 72.99 1.009404 72.79 0
E. On my third laptop (Core 2 Duo 2.50GHz, 2 GB memory) running Mint Debian (g++ 4.6.2):
test replications elapsed relative user.self sys.self
3 fibRcpp(N) 1 0.148 1.0000 0.148 0.00
1 fibR(N) 1 65.535 442.8041 65.328 0.200
2 fibRC(N) 1 65.664 443.6757 65.492 0.172
Why the faster computer performed worse, on both R and Rcpp versions?
Thursday, January 12, 2012
Visual debugger and the debug mode of the autorun R console
The StatET team kept their promise and delivered the autorun R console with debug mode on. This, combined with the visual debugger, makes the StatET a very appealing cross-platform environment for working with R.
Saturday, October 29, 2011
SabreR
SabreR just released an update. It is another software package that can estimate multivariate multilevel model (other options are aML, MCMCglmm, etc.). They seem to also have a book dedicated to the software, which be worth checking out.
It will be great if the author can incorporate some plotting function into the package.
It will be great if the author can incorporate some plotting function into the package.
Saturday, October 08, 2011
Performance difference between Stata and R
With respect to multinomial logit model, the performance difference between the two packages are quite large, based on this post.
Tuesday, October 04, 2011
GEE using Stata vs. R
I am running GEE logistic regression model for my fetal loss paper. As usual, I compare results between Stata and R and make sure they are consistent. To my surprise, the models assuming independent correlation structure give similar results but the models assuming exchangeable correlation structure give drastically different results.
It turns out that there is only one woman in my sample who reported a total number of eleven pregnancies (all others reported ten or less) and the presence of this single observation had huge influence on the algorithm used in R but not the one used in Stata. After excluding this single observation, the two sets of results look identical.
It turns out that there is only one woman in my sample who reported a total number of eleven pregnancies (all others reported ten or less) and the presence of this single observation had huge influence on the algorithm used in R but not the one used in Stata. After excluding this single observation, the two sets of results look identical.
Saturday, October 01, 2011
Updated examples that combine Rcpp and CppBugs
This example is very informative in illustrating how to use CppBugs module with Rcpp to conduct fast MCMC simulation in R.
Sunday, August 28, 2011
Ra vs. compiler package
R seems to have two byte code compilers: the Ra add-on module (and the accompanying "jit" package) and the "compiler" package came with the default installation. I wonder how they differentiate from each other and what the strengths and weaknesses of each are.
Sunday, May 22, 2011
Legends in ggplot2
A simple plot takes a few lines of coding:
g1 <- ggplot(d, aes(birth.year))
g2 <- g1 + geom_line(aes(y=alive0, linetype="Famale")) +
geom_line(aes(y=alive1, linetype="Male")) + scale_linetype_discrete(name = "")
g3 <- g2 + geom_point(aes(y=alive0, shape="Famale")) +
geom_point(aes(y=alive1, shape="Male")) + scale_x_continuous("") + scale_y_continuous("Proportion Alive in 1982") + opts(legend.position=c(.24, .95), legend.justification = c(1, 1)) + scale_shape_discrete(name="")
g4 <- g3 + labs(fill="")
g1 <- ggplot(d, aes(birth.year))
g2 <- g1 + geom_line(aes(y=alive0, linetype="Famale")) +
geom_line(aes(y=alive1, linetype="Male")) + scale_linetype_discrete(name = "")
g3 <- g2 + geom_point(aes(y=alive0, shape="Famale")) +
geom_point(aes(y=alive1, shape="Male")) + scale_x_continuous("") + scale_y_continuous("Proportion Alive in 1982") + opts(legend.position=c(.24, .95), legend.justification = c(1, 1)) + scale_shape_discrete(name="")
g4 <- g3 + labs(fill="")
Wednesday, May 04, 2011
Extension to mtable function
Here are some useful extension to the "mtable" function in the memisc package.
Saturday, March 12, 2011
Ask R not to create a local directory tree
I don't like R to create a local directory tree in my home directory because new packages will automatically be installed into that directory. The way to do this is to modify the "/usr/local/lib64/R/etc/Renviron" and mark the line "R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-unknown-linux-gnu-library/2.12'}" off. Simple as that.
Monday, February 28, 2011
Rstudio
Rstudio, an open source cross-platform IDE for R, seems to be really attractive. It probably will not drag me away from Emacs, but having more options is always a good thing.
The only downside of it is that it automatically installs R from the Ubuntu software repository, even though I have already had a manually compiled version installed on the system. It will be better if Rstudio does not try to install a version of R as default but gives the choice to the user.
I think this IDE is an excellent choice for teaching purpose: by now the only reason I am hesitating to teach my students using R as opposed to other statistical package is the lack of a "modern" IDE. I mean, Emacs + ESS suites me just fine, but I cannot imagine what will happen if I try to make my undergraduate students learn to do this on their Windows machine!
After a few tweaks, and many thanks to the guys working on Rstudio, I was able to compile and install the source distribution.
Commands used:
The only downside of it is that it automatically installs R from the Ubuntu software repository, even though I have already had a manually compiled version installed on the system. It will be better if Rstudio does not try to install a version of R as default but gives the choice to the user.
I think this IDE is an excellent choice for teaching purpose: by now the only reason I am hesitating to teach my students using R as opposed to other statistical package is the lack of a "modern" IDE. I mean, Emacs + ESS suites me just fine, but I cannot imagine what will happen if I try to make my undergraduate students learn to do this on their Windows machine!
After a few tweaks, and many thanks to the guys working on Rstudio, I was able to compile and install the source distribution.
Commands used:
- git clone git@github.com:rstudio/rstudio.git
- git submodule update --init --recursive
- cmake -DRSTUDIO_TARGET=Desktop -DCMAKE_BUILD_TYPE=Release
- sudo make install
Subscribe to:
Posts (Atom)


