博文

使用INLAspacetime完成时空建模

library(sf) library(fdmr) library(fmesher) library(INLA) library(inlabru) library(INLAspacetime) library(tidyverse) # 1. 加载数据并转换为 sf 对象 sf_data % st_as_sf(coords = c("LONG", "LAT"), crs = 4326) %>% mutate(mapp = 0) covid19_sf % st_as_sf(coords = c("LONG", "LAT"), crs = 4326) # 2. 计算网格参数,sf_data 的 geometry 类型是 sfc_MULTIPOLYGON coords 1) = 0.1 ) ) components

基于扩散的时空Gaussian Matérn场扩展

# DEMF模型总结:基于扩散的时空Gaussian Matérn场扩展 ## 概述 DEMF(Diffusion-based Spatio-temporal Extension of Matérn Fields)是一种通过随机偏微分方程(SPDE)将Gaussian Matérn场扩展到时空领域的随机过程家族。文章定义了四种具体模型:DEMF(1,0,2)、DEMF(1,2,1)、DEMF(2,0,2)和DEMF(2,2,0)。 ## 模型参数与特征 ### 参数定义 所有DEMF模型通过六个参数定义: - **光滑度参数**:(αₜ, αₛ, αₑ) - 控制时间、空间和噪声过程特性 - **尺度参数**:(γₜ, γₛ, γₑ) - 控制时空相关范围 ### 四种模型对比 | 模型 | (αₜ, αₛ, αₑ) | 时间光滑度 νₜ | 空间光滑度 νₛ | 类型 | 可分性 | |------|---------------|---------------|---------------|------|--------| | DEMF(1,0,2) | (1,0,2) | 1/2 | 1 | 可分模型(1阶) | 可分 | | DEMF(1,2,1) | (1,2,1) | 1/2 | 1 | 临界扩散 | 非可分 | | DEMF(2,0,2) | (2,0,2) | 3/2 | 1 | 可分模型(2阶) | 可分 | | DEMF(2,2,0) | (2,2,0) | 1 | 2 | 迭代扩散 | 完全非可分 | ## 主要共同特征 ### 理论基础 - **SPDE定义**:所有模型基于统一的SPDE框架 - **空间边际**:均为Matérn协方差形式 - **物理可解释性**:基于扩散过程,适合环境/气候数据建模 ### 实现特点 - **R-INLA兼容**:通过有限元方法实现稀疏表示 - **全球应用**:均可用于全球温度数据等大规模时空建模 - **非欧空间支持**:可扩展到球面、流形、网络等复杂几何结构 ## 关键差异 ### 1. 可分性特征 **可分模型**(DEMF(1,0,2), DEMF(2,0,2)): - 协方差函数:R(hₛ, hₜ) = Rₛ(hₛ)Rₜ(hₜ) - 时间边际协方差为Matérn形式 -...

时空模型2

  ```{r} library ( INLA ) library ( inlabru ) library ( magrittr ) library ( sf ) library ( tidyverse ) sf_data <- readRDS ( 'data/spatial_data.rds' ) # 基于最早日期计算连续周数 st_covid <- readRDS ( 'data/st_covid.rds' ) |>   mutate ( week = as.numeric ( difftime ( date , min ( date , na.rm = TRUE ), units = "weeks" )) %/% 1 + 1 ) |>   left_join ( sf_data @ data |> dplyr :: select ( MSOA11CD , LONG , LAT ), by = "MSOA11CD" ) |>   drop_na ( cases , Population , IMD , perc.wb , perc.ba , age1 , pm25 ) |>   st_as_sf ( coords = c ( "LONG" , "LAT" )) ``` ```{r} coordinates <- st_coordinates ( st_covid )   # st_covid geometry 类型是 sfc_POINT locs <- unique ( coordinates ) # # 计算每个多边形的质心 # centroids <- st_centroid(sf_data) # locs <- st_coordinates(centroids) # 计算初始参数 initial_range <- diff ( range ( locs [, 1 ])) / 3 max_edge <- initial_range / 2 # 构建网格 mesh <- fm_mesh_2d_inla (   st_covid ,   ma...

拟合时空耦合模型

library ( sf ) library ( fdmr ) library ( fmesher ) library ( INLA ) library ( inlabru ) library ( tidyverse ) # 1. 加载数据并转换为 sf 对象 sf_data <- readRDS ( 'data/spatial_dataBris.rds' ) %>%   st_as_sf ( coords = c ( "LONG" , "LAT" ) , crs = 4326 ) %>%   mutate ( mapp = 0 ) covid19_sf <- readRDS ( 'data/covid19_dataBris.rds' ) %>%   st_as_sf ( coords = c ( "LONG" , "LAT" ) , crs = 4326 ) # 2. 计算网格参数,sf_data 的 geometry 类型是 sfc_MULTIPOLYGON coords <- st_coordinates ( sf_data ) initial_range <- diff ( range ( coords [ , "X" ])) / 3 max_edge <- initial_range / 2 # 3.mesh mesh <- fm_mesh_2d_inla ( sf_data ,   max.edge = c ( 1 , 2 ) * max_edge ,   offset = c ( initial_range , initial_range ) ,   cutoff = max_edge / 7 ) plot ( mesh ) # 5. 定义 SPDE 模型 prior_range <- initial_range spde <- INLA :: inla.spde2.pcmatern (   mesh = mesh ,   prior.range = c ( prior_...

inlabru 时空分析

library ( INLA ) # 用于之后的建模 library ( MASS ) # 用于真实的多元正态分布模拟 library ( tidyverse ) library ( inlabru ) library ( fdmr ) library ( sf ) library ( inlatools )   covid19_data <- fdmr :: load_tutorial_data ( dataset = "priors" , filename = "covid19_dataBris.rds" ) covid19_sf <- covid19_data |> st_as_sf ( coords = c ( "LONG" , "LAT" )) coordinates <- st_coordinates ( covid19_sf ) locs <- unique ( coordinates ) # 计算初始参数 initial_range <- diff ( range ( locs [, 1 ])) / 3 max_edge <- initial_range / 2 # 2. 构建改进的网格,mesh从地图构建,此处不对。 mesh <- fm_mesh_2d (   loc = locs ,   max.edge = c ( 1 , 2 ) * max_edge ,   offset = c ( initial_range , initial_range ),   cutoff = max_edge / 5 ) # 3. 定义SPDE模型 spde <- inla.spde2.pcmatern (   mesh = mesh ,   prior.range = c ( initial_range , 0.5 ),   prior.sigma = c ( 1 , 0.01 ) ) # 4. 设置时间趋势先验, rw1_prior <- list ( theta = list ( prior = "pc.prec" , param = c ( 1 , ...

计算工作日

 library(tidyverse) # 定义节假日和补班日 holidays_2024 <- as.Date(c(   "2024-01-01",  # 元旦   "2024-02-10", "2024-02-11", "2024-02-12", "2024-02-13", "2024-02-14", "2024-02-15", "2024-02-16",  # 春节   "2024-04-04", "2024-04-05", "2024-04-06",  # 清明节   "2024-05-01", "2024-05-02", "2024-05-03",  # 劳动节   "2024-06-08", "2024-06-09", "2024-06-10",  # 端午节   "2024-09-15", "2024-09-16", "2024-09-17",  # 中秋节   "2024-10-01", "2024-10-02", "2024-10-03", "2024-10-04", "2024-10-05", "2024-10-06", "2024-10-07"  # 国庆节 )) workdays_2024 <- as.Date(c(   "2024-02-04", "2024-02-18",  # 春节补班   "2024-04-07",  # 清明补班   "2024-04-28", "2024-05-11",  # 劳动节补班   "2024-06-02",  # 端午补班   "20...

open remote wsl for positron install

图片
  1 、安装 Positron 2 、 Positron 中安装 kv9898.open-remote-wsl-0.0.9.vsix 扩展 3 、 Press Ctrl+Shift+P (or Cmd+Shift+P on macOS) Run the command:   Register this WSL extension for launching Positron from WSL 4 、打开虚拟网卡模式和 Ubuntu , Press Ctrl+Shift+P (or Cmd+Shift+P on macOS) Run the command:   Remote-WSL Connect to WSL