osofr / tmlenet Goto Github PK
View Code? Open in Web Editor NEWTargeted Maximum Likelihood Estimation for Network Data
Targeted Maximum Likelihood Estimation for Network Data
Line 485 in 13cfc44
This line references NETIDnode, which is not in this dataset. The variable name is Net_str in the data set. Is this supposed to remove the network IDs from the data set?
I loaded the package from Github (as not currently on CRAN) and the version is 0.1.9. For the function tmlenet, there are warnings when using IDnode and NETIDnode. The function runs without the warning if I use NETIDmat.
Warning messages:
1: In [.data.table
(OdataDT_R6$OdataDT, , :=
(NETIDnode, NULL)) :
Column 'NETIDnode' does not exist to remove
2: In [.data.table
(OdataDT_R6$OdataDT, , :=
(IDnode, NULL)) :
Column 'IDnode' does not exist to remove
For this call below (from '1_tmlenet_example.R'), produces warnings (7 total). One of the variables may be a constant, or input variables are not independent (need to check this).
res_K6_1 <- tmlenet(data = df_netKmax6, Kmax = Kmax, sW = sW, sA = sA,
intervene1.sA = def_new_sA(A = 0L),
# Anodes = "A", f_gstar1 = 0L,
Qform = "Y ~ sum.netW3 + sum.netAW2",
hform.g0 = "netA ~ netW2 + sum.netW3 + nF",
hform.gstar = "netA ~ sum.netW3",
NETIDmat = NetInd_mat_Kmax6 )
Warning messages:
1: In chol.default(B, pivot = TRUE) :
the matrix is either rank-deficient or not positive definite
Line 490 in 13cfc44
The column name is not in the dataset. Should be "IDs". Is this line supposed to remove the ID variable from the data set?
Hi @osofr and @frbl ,
I am following the R code snippets for simulating a network of connected units from the publication https://arxiv.org/pdf/1705.10376.pdf.
Nevertheless, I am getting some errors when using the tmlenet() function:
After specifying the def.sA and def.sW with their respective def_sA() and def_sW() functions and defining and meanW1, sumW2, sumW3, and sumA inside the functions I pass def.sA and def.sW as arguments in the tmlenet() function obtaining the following error:
Error in eval(parse(text = x, keep.source = FALSE)[[1L]]) : object 'sumA' not found
Looking at the help of the tmlenet() function I see that if the argument DatNet.ObsP0 is specified, the following arguments no longer need to be provided: data, Kmax, sW, sA, IDnode, NETIDnode, optPars$sep, NETIDmat. Nevertheless, when just DatNet.ObsP0 is specified I get the following error:
Error in update.intervention.sA(new.sA = intervene1.sA, sA = sA) : argument "sA" is missing, with no default
When I specify all the arguments DatNet.ObsP0, data, Kmax, sW, sA, IDnode, NETIDnode, optPars$sep, NETIDmat, intervene1.sA, Qform, and hform.g0, I get the following error:
Error in
$<-.data.frame(
tmp, "intervene1.sA", value = <environment>) : replacement has 26 rows, data has 500
The code I am using is:
# Installation
install.packages('tmlenet') # Not available at CRAN
devtools::install_github('osofr/tmlenet', build_vignettes = TRUE)
library(tmlenet)
library(simcausal)
#data(df_netKmax6)
#head(df_netKmax6)
# --------------------------------------
# 1. Generate the DAG and Network
# --------------------------------------
set.seed(54321)
D <- DAG.empty() +
network('net', netfun = 'rnet.SmWorld', dim = 1, nei = 3, p = 0.2) +
node('W1', distr = 'rcat.b0', probs = c(0.0494, 0.1823, 0.2806, 0.2680, 0.1651, 0.0546)) +
node('W2', distr = 'rbern', prob = plogis(-0.2 + W1/3)) +
node('W3', distr = 'rbern', prob = 0.6) +
node('A.obs', distr = 'rnorm', mean = 0.58*W2 + 0.33*W3, sd = 1) +
node('A', distr = 'rconst', const = A.obs) +
node('Y', distr = 'rbern',
prob = plogis(5 + -0.5*W1 - 0.58*W2 - 0.33*W3 +
-1.5*ifelse(nF < 0, sum(W1[[1:Kmax]])/nF, 0) +
-1.4*sum(W2[[1:Kmax]]) + 2.1*sum(W3[[1:Kmax]]) +
+0.35*A + 0.15*sum(A[[1:Kmax]])
),
replaceNAw0 = TRUE)
Dset <- set.DAG(D, n.test = 300)
#View(Dset)
# --------------------------------------
# 2. Simulation of network and observed data
# --------------------------------------
dat0 <- sim(Dset, n = 300, rndseed = 54321)
NetInd_mat <- attributes(dat0)$netind_cl$NetInd
nF <- attributes(dat0)$netind_cl$nF
# --------------------------------------
# 3. Graphic representation of network
# --------------------------------------
library('igraph')
g <- sparseAdjMat.to.igraph((NetInd.to.sparseAdjMat(NetInd_mat, nF = nF)))
plot.igraph(g,
vertex.size=5,
vertex.label.dist=0.5,
vertex.color="deeppink2",
edge.color = 'azure3',
edge.arrow.size=0.2)
# --------------------------------------
# 4. Defining intervention + conterfactuals
# --------------------------------------
# Dset <- Dset +
# action('gstar',
# nodes = node('A', distr = 'rconst',
# const = ifelse(A.obs - (0.58*W2 + 0.33*W3) > (log(trunc)/shift + shift/2),
# A.obs,
# A.obs + shift)),
# trunc = 1,
# shift = 0.3)
#
# require('doParallel')
# registerDoParallel(cores = detectCores())
# networksize <- 500; nsims_psi0 <- 10000
#
# psi0_reps <- foreach(i.sim = seq(nsims_psi0), .combine = 'c') %dopar% {
# dat_gstar <- sim(Dset, actions = 'gstar', n = networksize)[['gstar']]
# psi0 <- mean(dat_gstar[['Y']])
# }
#
# psi0 <- mean(psi0_reps)
# print(psi0)
# --------------------------------------
# 5. Comparing performance of dependent-data estimators
# --------------------------------------
dat0 <- sim(Dset, n = 500)
net_obj <- attributes(dat0)[['netind_cl']]
NetInd_mat <- net_obj[['NetInd']]
nF <- net_obj[['nF']]
Kmax <- net_obj[['Kmax']]
g <- sparseAdjMat.to.igraph((NetInd.to.sparseAdjMat(NetInd_mat, nF = nF)))
plot.igraph(g,
vertex.size=5,
vertex.label.dist=0.5,
vertex.color="deeppink2",
edge.color = 'azure3',
edge.arrow.size=0.2)
require('tmlenet')
def.sW <- def_sW(W1 = W1, W2 = W2, W3 = W3) +
def_sW(meanW1 = ifelse(nF > 0, sum(W1[[1:Kmax]])/nF, 0), replaceNAw0 = TRUE) +
def_sW(sumW2 = sum(W2[[1:Kmax]]), replaceNAw0 = TRUE) +
def_sW(sumW3 = sum(W3[[1:Kmax]]), replaceNAw0 = TRUE)
def.sA <- def_sA(A = A, sumA = sum(A[[1:Kmax]]), replaceNAw0 = TRUE)
trunc <- 1; shift <- 0.5
newA.gstar <- def_new_sA(A = ifelse(A - (0.58*W2 + 0.33*W3) > (log(trunc)/shift + shift/2),
A,
A + shift))
Qform <- 'Y ~ A + sumA + meanW1 + sumW2 + sumW3 + W1 + W2 + W3'
hform <- 'A + sumA + meanW1 + sumW2 + sumW3 + W1 + W2 + W3'
tmlenet_options(bin.method = 'equal.mass', maxNperBin = 40)
DatNet.ObsP0 <- eval.summaries(data = dat0, Kmax = Kmax, sW = def.sW, sA = def.sA, #IDnode = NULL, NETIDnode = NetInd_mat,
sep = " ", NETIDmat = NetInd_mat, verbose = getOption("tmlenet.verbose"))
# res <- tmlenet(data = dat0,
# sW = def.sW,
# sA = def.sA,
# Kmax = Kmax,
# NETIDmat = NetInd_mat,
# intervene1.sA = newA.gstar,
# Qform = Qform,
# hform.g0 = hform,
# verbose = TRUE,
# optPars = list(bootstrap.var = TRUE, n.bootstrap = 100))
res <- tmlenet(DatNet.ObsP0 = DatNet.ObsP0,
data = dat0,
sW = def.sW,
sA = def.sA,
Kmax = Kmax,
NETIDmat = NetInd_mat,
intervene1.sA = newA.gstar,
Qform = Qform,
hform.g0 = hform,
verbose = TRUE,
optPars = list(bootstrap.var = TRUE, n.bootstrap = 100))
res
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.