权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>forecasting - Plotting Predicted Mortality Rates for Calendar Years 1990–2020 with Lee-Carter Model in R - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

forecasting - Plotting Predicted Mortality Rates for Calendar Years 1990–2020 with Lee-Carter Model in R - Stack Overflow

programmeradmin9浏览0评论

I'm working on fitting a Poisson Lee-Carter model to the Danish male population data for calendar years 1990–2010 and ages 20–90 in R. After fitting the model, I want to plot the predicted mortality rates $\hat{\mu}_t^{L-C,(1)}(x)$ for the years 1990–2020 for ages 20, 40, 60, 75, and 90. However, I am struggling to plot the predictions for the calendar years 1990–2010, which were used for fitting the model.

The code below plots the predicted mortality rates for 2011–2020, but I need help on how to plot the predicted rates for the years 1990–2010 as well, since those years were used to train the model. How can I extend the plot to include the predicted values for the years 1990–2010, while keeping the raw mortality rates for comparison?

Here is the R code I have so far:

# Load necessary libraries
library("devtools")
library("gnm")
library("forecast")
library("StMoMo")
library("cobs")
library("ftsa")
library("demography")

# Read in the Swedish mortality data
MortalityDataSWE <- read.demogdata("SWE_Mx_1x1.txt", "SWE_Exposures_1x1.txt", type = "mortality", label = "Sweden")

# Prepare the data using the StMoMo package for female mortality series
StMoMoSWE <- StMoMoData(MortalityDataSWE, series = "female")

# Define the LC model with a log-link function
mortalityFunc <- lc(link = "log")

# Define the age and year ranges for fitting the model
fitAges <- 20:90
fitYrs <- 1990:2010
totYrs <- 1990:2020
# Calculate observed death rates
obsDeathRateSWE <- StMoMoSWE$Dxt[as.character(fitAges), as.character(totYrs)] / StMoMoSWE$Ext[as.character(fitAges), as.character(totYrs)]
# Fit the LC model
LCfitSWE <- fit(mortalityFunc, data = StMoMoSWE, ages.fit = fitAges, years.fit = fitYrs)
# Simulate the model to get forecasts for 2011-2020
forecastLC <- simulate(LCfitSWE, years = 2011:2020)
# Get residuals from the fitted model
residualsLC <- residuals(LCfitSWE)
# Calculate mean and percentiles (5% and 95%) for the simulated mortality rates
meanMortality <- apply(forecastLC$rates, c(1, 2), mean)
lowerBound <- apply(forecastLC$rates, c(1, 2), quantile, probs = 0.05)
upperBound <- apply(forecastLC$rates, c(1, 2), quantile, probs = 0.95)
# Define the ages to analyze
selectedAges <- c(20, 40, 60, 75, 90)
years <- 2011:2020

# Extract the predicted mortality rates and percentiles for the selected ages
meanMortalitySelected <- meanMortality[as.character(selectedAges), ]
lowerBoundSelected <- lowerBound[as.character(selectedAges), ]
upperBoundSelected <- upperBound[as.character(selectedAges), ]

# Subset the predicted data to match the years range (2011-2020)
meanMortalitySelected <- meanMortalitySelected[, as.character(years)]
lowerBoundSelected <- lowerBoundSelected[, as.character(years)]
upperBoundSelected <- upperBoundSelected[, as.character(years)]

# Get the raw observed death rates (if available)
rawMortalityRates <- obsDeathRateSWE[as.character(selectedAges), as.character(years)]
# Plot for Age 20
plot(years, meanMortalitySelected["20", ], type = "l",
     ylim = range(c(meanMortalitySelected["20", ], lowerBoundSelected["20", ], upperBoundSelected["20", ], rawMortalityRates["20", ]), na.rm = TRUE),
     col = "blue", lwd = 2, main = "Age: 20")
lines(years, lowerBoundSelected["20", ], col = "red", lty = 2)
lines(years, upperBoundSelected["20", ], col = "red", lty = 2)
if (!is.null(rawMortalityRates)) {
  points(years, rawMortalityRates["20", ], col = "black", pch = 16)
}
legend("topright", legend = c("Mean Prediction", "5% and 95% Percentiles", "Raw Rates"),
       col = c("blue", "red", "black"), lty = c(1, 2, NA), pch = c(NA, NA, 16), bty = "n")

Then I have one plot for each age (here I only show the one for age 20). The datasets are from mortality and are called SWE_Mx_1x1 and SWE_Exposures_1x1.

I would appreciate any suggestions or solutions for plotting the predicted mortality rates for the years 1990–2010, so I can compare them with the raw mortality rates for that period. Thanks!

Note: I have tried to simulate for years = 1990:2020 (forecastLC <- simulate(LCfitSWE, years = 2011:2020)) but i still only get the first forecast at year 2011, which i am guessing is since we are fitting the model with the years 1990-2010.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论