-
Notifications
You must be signed in to change notification settings - Fork 0
/
constant_p1_functions.R
98 lines (75 loc) · 2.69 KB
/
constant_p1_functions.R
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#Functions for the Solow and Costello (2004) models
count_pi_const <- function(S,i,params,const,pi1){
pi0 <- ifelse(test = is.na(params["gama0"]),yes = as.numeric(const["gama0"]),no = as.numeric(params["gama0"]))
pi1 <- as.numeric(pi1[i])
pi2 <- ifelse(test = is.na(params["gama2"]),yes = as.numeric(const["gama2"]),no = as.numeric(params["gama2"]))
num <- exp(pi0 + pi1 + pi2*exp(i-S))
f <- is.infinite(num) # if the number is infinity
pi_total <- num/(1+num)
pi_total[f] <- 1
return(pi_total)
}
count_p_const <- function(i,params,const,pi1){
# This function calculates the value p from Solow and Costello (2004)
# It uses matrix coding for efficiency
library(matrixStats)
library(pracma)
S <- repmat(c(1:i),i,1)
thing <- 1-count_pi_const(S,Conj(t(S)),params,const)
thing[i,] <- 1
up <- ones(size(thing)[1],size(thing)[2])
upperones <- triu(up,1) # upper triangular
thing2 <- tril(thing) + upperones
product <- colProds(as.matrix(thing2))
pst <- product*count_pi_const(c(1:i),i,params,const,pi1)
return(pst)
}
count_log_like_const<- function(first_record_data,params,const,pi1){
lambda <- vector(mode = "numeric", length = length(first_record_data))
summand2 <- vector(mode = "numeric", length = length(first_record_data))
for (i in seq_along(first_record_data)){
S <- 1:i;
Am <- count_m(S,params,const)
Ap <- count_p_const(i,params,const,pi1)
lambda[i] <- sum(Am * Conj(t(as.matrix(Ap))))
summand2[i] <- first_record_data[i]*log(lambda[i]) - lambda[i]
}
LL <- (-sum(summand2))
return(LL) # lambda
}
count_lambda_const <- function(N,params,const,pi1){
# This function calculates lambda from Solow and Costello, 2004.
# params is a vector of parameters
lambda<-vector(mode = "numeric",length = N)
for(i in 1:N){
S=c(1:i)
Am = count_m(S,params,const)
Ap = count_p_const(i,params,const,pi1)
lambda[i] = sum(Am * Conj(t(Ap)))
}
return(lambda)
}
get_p_component_const <- function(N,params,const,pi1){
# This function calculates lambda from Solow and Costello, 2004.
# params is a vector of parameters
lambda<-vector(mode = "numeric",length = N)
for(i in 1:N){
S=c(1:i)
Am = count_m(S,params,const)
lambda[i] = sum(count_p_const(i,params,const,pi1))
}
return(lambda)
}
sim_random_p <- function(N,params,const){
# This function calculates lambda from Solow and Costello, 2004.
# params is a vector of parameters
lambda<-vector(mode = "numeric",length = N)
for(i in 1:N){
S=c(1:i)
Am = count_m(S,params,const)
Ap = sample(seq(0,1,0.0001),1)
Yt = rbinom(Am,1,Ap)
lambda[i] = round(sum(Am * Conj(t(Yt))))
}
return(lambda)
}