-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteadyStateModel
More file actions
54 lines (42 loc) · 1.65 KB
/
SteadyStateModel
File metadata and controls
54 lines (42 loc) · 1.65 KB
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
# Simple steady-state model - supply t, a, Amax, and k; return A0
SteadyStateModel <- function(t, kdeg, A_0, A_Inf) {
y <- A_0 + (A_Inf - A_0) * (1 - exp(-kdeg*t))
return(y)
}
# Solves dK/dA analytically
SteadyStateModel_dk <- function(t,
kdeg,
A_0,
A_Inf,
dA) {
kdeg*exp(1)/(A_Inf-A_0)*dA
}
# This performs the curve-fitting
fitSteadyStateModel <- function(ts,
As,
A_0,
A_Inf,
weights = NULL){
# If not weighted, initialize a string of 1's
if (is.null(weights)){
weights <- rep(1, length(As))
}
# Weighted least square function
# Takes in A_0, A_Inf, RIA data at each time point, and returns optimized k
# var is k; A_0 is beginning and A_Inf is plateau; for FS fitting these are 0 and 1
penaltyFunction <- function(var){
predicted <- sapply(ts, function(x) SteadyStateModel(x, var, A_0, A_Inf))
sumsOfSquare <- sum((As-predicted)^2 * weights^2) / sum(weights^2) * length(weights)
return(sumsOfSquare)
}
Optimize <- optim(log(2)/2, penaltyFunction, method = "BFGS", control = list(maxit=100))
k <- Optimize$par
SS <- Optimize$value
SE <- (Optimize$value/(length(ts)-1))^0.5
# Calculate dk from dA
dk <- sapply(ts, function(x) SteadyStateModel_dk(x, Optimize$par, A_0, A_Inf, SE)) %>% abs() %>% min()
R2 <- 1- (SS/(sum((As - mean(As))^2)))
output <- c(k, SS, SE, dk, R2)
names(output) <- c("k", "SS", "SE", "dk", "R2")
return(output)
}