Modelling Malaria in The Gambia

Author

Barry Rowlingson

In this worksheet we will fit a model to some malaria data in The Gambia. A number of malaria prevalence surveys were conducted in children, and the use of bednets was recorded along with basic demographics. More details of the data and an earlier analysis can be found in Childhood Malaria in the Gambia: A Case-Study in Model-Based Geostatistics. The survey data supplied here is not for distribution, and has been randomised from the original source. The substantive results are similar. Some of the plots and tables in the analysis below may have been made from a different randomisation of the source data and hence may differ in exact detail.

Download Course Materials and extract from the zip file for the datasets etc needed here. Alongside the survey data are some layers derived from OpenStreetMap data, made available under the Open Database License. See the link for details.

If you’ve not installed the R Console plugin, do that first - some help is available in our installation guide.

R Console Intro

Basic Usage

From an empty QGIS project you should see an R logo on a button in a toolbar.

R Icon in the Toolbar

Click it to open up a new panel, the R Console which is split into two. At the top will be an editor window for a currently untitled file, and below that two tabs, including the console tab which should have started R and is waiting for input with a > prompt.

The R Console Prompt

Here you can type R expressions and have them evaluated, or make plots and graphics that show up in the Plots tab.

Simple R Expressions at the Prompt

If your expression produces a plot then the Plots tab will be displayed with the graphic. Along the bottom you will see icons of the most recent plots which you can recall with a click. Go back to the Console and make a new plot with hist(runif(1000)) and see this as a new graphic and second icon in the list.

A Histogram in the Plots Window

Using the Script Editor

The editor panel above the Console and Plots tabs lets you type in R code which can then be sent to the console for execution. Put the following line of code into the window.

x = 1:10

With your cursor somewhere on the first line, press Control-Enter (Apple users may have a different modifier key for this). This will send the first line to the console where it is echoed and runs. Now type the rest of this little script in.

x = 1:10
y = 3 * x + rnorm(10, 0, 2)
plot(x,y)

m = lm(y~x)
abline(m)

Move the cursor to the second line and use Control-Enter to send it. This should now have created R vectors called x and y in your R workspace. Send the next line which will flip to the Plots tab and show a scatter plot.

The next line of R code will fit a least-squares straight-line model to the data, and then the next uses abline to add the line to the plot. Send those to see a scatterplot with a straight line through it.

As well as sending one line at a time, you can click the green “Go” arrow above the script to run the whole file. If you try that now you will get a slightly different scatterplot because of the random numbers generated by rnorm, which are the statistical noise in the data.

The Control-Enter key will either send the line the cursor is on or any lines that are selected. Instead of clicking the green “Go” arrow you can do Control-A Control-Enter to select all the code and run it.

The Script Editor can open multiple files (use the + sign) and save and load R scripts to your file storage for recall.

Load the Malaria Survey Data

The GeoPackage malaria_gambia.gpkg has three layers that we need to load into QGIS. You can click Control-Shift-V to open the “Add Vector Layer” dialog, open the file as the source and select all the layers. There should be one point layer, called survey, a line layer of water lines, and a polygon layer of water areas. With a bit of styling and rearrangement of the QGIS layers you can make something like this, showing the eastern part of the country with red stars for the survey locations, and various shades of blue and green for the water features. However you don’t need to style this for the statistical analysis we’re going to do, you can just leave the defaults.

Some Survey Locations and Water Features

Have a look at the survey data in QGIS. Open its attribute table. You’ll see it is individual data, with the age and sex of the subject. There’s also a three-level categorical value showing the use of bednets - either no bednet, and untreated bednet, or an insecticide-treated bednet. There’s also the survey date.

The result of the malaria test for each individual is in the PFPOS column, which stands for “Plasmodium falciparum positive”, meaning the child tested positive for malaria parasites in a blood sample. It is difficult to show this on a map since we have a number of tests at each village location, so the stars on the previous map are actually a number of coincident points. QGIS can do some fancy visualisations, for example switching the renderer to “Point Displacement” and crafting a few other settings results in this, where the positive tests are red and negative tests are blue, and the points are shown in rings around the village point. Its not perfect but it can give you an idea of positivity and the sample sizes at the villages.

Survey Results Shown Using Point Displacement

You could also aggregate the data at each village and produce a new point data set with the fraction of positive tests, and visualise this with colours or dot sizes. Some analyses in the literature use this sort of aggregated data but note that loses the individual information such as age, sex, and sample date, which may be important predictors of malaria parasite prevalence.

Getting QGIS Layers Into R

The R session running in the R Console communicates with the QGIS session via an object returned from the qgis_project() function. This function comes from a package called rqgis which is attached to every R Console session. Once you have this object, elements of it accessed via $- names will interact with QGIS. For example, try running these lines from the R Console prompt (text after the # is a comment and you don’t need to type it):

qgis = qgis_project()      # get the project object
qgis$get_canvas_extent()   # canvas limits
qgis$crs                   # project crs
qgis$list_layers()         # data frame of layers

To get a layer from QGIS into R, you use qgis$get_layer with the layer name or ID from the layer list. The default names from GeoPackages are a bit unwieldy, so use the QGIS layers panel to rename the malaria_gambia — survey layer to simply survey. Then the following two lines will get the survey data into R and show the first few rows.

mg = qgis$get_layer("survey")
head(mg)

This is much like the QGIS attribute table, but with an extra geometry column that stores the point locations, shown here in WKT form. The mg object is a “Simple feature collection” spatial data frame with a coordinate system attached. This object is defined in the sf package, so we should probably attach that now, as well as the terra package for rasters we might need later. If you don’t have these packages in your R session, use install.packages(c("sf","terra")) to get them. Once attached we can use functions from them on the mg object, for example to get the CRS.

# attach essential packages
library(sf)
library(terra)

st_crs(mg)

Its important to know that mg is a full copy of the data from the QGIS layer, and if you change the QGIS layer then the mg object is unaffected. If you do any changes to the layer in QGIS, for example deleting features or adding new attribute columns, the mg object will not be updated, and you would need to run mg = qgis$get_layer("survey") to get the new version.

Fitting a Model

Now our question is whether using mosquito bednets prevents infection with the malaria parasites. Infection might also depend on the age and sex of the child, as well as the time of the year that the survey was conducted since malaria is seasonal. Our outcome is a binary value where 0 means no infection and 1 means infection, which points us to a logistic regression. If this means nothing to you, never mind, you can type code in, run it, and the outputs will be explained.

Logistic regressions in R can be fitted using the glm function and the binomial “family”. You tell the function what your outcome might depend on, where the data comes from, and that you want a binomial regression. It returns a fitted model object.

model1 = glm(PFPOS ~ AGE_M + SEX + NETS + DATE, data=mg, family=binomial)

The first argument here is an R formula and specifies our model as “does the parasite presence depend linearly on age (in months), sex, the different types of bednet use, and the date?”. We can get an answer to this question by looking at the output from summary(model1), specifically the Coefficients section.

summary(model1)
Coefficients:
                Estimate Std. Error z value Pr(>|z|)
(Intercept)   -4.114e+02  5.699e+01  -7.219 5.24e-13 ***
AGE_M          1.087e-02  3.556e-03   3.058  0.00223 ** 
SEXM          -4.710e-02  9.735e-02  -0.484  0.62847
NETSTreated   -8.537e-01  1.312e-01  -6.509 7.58e-11 ***
NETSUntreated -4.581e-01  1.145e-01  -4.001 6.31e-05 ***
DATE           4.922e-02  6.827e-03   7.210 5.60e-13 ***

Ignore everything in that table except the sign (plus or minus) of the Estimate column and the stars at the end. Ignore the first row ((Intercept)). The AGE_M row has a positive value and two stars. This means the model found it to be significantly non-zero - an increasing age meant an increasing risk of parasites. The next line, SEXM, is negative but no stars. The data doesn’t tell us if male children are at higher or lower risk than female children. The next two lines tell us about bednet usage, relative to a baseline corresponding to the category missing from the table, which is "Untreated". Both the estimates for treated and untreated nets are negative, and highly significant (three stars). This means the infection probability goes down when using nets compared to not using nets. The estimate for treated nets is more negative than untreated nets, which we might expect. The last line shows a significant effect with date, meaning surveys later in the season resulted in higher infection probabilities.

What we haven’t done here (and won’t do, for simplicity) is work out the strength of each effect. This is hard to tell from the table directly, but rigorous statistical analysis isn’t what we’re doing here. We’d have to go into likelihood ratio tests and… well, let’s not.

A Smooth Spatial Model

Models such as the above are subject to much criticism, so don’t try publishing this. Firstly, why should the relationship with the date be a simple linear – either up or down – with time? If malaria is seasonal this might have peaked and then declined over the times of the survey. We should model a more complex relationship with date than just a linear one.

Secondly, we’ve assumed every child tested is an independent data record. But we have large numbers of children tested at each village location, and the location might have some effect on the parasite prevalence. We need a spatial model.

We can fix these problems using a Generalised Additive Model framework. Instead of fitting straight lines through the data, the system fits smooth curves, and uses some methods to not overfit, resulting in overconfident estimates. We need a couple of packages for this, mgcv and gratia – which you can install using install.packages(c("mgcv","gratia")) if you dont have them already. All the packages we’ve used are open source and available from the CRAN package library site.

library(mgcv)
library(gratia)

We want to fit a smooth curve to the date and age variables, and also include a two-dimensional smooth surface over the locations to model any overall effect of location. Our parasite measurements should then be considered independent once this spatial effect is include in our model. This means we need the X and Y coordinates of the data added as columns, which we can get with the st_coordinates function, using cbind to add them as columns.

mg = cbind(mg, st_coordinates(mg))

Then we can fit our model with smooths using the gam function from the mgcv package. We wrap each variable we want fitted smoothly in s(.) and for our two-dimensional spatial smooth we use s(X,Y). The only other complication is that gam needs dates to be converted to numeric values, which glm does automatically.

model_s = gam(PFPOS ~ s(AGE_M) + SEX + NETS + s(as.numeric(DATE)) + s(X,Y),
              data=mg, family=binomial)

We can use summary(model_s) to see what the model thinks about the parameters. This section tells us about the categorical variables that we aren’t fitting with a smooth function.

Parametric coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept)   -0.42562    0.11677  -3.645 0.000267 ***
SEXM          -0.03267    0.10310  -0.317 0.751310 
NETSTreated   -0.71031    0.16240  -4.374 1.22e-05 ***
NETSUntreated -0.25446    0.14294  -1.780 0.075039 .

Now we see untreated nets only merits a single dot in the significance stars, showing very weak evidence of them being better than the baseline of no bednet, but treated nets are still significant, and with a negative sign to the estimate which shows us they are protective of parasite infection.

The next section of the output relates to the smoothed terms.

Approximate significance of smooth terms:
                       edf Ref.df Chi.sq  p-value
s(AGE_M)             4.154  5.133  34.57 3.02e-06 ***
s(as.numeric(DATE))  7.287  8.188  36.30 8.19e-06 ***
s(X,Y)              21.942 26.002  59.66 0.000186 ***

The stars here indicate that our smoothed terms are giving us information about the parasite positivity, so we are justified in using them. To see how they affect parasite positivity we should plot them.

Plotting GAM Smooths

The gratia package has some functionality for making plots of smoothers from GAM models. You can just use draw(model_s) to see all three smooth function but they are a bit crammed in the R Plots window. Draw the first two in one plot, and the spatial smooth in a second.

draw(model_s, select=c(1,2))
draw(model_s, select=3)

The first two graphs show that the parasite presence increases smoothly with age but does drop off steeply at about 45 months. The change of parasite presence with date of sample is more complex.

Age and Date Smooths

The spatial smooth shows definite regions of high and low values indicating some inhomogeneity in parasite prevalence that is not explained by the other variables. The smoothed function is only shown in the vicinity of the data points, and grey otherwise, to stop the model extrapolating too far from data.

Spatial Smooth

You may have noticed that these plots are quite low resolution and hard to read. Improving this might be a useful suggestion for the R Console project maintainer.

Data Transfer to QGIS

Let’s get this spatial smooth raster into QGIS for further mapping. To do this we need to get the data of the smoothed grid into a spatial raster object as defined by the terra package. There are some functions in gratia that will greatly help with this. There’s a few steps in the following code, but if you want to know what is happening inspect the created objects at each stage and read the help for the functions being used.

### Use `smooth_estimates` to get the smooth as
### a 3-column data frame of X, Y, S. X and Y form
### a grid over the space.
sm_data = data.frame(smooth_estimates(model_s, "s(X,Y)"))

### Use `too_far` to add a fourth TRUE/FALSE column where
### the grid coordinates X,Y are not close to data points
sm_data$exclude = too_far(sm_data$X, sm_data$Y, mg$X, mg$Y, dist=0.1)

### Use terra's `rast` function to turn the estimates from the data frame to a raster.
### Set the CRS from the original data since X and Y are derived from there.
sm_est = terra::rast(sm_data[,c("X","Y",".estimate")], type="xyz", crs=terra::crs(mg))

### Similarly, create a raster of the data we want to exclude
### for being distant from data
sm_exclude = terra::rast(sm_data[,c("X","Y","exclude")], type="xyz", crs=terra::crs(mg))

### set the estimate grid to NA where we want to exclude values
sm_est[sm_exclude] = NA

One great thing about using a programming language is that we could put all this manipulation into a function and end up with something like:

sm_est = gam2rast(model_s, mg)

If you have the R skills, try writing that function!

Now we have a raster we can plot from the R Console.

terra::plot(sm_est)

Raster Smooth Plotted in R

To get this into QGIS, we use the qgis$insert_layer function, and give the new layer a meaningful name. At the R Console run this.

qgis$insert_layer(sm_est, "sm_est")

You can then style the layer in QGIS. Here I’ve used a blue-to-red colour palette, and set the blend mode to “Multiply”.

Raster Rendering Dialog in QGIS

When combined with a monochrome background layer, this shows the map context with the raster blended nicely over the top. Here I’ve used the “VersaTiles Graybeard” background layer available from the QGIS Quick Map Services plugin. These tiles, like all OpenStreetMap-derived maps, are © OpenStreetMap contributors.

Spatial Smooth over Gambia Map

At this point the analyst might look at this map and consider what might explain this pattern of spatial smoothing, as there may be some other spatial variable not yet included in the model that correlates with the pattern of highs and lows in the surface. But we have established how insecticide-treated bednets decrease malaria parasite infection in a robust model considering spatial correlation. Win.

Improvements

There’s a few things that could be improved here. You might notice that the pixels of the raster aren’t square - this is because the smooth_estimates function creates a 100x100 grid over the boundary of the points, but that boundary is a rectangle. This also accounts for the elliptical bounds computed by the too_far function - it is using pixels for distance rather than distance in X and Y. These things could be corrected but its not the focus here - I wanted to show how raster (and vector) data can be transferred between QGIS and R using the R Console system.

Further Work

The supplied GeoPackage contains two layers relating to water features in the study area, both extracted from OpenStreetMap. Mosquitos need water for their lifecycles, so perhaps distance to water is a contributing factor? But what kind of water? The linear and polygon water feature layer have a range of classes such as “reservoir” and “wetland_marsh”, so you could subset from these to create a layer of potential mosquito preferences.

You would then use a function from the QGIS Processing Toolbox to add the distance to nearest water feature, read that in the R Console and add it as a variable to the analysis. Should it be a smoothed function or a linear function? You could instead use some of the sf package functions to compute the distance to the nearest water feature instead.

QGIS R Console Thoughts

  • The QGIS R Console system is a neat way of tightly integrating R and QGIS.
  • It is still in early development, and there are some rough edges.
  • Functionality is limited mostly to data transfer with a few status and layer information functions. There are no functions for working with QGIS styles, for example.
  • The working environment, with separate R Console, Plots and Script Editor panes is primitive compared to full development environments such as RStudio, VS Code or Emacs.
Analytics by statpx