r - 代码成功运行,但没有图形作为输出
问题描述
我试图在巴基斯坦的案例中复制Fabian Dablander 的非线性感染工作。但是当我运行下面的代码时,它不会将任何图形作为输出”
plot_SIR <- function(res, main = '') {
cols <- brewer.pal(3, 'Set1')
matplot(
res, type = 'l', col = cols, axes = FALSE, lty = 1, lwd = 2,
ylab = 'Subpopulations(t)', xlab = 'Time t', xlim = c(0, 4000),
ylim = c(0, 1), main = main, cex.main = 1.75, cex.lab = 1.5,
font.main = 1, xaxs = 'i', yaxs = 'i'
)
axis(1, cex.axis = 1.25)
axis(2, las = 2, cex.axis = 1.25)
legend(
3000, 0.65, col = cols, legend = c('S', 'I', 'R'),
lty = 1, lwd = 2, bty = 'n', cex = 1.5
)
}
解决方案
快速浏览一下,您需要运行另一个函数来获取参数:res,然后将其粘贴在原始函数中,就像您在帖子中描述的那样:
solve_SIR <- function(S0, I0, beta = 1, gamma = 1, delta_t = 0.01, times = 8000) {
res <- matrix(
NA, nrow = times, ncol = 4, dimnames = list(NULL, c('S', 'I', 'R', 'Time'))
)
res[1, ] <- c(S0, I0, 1 - S0 - I0, delta_t)
dS <- function(S, I) -beta * I * S
dI <- function(S, I) beta * I * S - gamma * I
for (i in seq(2, times)) {
S <- res[i-1, 1]
I <- res[i-1, 2]
res[i, 1] <- res[i-1, 1] + delta_t * dS(S, I)
res[i, 2] <- res[i-1, 2] + delta_t * dI(S, I)
res[i, 4] <- delta_t * i
}
res[, 3] <- 1 - res[, 1] - res[, 2]
res
}
使用函数生成参数输入并为 S0 和 I0 选择随机值,这将输出您想要的图形。
plot_SIR(solve_SIR(0.95, 0.05))
推荐阅读
- html - 使用包含 / 键的 JSON 对象进行角度插值
- react-native - 以编程方式对本机 this.props 所有值做出反应
- android - Consent SDK“是,继续查看相关广告”按钮丢失,如何修复?
- javascript - 如何在 Vue.js 中用一种方法处理两个输入字段?
- html - 如何垂直对齐列,以使相邻行彼此不对齐
- firebase - angularFire2中的用户身份验证失败
- sql-server-2008 - 如何在 Sql Server 2008 中选择最后/最新编辑的行?
- javascript - 未调用 Selectize 渲染方法,并且我的选项未渲染
- android - 将 MP3 转换为 PCM 并在 Android 中播放
- python - NameError:名称“pigLatin”未定义