Logo Lanfrica
  • Home
  • Atlas
  • Insights
  • Docs
  • Sign in

© 2026 Lanfrica. All rights reserved. All copyrights of the resources shown on the Lanfrica website belong to the original copyright holders, unless explicitly stated otherwise.

january-msemakweli/Climate-and-Malaria-Plots-and-Analysis: Climate and Malaria Analysis Project

Domain:

healthcareclimate

Record type:

software
Creator:
Jan
Publisher:
Zenodo
Host:avatar

Climate and Malaria Analysis Project

https://doi.org/10.5281/zen…

Overview

This repository contains R code for analyzing the relationship between climate variables and malaria incidence in Dar es Salaam, Tanzania. The analysis includes STL (Seasonal and Trend decomposition using Loess), Generalized Additive Model (GAM) analysis, and district-level mapping outputs.

Project Structure

Climate and Malaria Plots and Analysis/
├── Data.csv                           # Main dataset with climate and malaria data
├── STL.R                             # STL decomposition analysis
├── GAM Analysis Code.R               # GAM analysis with smooth term plots
├── Dar es Salaam District Map.R      # District map from local ward shapefile
├── Preliminary_Analysis.R            # Comprehensive preliminary analysis
├── df.R                              # STL seasonal comparisons and raw vs seasonal fits
├── SHAPEFILES/                       # Local ward shapefile inputs (not tracked)
├── Maps/                             # Output directory for map figures
├── Smooth Term Plots/                # Output directory for GAM plots
├── STL Plots/                        # Output directory for STL plots
├── Preliminary_Plots/                # Output directory for preliminary analysis
├── renv.lock                         # Locked package versions
└── README.md                         # This file

Data Description

The Data.csv file contains monthly time series data with the following variables:

  • yearmon: Time period (YYYY-MM format)
  • Malaria_incidence_per_10000: Malaria cases per 10,000 population
  • daytime_temperature: Daytime temperature in °C
  • nighttime_temperature: Nighttime temperature in °C
  • monthly_rainfall: Monthly rainfall in mm
  • Relative_humidity: Relative humidity in %

Analysis Components

1. STL Decomposition (STL.R)

Purpose: Decompose the malaria incidence time series into trend, seasonal, and residual components.

What it does:

  • Reads monthly malaria incidence data
  • Normalizes time index and handles missing values
  • Performs STL decomposition with periodic seasonality
  • Creates publication-ready 4-panel STL plot (300 dpi PNG)

Output: STL Plots/STL_Decomposition.png

Key features:

  • Robust fitting to handle outliers
  • Linear interpolation for internal gaps
  • Edge extrapolation for contiguous series
  • Publication-quality graphics

2. GAM Analysis (GAM Analysis Code.R)

Purpose: Model the relationship between climate variables and malaria incidence using Generalized Additive Models.

What it does:

  • Fits GAM with smooth terms for all climate variables
  • Includes lagged effects (1-3 months) and tests an extended 4-month lag model
  • Uses shrinkage selection to automatically remove unimportant terms
  • Creates individual plots for each smooth term
  • Performs model diagnostics (gam.check, concurvity, residual ACF/PACF)
  • Compares 3-month and 4-month lag GAM specifications using AIC

Output: Multiple PNG files in Smooth Term Plots/ directory

Key features:

  • Cubic spline basis functions with shrinkage
  • REML estimation
  • Automatic term selection
  • Publication-ready theme for all plots

3. Preliminary Analysis (Preliminary_Analysis.R)

Purpose: Comprehensive exploratory analysis of malaria incidence patterns and climate relationships.

What it does:

  • Creates time series plots of malaria incidence and climate variables
  • Analyzes seasonal patterns and annual trends
  • Generates correlation matrices and scatter plots
  • Identifies peak months and seasonal variations
  • Provides summary statistics for all variables

Output: 10 PNG files and 1 CSV file in Preliminary_Plots/ directory

Key visualizations:

  • Monthly malaria incidence time series (2014-2024)
  • Seasonal patterns with box plots by month
  • Annual trends with error bars
  • Climate variables time series (4-panel)
  • Correlation heatmap matrix
  • Scatter plots with trend lines
  • Seasonal decomposition
  • Peak months identification
  • Seasonal analysis by climate zones

4. STL Seasonal Comparisons (df.R)

Purpose: Detailed STL decomposition analysis comparing malaria incidence with individual climate variables.

What it does:

  • Performs STL decomposition on malaria incidence and each climate variable
  • Creates standardized seasonal component comparisons
  • Generates raw vs seasonal fit plots for each variable pair
  • Uses publication-ready academic styling with consistent themes

Output: 8 JPEG files in Preliminary_Plots/ directory

Key visualizations:

  • Malaria vs Rainfall seasonal components (standardized)
  • Malaria vs Daytime Temperature seasonal components
  • Malaria vs Nighttime Temperature seasonal components
  • Malaria vs Relative Humidity seasonal components
  • Raw series with STL seasonal fits for each climate variable
  • Academic theme with no titles, enhanced legends, and professional styling

5. Dar es Salaam District Map (Dar es Salaam District Map.R)

Purpose: Create a publication-ready district map of Dar es Salaam showing the five districts used in the study context.

What it does:

  • Reads local ward boundaries from SHAPEFILES/TANZANIA_2022PHC_WARDS_SHAPEFILES.shp
  • Filters Dar es Salaam wards and aggregates to district polygons
  • Keeps the five districts: Ilala, Kinondoni, Temeke, Ubungo, and Kigamboni
  • Adds district labels and a north arrow symbol
  • Exports a high-resolution PNG (300 dpi)

Output: Maps/Dar_es_Salaam_5_Districts.png

How to Run the Code

Prerequisites

  • R (version 4.0 or higher recommended)
  • RStudio (optional but recommended)

Setup Instructions

  1. Clone or download this repository

    git clone github.com
    cd "Climate and Malaria Plots and Analysis"
    
  2. Install dependencies (if using renv)

    renv::restore()
    
  3. Run the analyses

    For STL decomposition:

    source("STL.R")
    

    For GAM analysis:

    source("GAM Analysis Code.R")
    

    For preliminary analysis:

    source("Preliminary_Analysis.R")
    

    For STL seasonal comparisons:

    source("df.R")
    

    For Dar es Salaam district map:

    source("Dar es Salaam District Map.R")
    

Alternative Setup (without renv)

If you prefer not to use renv, the code will automatically install required packages:

  • dplyr, zoo (for STL analysis)
  • mgcv, dplyr, tidyr, gratia, ggplot2, stringr, purrr (for GAM analysis)
  • tidyverse, zoo, lubridate (for STL seasonal comparisons)
  • sf, ggplot2, ggspatial (for district map plotting)

Output Files

STL Analysis

  • STL Plots/STL_Decomposition.png: 4-panel STL decomposition plot showing:
    • Original time series
    • Trend component
    • Seasonal component
    • Residuals

GAM Analysis

  • Multiple PNG files in Smooth Term Plots/ directory, each showing:
    • Individual smooth term effects
    • 95% confidence intervals
    • Partial effects on malaria incidence
  • Smooth Term Plots/ACF_PACF_GAM_Residuals.png: Combined residual ACF/PACF diagnostic plot (300 dpi)

Preliminary Analysis

  • 01_malaria_timeseries.png: Monthly malaria incidence over time
  • 02_seasonal_patterns.png: Box plots showing seasonal variation
  • 03_annual_trends.png: Annual averages with standard deviations
  • 04_climate_timeseries.png: Time series of all climate variables
  • 05_correlation_heatmap.png: Correlation matrix between variables
  • 06_scatter_plots.png: Scatter plots with trend lines
  • 07_seasonal_decomposition.png: Average seasonal pattern
  • 08_summary_statistics.csv: Summary statistics table
  • 09_peak_months.png: Peak months identification
  • 10_seasonal_analysis.png: Seasonal analysis by climate zones

STL Seasonal Comparisons

  • 11_stl_seasonal_malaria_vs_rainfall.jpg: Standardized seasonal components comparison
  • 12_raw_vs_seasonal_fits.jpg: Raw data with STL seasonal fits (malaria and rainfall)
  • 13_stl_seasonal_malaria_vs_daytime_temperature_°c_.jpg: Malaria vs daytime temperature seasonality
  • 14_raw_vs_seasonal_fits_daytime_temperature_°c_.jpg: Raw vs seasonal fits for temperature
  • 15_stl_seasonal_malaria_vs_nighttime_temperature_°c_.jpg: Malaria vs nighttime temperature seasonality
  • 16_raw_vs_seasonal_fits_nighttime_temperature_°c_.jpg: Raw vs seasonal fits for nighttime temperature
  • 17_stl_seasonal_malaria_vs_relative_humidity_.jpg: Malaria vs humidity seasonality
  • 18_raw_vs_seasonal_fits_relative_humidity_.jpg: Raw vs seasonal fits for humidity

Dar es Salaam District Map

  • Maps/Dar_es_Salaam_5_Districts.png: District map (Ilala, Kinondoni, Temeke, Ubungo, Kigamboni), 300 dpi, white background, north arrow symbol

Code Features

Reproducibility

  • Set random seed for consistent results
  • Uses relative paths (setwd(".")) for portability
  • Automatic package installation and loading

Data Handling

  • Robust handling of missing values
  • Automatic data type conversion
  • Time series regularization

Visualization

  • Publication-ready graphics (300 dpi)
  • Consistent theming across plots
  • Professional color schemes and typography

Technical Notes

STL Parameters

  • s.window = "periodic": Fixed annual seasonality
  • robust = TRUE: Outlier-resistant fitting
  • Linear interpolation with edge extrapolation

GAM Specifications

  • Cubic spline basis (bs = "cs")
  • 10 knots per smooth term (k = 10)
  • REML estimation for optimal smoothing
  • Shrinkage selection (select = TRUE)

Model Diagnostics

  • gam.check(): Residual diagnostics
  • concurvity(): Check for concurvity issues
  • Residual autocorrelation checks via acf() and pacf()
  • AIC comparison between 3-lag and 4-lag GAM models

Troubleshooting

Common Issues

  1. Package installation errors: Ensure you have write permissions and internet connection
  2. Missing data: The code handles missing values automatically
  3. Path issues: All paths are now relative to the project root

Getting Help

  • Check R console for error messages
  • Ensure all required packages are installed
  • Verify the Data.csv file is in the project root

Citation

If you use this code in your research, please cite the relevant R packages:

  • mgcv for GAM analysis
  • zoo for time series operations
  • ggplot2 for visualization

Contact

For questions about this analysis, please contact the original author or repository maintainer.

Visit

doi.org

Licenses

info:eu-repo/semantics/openAccessCreative Commons Attribution 4.0 Internationalhttps://creativecommons.org/licenses/by/4.0/legalcode

Similar

january-msemakweli/R-Codes---Climate-Change-and-Malaria-Knowledge-and-Perceptions-In-Dar-es-Salaam: v1january-msemakweli/tanzania-malaria-anemia-inequalityjanuary-msemakweli/R-Codes---Climate-Change-and-Malaria-Knowledge-and-Perceptions-In-Dar-es-Salaam: Climate Change and Malaria Transmission: Knowledge and Perceptions Among Communities in Dar es Salaam, TanzaniaClimate blind spots in malaria control: frontline perspectives on climate adaptation and malaria control in ZambiaClimate Variability and Malaria over West AfricaInterannual Climate Variability and Malaria in Mozambique

january-msemakweli/R-Codes---Climate-Change-and-Malaria-Knowledge-and-Perceptions-In-Dar-es-Salaam: v1

Climate Change and Malaria Transmission: Knowledge and Perceptions Among Communities in Da

january-msemakweli/tanzania-malaria-anemia-inequality

Analysis code for rural and wealth-related inequality in malaria-anemia co-infection among children

january-msemakweli/R-Codes---Climate-Change-and-Malaria-Knowledge-and-Perceptions-In-Dar-es-Salaam: Climate Change and Malaria Transmission: Knowledge and Perceptions Among Communities in Dar es Salaam, Tanzania

Climate Change and Malaria Transmission: Knowledge and Perceptions Among Communities in Da

Climate blind spots in malaria control: frontline perspectives on climate adaptation and malaria control in Zambia

Background Climate change is increasingly recognised as a significant barrier

Climate Variability and Malaria over West Africa

Malaria is a major public health problem in West Africa. Previous studies have shown that climate va

Interannual Climate Variability and Malaria in Mozambique

Abstract Malaria is among the greatest public health threats in Mozambique, with over 10 million ca