负指数函数的跨站点全局优化
Global optimization across sites for negative exponential function
尝试估计适合整个研究的最佳参数。每项研究都有不同长度的数据集,需要了解在剩余变量和时间变量之间拟合的负指数模型的最佳参数是什么。示例数据集如下;
Study <- as.factor(c(1, 1, 1, 1, 2, 2, 2, 3, 3))
Time <- as.numeric(c(0, 0.08, 0.16, 0.24, 0, 0.05, 0.88, 0, 0.99))
Remaining <- as.numeric(c(100, 80, 69, 45, 100, 60, 35, 0, 25))
data_n <- cbind(Study, Time, Remaining)
head(data_n)
需要优化负指数函数可以计算出适合所有站点的K和K2。函数是
Predicted_remaining <- 41* exp(-k*Time) + (100-41) * exp(-k2*Time)
值41是一个常数,-k和-k2是需要优化的负指数函数的参数。尝试在 R 中进行优化,所以任何建议都是
非常感谢。
首先,我根据您的数据创建了一个数据框。接下来,我定义了将拟合数据的函数,它以拟合参数 p
和 time
作为参数。然后,我创建了一个成本函数,计算平方和作为拟合质量的指标。之后,我使用 optim
最小化成本函数,最后绘制结果。
# Create data frame
data_n <- data.frame(Study, Time, Remaining)
# Function to be fit
fit_function <- function(p, time){
(41 * exp(-p[1] * time) + 59 * exp(-p[2] * time))
}
# Cost function using the sum of squares
cost_function <- function(p, data){
sum((data$Remaining - fit_function(p, data$Time))^2)
}
# Using 'optim' to minimise the cost function
fit <- optim(c(1, 1), cost_function, data = data_n)
# Plot results
plot(data_n$Time, data_n$Remaining, xlab = "Time", ylab = "Remaining")
lines(seq(0, 1, by = 0.1), fit_function(fit$par, seq(0, 1, by = 0.1)))
尝试估计适合整个研究的最佳参数。每项研究都有不同长度的数据集,需要了解在剩余变量和时间变量之间拟合的负指数模型的最佳参数是什么。示例数据集如下;
Study <- as.factor(c(1, 1, 1, 1, 2, 2, 2, 3, 3))
Time <- as.numeric(c(0, 0.08, 0.16, 0.24, 0, 0.05, 0.88, 0, 0.99))
Remaining <- as.numeric(c(100, 80, 69, 45, 100, 60, 35, 0, 25))
data_n <- cbind(Study, Time, Remaining)
head(data_n)
需要优化负指数函数可以计算出适合所有站点的K和K2。函数是
Predicted_remaining <- 41* exp(-k*Time) + (100-41) * exp(-k2*Time)
值41是一个常数,-k和-k2是需要优化的负指数函数的参数。尝试在 R 中进行优化,所以任何建议都是 非常感谢。
首先,我根据您的数据创建了一个数据框。接下来,我定义了将拟合数据的函数,它以拟合参数 p
和 time
作为参数。然后,我创建了一个成本函数,计算平方和作为拟合质量的指标。之后,我使用 optim
最小化成本函数,最后绘制结果。
# Create data frame
data_n <- data.frame(Study, Time, Remaining)
# Function to be fit
fit_function <- function(p, time){
(41 * exp(-p[1] * time) + 59 * exp(-p[2] * time))
}
# Cost function using the sum of squares
cost_function <- function(p, data){
sum((data$Remaining - fit_function(p, data$Time))^2)
}
# Using 'optim' to minimise the cost function
fit <- optim(c(1, 1), cost_function, data = data_n)
# Plot results
plot(data_n$Time, data_n$Remaining, xlab = "Time", ylab = "Remaining")
lines(seq(0, 1, by = 0.1), fit_function(fit$par, seq(0, 1, by = 0.1)))