Skip to content

Commit 5b8e6db

Browse files
committed
Fixing summary.diffnet() for multi-diff
1 parent 1c47417 commit 5b8e6db

File tree

2 files changed

+51
-47
lines changed

2 files changed

+51
-47
lines changed

R/diffnet-methods.r

+51-42
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,13 @@ summary.diffnet <- function(
209209
# Computing density
210210
d <- unlist(lapply(object$graph[slices], function(x) {
211211
nlinks(x)/nnodes(x)/(nnodes(x)-1)
212-
# nelements <- length(x@x)
213-
# x <-nelements/(meta$n * (meta$n-1))
214212
}))
215213

216214
# identify single-diff from multi-diff
217215
single <- !inherits(object$cumadopt, "list")
218216

219-
# Computing moran's I
220217
if (single) {
218+
# Computing moran's I
221219
if (!skip.moran) {
222220
m <- matrix(NA, nrow=length(slices), ncol=4,
223221
dimnames = list(NULL, c("moran_obs", "moran_exp", "moran_sd", "moran_pval")))
@@ -230,6 +228,7 @@ summary.diffnet <- function(
230228

231229
m[i,] <- unlist(moran(object$cumadopt[,slices[i]], g))
232230
}
231+
}
233232

234233
# Computing new adopters, cumadopt and hazard rate
235234
ad <- colSums(object$adopt[,slices,drop=FALSE])
@@ -253,13 +252,16 @@ summary.diffnet <- function(
253252
}
254253

255254
if (no.print) return(out)
256-
}
257255

258256
} else {
259-
if (!skip.moran) {
260-
out_list <- list()
261-
data_beh_list <- list()
262-
for (q in 1:length(object$cumadopt)) {
257+
258+
out_list <- list()
259+
data_beh_list <- list()
260+
261+
for (q in 1:length(object$cumadopt)) {
262+
263+
if (!skip.moran) {
264+
#for (q in 1:length(object$cumadopt)) {
263265
m <- matrix(NA, nrow=length(slices), ncol=4,
264266
dimnames = list(NULL, c("moran_obs", "moran_exp", "moran_sd", "moran_pval")))
265267

@@ -270,42 +272,42 @@ summary.diffnet <- function(
270272

271273
m[i,] <- unlist(moran(object$cumadopt[[q]][,slices[i]], g))
272274
}
275+
#}
276+
}
273277

274-
# Computing new adopters, cumadopt and hazard rate
275-
ad <- colSums(object$adopt[[q]][,slices,drop=FALSE])
276-
ca <- t(cumulative_adopt_count(object$cumadopt[[q]]))[slices,-3, drop=FALSE]
277-
hr <- t(hazard_rate(object$cumadopt[[q]], no.plot = TRUE))[slices,,drop=FALSE]
278-
279-
# Left censoring
280-
lc <- sum(object$toa[,q] == meta$pers[1], na.rm = TRUE)
281-
rc <- sum(is.na(object$toa[,q]), na.rm=TRUE)
282-
283-
#data_beh_list[[q]] <- list(ad, ca, hr, lc, rc)
278+
# Computing new adopters, cumadopt and hazard rate
279+
ad <- colSums(object$adopt[[q]][,slices,drop=FALSE])
280+
ca <- t(cumulative_adopt_count(object$cumadopt[[q]]))[slices,-3, drop=FALSE]
281+
hr <- t(hazard_rate(object$cumadopt[[q]], no.plot = TRUE))[slices,,drop=FALSE]
284282

285-
out <- data.frame(
286-
adopt = ad,
287-
cum_adopt = ca[,1],
288-
cum_adopt_pcent = ca[,2],
289-
hazard = hr,
290-
density=d
291-
)
283+
# Left censoring
284+
lc <- sum(object$toa[,q] == meta$pers[1], na.rm = TRUE)
285+
rc <- sum(is.na(object$toa[,q]), na.rm=TRUE)
292286

293-
if (!skip.moran) {
294-
out <- cbind(out, m)
295-
}
287+
data_beh_list[[q]] <- c(lc, rc)
296288

297-
if (no.print) return(out)
289+
out <- data.frame(
290+
adopt = ad,
291+
cum_adopt = ca[,1],
292+
cum_adopt_pcent = ca[,2],
293+
hazard = hr,
294+
density=d
295+
)
298296

299-
out_list[[q]] <- out
297+
if (!skip.moran) {
298+
out <- cbind(out, m)
300299
}
300+
301+
out_list[[q]] <- out
301302
}
303+
304+
if (no.print) return(out_list)
302305
}
303306

304307
# Function to print data.frames differently
305308
header <- c(" Period "," Adopters "," Cum Adopt. (%) ",
306309
" Hazard Rate "," Density ",
307-
if (!skip.moran) c(" Moran's I (sd) ") else NULL
308-
)
310+
if (!skip.moran) c(" Moran's I (sd) ") else NULL)
309311

310312
slen <- nchar(header)
311313
hline <- paste(sapply(sapply(slen, rep.int, x="-"), paste0, collapse=""),
@@ -315,27 +317,34 @@ summary.diffnet <- function(
315317
# Quick Formatting function
316318
qf <- function(x, digits=2) sprintf(paste0("%.",digits,"f"), x)
317319

320+
# Start printing result
318321
cat("Diffusion network summary statistics\n",
319-
"Name : ", meta$name, "\n",
320-
"Behavior : ", meta$behavior, "\n",
321-
rule,"\n",sep="")
322-
cat(header,"\n")
323-
cat(hline, "\n")
322+
"Name : ", meta$name, "\n")
324323

325324
if (single) {
325+
cat(" Behavior : ", meta$behavior, "\n",
326+
rule,"\n",sep="")
327+
cat(header,"\n")
328+
cat(hline, "\n")
326329
summary_diffnet_out_display(out, slen, meta, slices, qf, skip.moran)
330+
cat(rule, "\n",
331+
paste("Left censoring :", sprintf("%3.2f (%d)", lc/meta$n, lc)), "\n",
332+
paste("Right centoring :", sprintf("%3.2f (%d)", rc/meta$n, rc)), "\n")
327333
} else {
334+
beh_names <- strsplit(meta$behavior, ", ")[[1]]
328335
for (q in 1:length(object$cumadopt)) {
336+
cat("\n Behavior : ", beh_names[q], "\n",
337+
rule,"\n",sep="")
338+
cat(header,"\n")
339+
cat(hline, "\n")
329340
summary_diffnet_out_display(out_list[[q]], slen, meta, slices, qf, skip.moran)
341+
cat(rule, "\n",
342+
paste("Left censoring :", sprintf("%3.2f (%d)", lc/meta$n, data_beh_list[[q]][1])), "\n",
343+
paste("Right centoring :", sprintf("%3.2f (%d)", rc/meta$n, data_beh_list[[q]][2])), "\n")
330344
}
331345
}
332346

333-
# print(out, digits=2)
334-
335347
cat(
336-
rule,
337-
paste(" Left censoring :", sprintf("%3.2f (%d)", lc/meta$n, lc)),
338-
paste(" Right centoring :", sprintf("%3.2f (%d)", rc/meta$n, rc)),
339348
paste(" # of nodes :", sprintf("%d",meta$n)),
340349
"\n Moran's I was computed on contemporaneous autocorrelation using 1/geodesic",
341350
" values. Significane levels *** <= .01, ** <= .05, * <= .1.",

R/rdiffnet.r

-5
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,6 @@ rdiffnet <- function(
474474
if (length(whoadopts) > 0) {
475475
toa[cbind(whoadopts, q)] <- i
476476
}
477-
#toa[cbind(whoadopts, q)] <- i
478-
# toa[, q] <- apply(cumadopt[,, q], 1, function(x) {
479-
# first_adopt <- which(x == 1)
480-
# if (length(first_adopt) > 0) first_adopt[1] else NA
481-
# })
482477
}
483478

484479
# 3.5 identifiying the disadopters

0 commit comments

Comments
 (0)