Skip to content

Bootstrap encounter histories data

3 messages · Simone Santoro, Rui Barradas

#
Hello,

It doesn't seem very complicated.
First of all, for the function fun below to work, you need the data not 
as strings of staes followed by a space followed by a broup number, but 
in two columns vectors, a character vector of states and a vector of 
groups. The vector of groups can be of class character, factor or 
numeric. I've written a function to simulate such data.


makeData <- function(n, g = 3, size = 10){
	res <- matrix(0, nrow = n, ncol = size)
	gr <- sample(g, n, replace = TRUE)
	for(i in seq_len(n))
		res[i, ] <- sample(0:2, 10, replace = TRUE, prob = c(0.5, 0.25, 0.25))
	res <- apply(res, 1, paste0, collapse = "")
	data.frame(states = res, group = gr, stringsAsFactors = FALSE)
}

dat <- makeData(10)

# Now to sample from 'dat', by group.
fun <- function(x){
	f <- function(y){
		idx <- sample(nrow(y), nrow(y), replace = TRUE)
		y[idx, ]
	}
	res <- do.call(rbind, lapply(split(x, x[, 2]), f))
	rownames(res) <- seq_len(nrow(res))
	res
}

fun(dat)


Hope this helps,

Rui Barradas

Em 14-03-2013 10:25, Simone Santoro escreveu:
#
Hello,

One more thing, if you have the data as strings of states/space/group, 
you can split it in vectors state/group with

# This is your data example
x <- c(
"1100222101 1",
"0000020010 3",
"0010101022 2"
)

mat <- do.call(rbind, strsplit(x, " "))


But this creates a matrix, so you'll need to revise the function as

fun <- function(x){
	f <- function(y){
		idx <- sample(NROW(y), NROW(y), replace = TRUE)
		y[idx, ]
	}
	sp <- split(as.data.frame(x), x[, 2])
	res <- do.call(rbind, lapply(sp, f))
	rownames(res) <- seq_len(nrow(res))
	res
}

fun(mat)


Hope this helps,

Rui Barradas

Em 14-03-2013 11:34, Rui Barradas escreveu: