simulate_thining_single: simulate_thining_single

Usage Arguments Examples

View source: R/functions_burninthining.R

Usage

1
simulate_thining_single(partition, theta, nodes, effects, objects, num.steps, neighborhood, sizes.allowed, sizes.simulated, burnin, max.thining)

Arguments

partition

Observed partition.

theta

Initial model parameters

nodes

Data frame containing the nodes.

effects

Effects or sufficient statistics. A list with a vector "names" and a vector "objects".

objects

Objects used for statistics calculation. A list with a vector "name" and a vector "object".

num.steps

Number of samples wanted.

neighborhood

Way of choosing partitions: probability vector (probability actors swap, probability merge/division, probability single actor move).

sizes.allowed

Vector of group sizes allowed in sampling.

sizes.simulated

Vector of group sizes allowed in the Markov chain but not necessraily sampled.

burnin

Integer for the number of burn-in steps before sampling.

max.thining

Integer where to stop adding thining.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (partition, theta, nodes, effects, objects, num.steps,
    neighborhood, sizes.allowed, sizes.simulated, burnin, max.thining)
{
    num.effects <- length(effects$names)
    chain <- draw_Metropolis_single(theta, partition, nodes,
        effects, objects, burnin, 1, num.steps, neighborhood,
        sizes.allowed, sizes.simulated, return.all.partitions = F)
    ok <- F
    current.draws <- chain$draws
    current.last <- chain$last.partition
    current.thining <- 1
    allautocors <- c()
    while (!ok) {
        draws <- current.draws[seq(1, nrow(current.draws), current.thining),
            ]
        autocors <- rep(0, num.effects)
        for (e in 1:num.effects) {
            autocors[e] <- cor(draws[1:(num.steps - 1), e], draws[2:num.steps,
                e])
        }
        if (current.thining%%50 == 0) {
            print("thining")
            print(current.thining)
            print("autocorrelations")
            print(autocors)
        }
        allautocors <- rbind(allautocors, autocors)
        if (current.thining >= max.thining) {
            ok <- T
        }
        else {
            new.chain <- draw_Metropolis_single(theta, current.last,
                nodes, effects, objects, 1, 1, num.steps, neighborhood,
                sizes.allowed, sizes.simulated, return.all.partitions = F)
            current.draws <- rbind(current.draws, new.chain$draws)
            current.last <- new.chain$last.partition
            current.thining <- current.thining + 1
        }
    }
    smoothedautocors <- allautocors
    for (eff in 1:num.effects) {
        lo <- loess(y ~ x, data.frame(x = 1:max.thining, y = allautocors[,
            eff]))
        smoothedautocors[!is.na(smoothedautocors[, eff]), eff] <- lo$fitted
    }
    return(list(draws = current.draws, autocorrelations = allautocors,
        autocorrelations.smoothed = smoothedautocors))
  }

isci1102/ERPM documentation built on Jan. 18, 2022, 12:25 a.m.