Title: | Modelling Spatial Variation in Disease Risk for Areal Data |
---|---|
Description: | Formatting of population and case data, calculation of Standardized Incidence Ratios, and fitting the BYM model using 'INLA'. For details see Brown (2015) <doi:10.18637/jss.v063.i12>. |
Authors: | Patrick Brown [aut, cre, cph], Lutong Zhou [aut] |
Maintainer: | Patrick Brown <[email protected]> |
License: | GPL |
Version: | 2.0.6 |
Built: | 2024-11-07 02:55:58 UTC |
Source: | https://github.com/cran/diseasemapping |
Functions for calculating observed and expected counts by region, and manipulating posterior samples from Bayesian models produced by glmmBUGS.
Patrick Brown
# creating SMR's data('kentucky') kentucky = terra::unwrap(kentucky) kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") if(require('mapmisc', quietly=TRUE)) { mycol = colourScale(kentucky2$SMR, breaks=9, dec=-log10(0.5), style='equal', transform='sqrt') plot(kentucky2, col=mycol$plot) legendBreaks('topleft', mycol) } if(require("INLA", quietly=TRUE)) { if(requireNamespace('INLA')) { INLA::inla.setOption(num.threads=2) # not all versions of INLA support blas.num.threads try(INLA::inla.setOption(blas.num.threads=2), silent=TRUE) } kBYM = bym(observed ~ offset(logExpected) + poverty, data= kentucky2, prior = list(sd=c(0.1, 0.5), propSpatial=c(0.5, 0.5)) ) kBYM$par$summary }
# creating SMR's data('kentucky') kentucky = terra::unwrap(kentucky) kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") if(require('mapmisc', quietly=TRUE)) { mycol = colourScale(kentucky2$SMR, breaks=9, dec=-log10(0.5), style='equal', transform='sqrt') plot(kentucky2, col=mycol$plot) legendBreaks('topleft', mycol) } if(require("INLA", quietly=TRUE)) { if(requireNamespace('INLA')) { INLA::inla.setOption(num.threads=2) # not all versions of INLA support blas.num.threads try(INLA::inla.setOption(blas.num.threads=2), silent=TRUE) } kBYM = bym(observed ~ offset(logExpected) + poverty, data= kentucky2, prior = list(sd=c(0.1, 0.5), propSpatial=c(0.5, 0.5)) ) kBYM$par$summary }
Uses inla to fit a Besag, York and Mollie disease mapping model
## S4 method for signature 'formula,ANY,ANY,missing' bym(formula,data,adjMat,region.id,...) ## S4 method for signature 'formula,ANY,missing,missing' bym(formula,data,adjMat,region.id,...) ## S4 method for signature 'formula,SpatVector,NULL,character' bym(formula, data, adjMat, region.id, ...) ## S4 method for signature 'formula,SpatVector,missing,character' bym(formula, data, adjMat, region.id, ...) ## S4 method for signature 'formula,SpatVector,matrix,character' bym(formula,data,adjMat,region.id,...) ## S4 method for signature 'formula,data.frame,matrix,character' bym( formula,data,adjMat,region.id, prior=list(sd=c(0.1,0.5),propSpatial=c(0.5,0.5)), family="poisson",formula.fitted=formula,...)
## S4 method for signature 'formula,ANY,ANY,missing' bym(formula,data,adjMat,region.id,...) ## S4 method for signature 'formula,ANY,missing,missing' bym(formula,data,adjMat,region.id,...) ## S4 method for signature 'formula,SpatVector,NULL,character' bym(formula, data, adjMat, region.id, ...) ## S4 method for signature 'formula,SpatVector,missing,character' bym(formula, data, adjMat, region.id, ...) ## S4 method for signature 'formula,SpatVector,matrix,character' bym(formula,data,adjMat,region.id,...) ## S4 method for signature 'formula,data.frame,matrix,character' bym( formula,data,adjMat,region.id, prior=list(sd=c(0.1,0.5),propSpatial=c(0.5,0.5)), family="poisson",formula.fitted=formula,...)
formula |
model formula, defaults to intercept-only model suitable for
output from |
data |
The observations and covariates for the model, can be output from
|
adjMat |
An object of class |
region.id |
Variable in |
prior |
named list of vectors specifying priors, see Details |
family |
distribution of the observations, defaults to |
formula.fitted |
formula to use to compute the fitted values, defaults to the model formula but may, for example, exclude individual-level covariates. |
... |
Additional arguments passed to
|
The Besag, York and Mollie model for Poisson distributed case counts is:
is the response variable for region
, on the left side of the
formula
argument.
is the 'baseline' expected count, which is specified
in
formula
on the log scale with an
offset
variable.
are covariates, on the right side of
formula
is a spatial random effect, with a spatially structured variance parameter
and a spatially independent variance
.
The prior
has elements named sd
and propSpatial
, which
specifies model="bym2"
should be used with penalized complexity priors.
The sd
element gives a prior for the marginal standard deviation
.
This prior is approximately exponential, and
prior$sd = c(1, 0.01)
specifies a
prior probability .
The
propSpatial
element gives the prior for the ratio
. Having
prior$propSpatial = c(0.5, 0.9)
implies
.
A list containing
inla |
results from the call to
|
data |
A |
parameters |
Prior and posterior distributions of the two covariance parameters, and a table summary with posterior quantiles of all model parameters. |
Patrick Brown
data('kentucky') kentucky = terra::unwrap(kentucky) # get rid of under 10s larynxRates = larynxRates[-grep("_(0|5)$",names(larynxRates))] kentucky = getSMR(kentucky, larynxRates, larynx, regionCode="County") if(requireNamespace('INLA')) { INLA::inla.setOption(num.threads=2) # not all versions of INLA support blas.num.threads try(INLA::inla.setOption(blas.num.threads=2), silent=TRUE) } kBYM <- try( bym( observed ~ offset(logExpected) + poverty, kentucky, prior= list(sd=c(0.1, 0.5), propSpatial=c(0.5, 0.5)) ), silent=TRUE) if(length(grep("parameters", names(kBYM)))) { kBYM$parameters$summary } if( require("mapmisc", quietly=TRUE) && length(grep("parameters", names(kBYM))) ) { thecol = colourScale(kBYM$data$fitted.exp, breaks=5, dec=1, style="equal") terra::plot(kBYM$data, col=thecol$plot) legendBreaks("topleft", thecol) }
data('kentucky') kentucky = terra::unwrap(kentucky) # get rid of under 10s larynxRates = larynxRates[-grep("_(0|5)$",names(larynxRates))] kentucky = getSMR(kentucky, larynxRates, larynx, regionCode="County") if(requireNamespace('INLA')) { INLA::inla.setOption(num.threads=2) # not all versions of INLA support blas.num.threads try(INLA::inla.setOption(blas.num.threads=2), silent=TRUE) } kBYM <- try( bym( observed ~ offset(logExpected) + poverty, kentucky, prior= list(sd=c(0.1, 0.5), propSpatial=c(0.5, 0.5)) ), silent=TRUE) if(length(grep("parameters", names(kBYM)))) { kBYM$parameters$summary } if( require("mapmisc", quietly=TRUE) && length(grep("parameters", names(kBYM))) ) { thecol = colourScale(kBYM$data$fitted.exp, breaks=5, dec=1, style="equal") terra::plot(kBYM$data, col=thecol$plot) legendBreaks("topleft", thecol) }
Rates by age and sex group are retreived from http://ci5.iarc.fr/CI5plus/ci5plus.htm
cancerRates(area = "canada", year=2000, sex=c("M", "F"), site="Lung")
cancerRates(area = "canada", year=2000, sex=c("M", "F"), site="Lung")
area |
Region to retrieve rates from, |
year |
year or sequence of years to retrieve data from, within the period 1978 to 2002 |
site |
a vector of cancer sites, see details |
sex |
|
area
must be one of Canada, Norway,
Latvia,
Lithuania,
Iceland,
Finland,
Estonia,
Denmark,
"Czech Republic",
"Costa Rica",
USA,
Iowa,
"New Mexico"
or the Canadian provinces of
Newfoundland, Prince Edward Island,
Nova Scotia,
Ontario,
Manitoba,
Saskatchewan,
Alberta, and
British Columbia. Alternately an integer specifying a registry code from http://ci5.iarc.fr.
site
must be one or more of
All Sites,
Oral Cavity and Pharynx,
Oesophagus.
Stomach,
Colon,
Rectum and Anus,
Liver,
Gallbladder,
Pancreas,
Larynx,
Lung,
Bone,
Melanoma of skin,
Prostate (Males only),
Testis (Males only),
Breast (Females only),
Cervix uteri (Females only),
Corpus uteri (Females only),
Ovary and other uterine adnexa (Females only),
Kidney,
Bladder,
Eye,
Brain and Central Nervous System,
Thyroid,
Non-Hodgkin Lymphoma,
Hodgkin Lymphoma,
Multiple myeloma,
Leukaemia.
vector of cancer rates, by age and sex group
# won't work if offline or if the iarc web site is down qcLungF=try(cancerRates(area="canada", year=2001:2002, site="lung", sex="F"), silent=TRUE) if(length(grep("try-error", class(qcLungF)))) { qcLungF = structure(c(0, 5e-06, 0, 0, 5e-06, 1e-05, 0, 3.4e-05, 9.6e-05, 0.000211, 0.000559, 0.001289, 0.002003, 0.002508, 0.002728, 0.003189, 0.002792, 0.001905), .Names = c("F_0", "F_5", "F_10", "F_15", "F_20", "F_25", "F_30", "F_35", "F_40", "F_45", "F_50", "F_55", "F_60", "F_65", "F_70", "F_75", "F_80", "F_85"), site = "Lung", area = "Canada", year = "2001-2002") } qcLungF data('popdata') popdata = terra::unwrap(popdata) qcLungExp = getSMR(popdata, qcLungF) names(qcLungExp) if(require('mapmisc', quietly=TRUE)) { mycol = colourScale(qcLungExp$expected, breaks=12, dec=0, style='quantile') plot(popdata[1:400,]) plot(qcLungExp, col=mycol$plot, border='#00000040',add=TRUE) legendBreaks('topright', mycol) }
# won't work if offline or if the iarc web site is down qcLungF=try(cancerRates(area="canada", year=2001:2002, site="lung", sex="F"), silent=TRUE) if(length(grep("try-error", class(qcLungF)))) { qcLungF = structure(c(0, 5e-06, 0, 0, 5e-06, 1e-05, 0, 3.4e-05, 9.6e-05, 0.000211, 0.000559, 0.001289, 0.002003, 0.002508, 0.002728, 0.003189, 0.002792, 0.001905), .Names = c("F_0", "F_5", "F_10", "F_15", "F_20", "F_25", "F_30", "F_35", "F_40", "F_45", "F_50", "F_55", "F_60", "F_65", "F_70", "F_75", "F_80", "F_85"), site = "Lung", area = "Canada", year = "2001-2002") } qcLungF data('popdata') popdata = terra::unwrap(popdata) qcLungExp = getSMR(popdata, qcLungF) names(qcLungExp) if(require('mapmisc', quietly=TRUE)) { mycol = colourScale(qcLungExp$expected, breaks=12, dec=0, style='quantile') plot(popdata[1:400,]) plot(qcLungExp, col=mycol$plot, border='#00000040',add=TRUE) legendBreaks('topright', mycol) }
Cases of Hepatitis Z in Ontario.
data(casedata)
data(casedata)
data frame
This dataset refers to cases of Hepatitis Z in Ontario for the years 1916 to 1918, giving the number of cases in each census subdivision by age, sex and year. For reasons of privacy, any counts between 1 and 5 have been changed to 1.
data(casedata) head(casedata) table(casedata$cases) tapply(casedata$cases, casedata$age, sum) ## maybe str(casedata) ; plot(casedata) ...
data(casedata) head(casedata) table(casedata$cases) tapply(casedata$cases, casedata$age, sum) ## maybe str(casedata) ; plot(casedata) ...
The formatCases funtion formats the case data set. Changes other formats of age and sex group to three columns: age, ageNumeric and sex.
formatCases(casedata, ageBreaks = NULL, years = NULL, aggregate.by = NULL)
formatCases(casedata, ageBreaks = NULL, years = NULL, aggregate.by = NULL)
casedata |
disease cases data set, usually a data.frame which contains age and sex and number of cases. |
ageBreaks |
results from |
years |
if it contains multiple years, define which years will be included in. |
aggregate.by |
if want to view the data set from a macro way, could aggregate the data set by age or sex or other variables. |
After using formatCases function, the age columns will change to a "character" column contains ages in cut
format, i.e [50,55), denotes age 50.
The cut breaks can be found from the breaks of the population data set or defined by user.
The original "age" column will changed to "ageNumeric" columns as factors.
The sex column will have two levels "M" and "F" as factors.
If "aggregate.by" is not NULL, the number of cases will be sum up by the groups defined in aggregate.by
argument.
formatCases function will return a data frame.
Patrick Brown
data('casedata') data('popdata') head(casedata) caseformat <- formatCases(casedata, ageBreaks = getBreaks(names(popdata))) head(caseformat) caseformatagg <- formatCases(casedata, ageBreaks = getBreaks(names(popdata)), aggregate.by=c("age", "sex")) head(caseformatagg)
data('casedata') data('popdata') head(casedata) caseformat <- formatCases(casedata, ageBreaks = getBreaks(names(popdata))) head(caseformat) caseformatagg <- formatCases(casedata, ageBreaks = getBreaks(names(popdata)), aggregate.by=c("age", "sex")) head(caseformatagg)
The formatCases funtion formats the population data set. Reshape the population data set to "long" format, add in 4 columns : GROUP, POPULATION, sex and age.
## S4 method for signature 'data.frame' formatPopulation( popdata, aggregate.by = NULL, breaks = NULL, ... ) ## S4 method for signature 'SpatVector' formatPopulation( popdata, aggregate.by = NULL, breaks = NULL, ... ) ## S4 method for signature 'list' formatPopulation( popdata, aggregate.by = NULL, breaks = NULL, years=as.integer(names(popdata)), year.range=NULL, time="YEAR", personYears=TRUE,... )
## S4 method for signature 'data.frame' formatPopulation( popdata, aggregate.by = NULL, breaks = NULL, ... ) ## S4 method for signature 'SpatVector' formatPopulation( popdata, aggregate.by = NULL, breaks = NULL, ... ) ## S4 method for signature 'list' formatPopulation( popdata, aggregate.by = NULL, breaks = NULL, years=as.integer(names(popdata)), year.range=NULL, time="YEAR", personYears=TRUE,... )
popdata |
population data set. It can be a data frame, list, database connection, or spatial polygon data frame |
aggregate.by |
if want to view the data set from a macro way, could aggregate the data set by age or sex or other variables |
breaks |
age breaks the user want to use. i.e breaks = c(10, 20, 30 ,40, 60, Inf). |
time |
the time variable, i.e years |
personYears |
convert populations to person-years |
years |
a vector with the year of each dataset |
year.range |
two dimensional vector with first and last year |
... |
additional arguments |
After using the formatPopulation
function, it will return the population data set in the same class as the original data set.
i.e if a spatial polygon data frame has been put into the formatPopulation
function, it will return a spatial polygon data frame.
If aggregate.by
is not NULL, the number of cases will be sum up by the groups define in aggregate.by.
The "Group" column contains information of sex and age groups,in the format of M.55, denotes male, year 55.
The "POPULATION" column is a numeric column, denotes the size of population for the particular age and sex group.
The "age" column will be a "character" column contains ages in a cut format. i.e [50,55), denotes age 50.
The cut breaks will get from the breaks of population data set or define by user.
The sex column will have two levels "M" and "F" as factors.
A data frame or spatial object, matching the input.
If breaks
is not specified, the function will aggregate by "age" as default.
Patrick Brown
data('kentucky') kentucky = terra::unwrap(kentucky) head(terra::values(kentucky)) poptry <- formatPopulation(kentucky, breaks = c(seq(0, 80, by=10), Inf)) head(poptry) poptryagg <- formatPopulation(kentucky, breaks = c(seq(0, 80, by=10), Inf), aggregate.by=c("sex", "age")) head(poptryagg)
data('kentucky') kentucky = terra::unwrap(kentucky) head(terra::values(kentucky)) poptry <- formatPopulation(kentucky, breaks = c(seq(0, 80, by=10), Inf)) head(poptry) poptryagg <- formatPopulation(kentucky, breaks = c(seq(0, 80, by=10), Inf), aggregate.by=c("sex", "age")) head(poptryagg)
An internal function to return a list contains age breaks, ages in the population data set,
sex in the population data set, and age sex groups will be used in the formatPopulation
function.
getBreaks(colNames, breaks = NULL)
getBreaks(colNames, breaks = NULL)
colNames |
names from the population data set |
breaks |
the age breaks, i.e breaks =seq(0, 80, by= 10) |
vector of ages
data('kentucky') ageBreaks = getBreaks(names(kentucky), breaks=c(seq(0, 80, by=10), Inf)) ageBreaks
data('kentucky') ageBreaks = getBreaks(names(kentucky), breaks=c(seq(0, 80, by=10), Inf)) ageBreaks
The getRates function calculates the estimated coefficient of the age and sex group from the case and population data set. It fits a glm model with Poisson distribution by default.
getRates(casedata, popdata, formula, family = 'poisson', minimumAge = 0, maximumAge = 100, S = c("M", "F"), years = NULL, year.range = NULL, case.years = grep("^year$", names(casedata), ignore.case = TRUE, value = TRUE), fit.numeric=NULL, breaks = NULL)
getRates(casedata, popdata, formula, family = 'poisson', minimumAge = 0, maximumAge = 100, S = c("M", "F"), years = NULL, year.range = NULL, case.years = grep("^year$", names(casedata), ignore.case = TRUE, value = TRUE), fit.numeric=NULL, breaks = NULL)
casedata |
A data frame of case data, with columns corresponding to variables in |
popdata |
population data set |
formula |
the glm model you want to fit. ie. |
family |
the distribution to fit the model |
minimumAge |
the lower boundary of the age, default is 0 |
maximumAge |
the higher boundary of the age, default is 100 |
S |
vector of sexes to include in the analysis. Defaults to both "M" and "F" |
years |
a vector of census years |
year.range |
study period: a vector of two elements, starting dates and ending dates |
case.years |
variable name in the case data which contains time |
fit.numeric |
the variables which needed to be changed from factor to numeric |
breaks |
the age breaks |
It fits a glm model with Poisson or binomial distribution over case and population data sets. If there is no data set in some age and sex group, an NA will show there.
A summary of the glm model contains set of estimated coefficients for different age and sex groups.
Patrick Brown
data('casedata') data('popdata') popdata = terra::unwrap(popdata) therates = getRates(casedata, popdata, ~sex*age, breaks=c(seq(0, 80, by=10), Inf)) therates
data('casedata') data('popdata') popdata = terra::unwrap(popdata) therates = getRates(casedata, popdata, ~sex*age, breaks=c(seq(0, 80, by=10), Inf)) therates
Calculates the rate of observe value over expected value. It will also merge back the observed value, expected value and the ratio back to the population data set.
## S4 method for signature 'SpatVector,ANY,ANY,ANY,ANY' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'list,ANY,ANY,ANY,ANY' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale=1, sex=c('m','f'), ... ) ## S4 method for signature 'data.frame,ANY,missing,missing,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,data.frame,missing,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,data.frame,character,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,missing,character,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,data.frame,character,character' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... )
## S4 method for signature 'SpatVector,ANY,ANY,ANY,ANY' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'list,ANY,ANY,ANY,ANY' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale=1, sex=c('m','f'), ... ) ## S4 method for signature 'data.frame,ANY,missing,missing,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,data.frame,missing,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,data.frame,character,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,missing,character,missing' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... ) ## S4 method for signature 'data.frame,ANY,data.frame,character,character' getSMR( popdata, model, casedata, regionCode , regionCodeCases , area.scale = 1, sex=c('m','f'),... )
popdata |
the name of population data set |
model |
rates, either fitted model (usually a |
casedata |
the name of case data set |
regionCode |
the name of district area column in population data set |
regionCodeCases |
the name of district area column in case data set |
area.scale |
scale the unit of area. e.g $10^6$: if your spatial coordinates are metres and you want intensity in cases per km2 |
sex |
possible subsetting of cases and population, set |
... |
additional arguments. When |
If model
is numeric, it's assumed to be a vector of rates, with the names of the elements corresponding to columns of the population data set. Names do not need to match exactly (can have M in one set of names, male in another for instance).
Otherwise, model
is passed to the predict
function.
Returns a new population data set contains expected number of cases, observed number of cases and SMR. It has the same format as the population data set which put into the function.
data(kentucky) kentucky = terra::unwrap(kentucky) kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") terra::values(kentucky2)[1:10,grep("^F|^M", names(kentucky2), invert=TRUE)] theBreaks = signif(seq(0, max(kentucky2$SMR, na.rm=TRUE), len=9),1) theCol = heat.colors(length(theBreaks)-1) terra::plot(kentucky2, col=theCol, breaks = theBreaks) legend('left', fill=theCol, legend = paste(theBreaks[-length(theBreaks)], ' - ', theBreaks[-1]))
data(kentucky) kentucky = terra::unwrap(kentucky) kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") terra::values(kentucky2)[1:10,grep("^F|^M", names(kentucky2), invert=TRUE)] theBreaks = signif(seq(0, max(kentucky2$SMR, na.rm=TRUE), len=9),1) theCol = heat.colors(length(theBreaks)-1) terra::plot(kentucky2, col=theCol, breaks = theBreaks) legend('left', fill=theCol, legend = paste(theBreaks[-length(theBreaks)], ' - ', theBreaks[-1]))
A function to calculate the standard rate according to the Canadian standard population data set from year 1991.
getStdRate(relativeRate, model, referencePopulation, scale = 1e+05)
getStdRate(relativeRate, model, referencePopulation, scale = 1e+05)
relativeRate |
the relative cancer rate calculated by glmmBUGS of different sex and age group of people from ontario . |
model |
Model to standardize to, either |
referencePopulation |
population to standardize to |
scale |
compute the expected rate per ‘scale’ number of people. |
numeric value, incidence rate in reference population.
Lutong Zhou
data(kentucky) kentucky = terra::unwrap(kentucky) kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") data(referencepop) newpop <- getStdRate(kentucky2$SMR, larynxRates, referencepop, scale=100000) newpop[1:10]
data(kentucky) kentucky = terra::unwrap(kentucky) kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") data(referencepop) newpop <- getStdRate(kentucky2$SMR, larynxRates, referencepop, scale=100000) newpop[1:10]
calls the function of the same name in INLA
inla.models()
inla.models()
a list
Data set contains the information of population, by age, sex, and census subdivision.
data('kentucky')
data('kentucky')
A SpatialPolygonsDataFrame
of Kentucky boundaries and populations,
case numbers for each county, and a vector of cancer rates by age and sex group.
larynx
is a data.frame
of cancer case counts by county,
obtained from http://www.cancer-rates.info and are for a single
deliberately unspecified year.
kentucky
contains country boundaries and populations.
kentuckyTract
contains census tract boundaries and populations.
library('terra') data('kentucky') kentucky = terra::unwrap(kentucky) head(larynx) 10^5*larynxRates[paste(c("M","F"), 50, sep="_")] kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") names(kentucky2) length(kentucky2) data('kentuckyTract') kentuckyTract = unwrap(kentuckyTract) length(kentuckyTract) if(require('mapmisc', quietly=TRUE)) { mycol = colourScale(kentucky2$SMR, breaks=10, dec=-log10(0.5), style='quantile') plot(kentucky2, col=mycol$plot, border='#00000040') legendBreaks('topright', mycol) } else { terra::plot(kentucky2) } breaks = c(0,1,seq(2, ceiling(max(kentucky2$SMR,na.rm=TRUE)),by=2)) thecol = terrain.colors(length(breaks)-1) plot(kentucky2, col = thecol[cut(kentucky2$SMR, breaks,include.lowest=TRUE)] ) legend("topleft", pch=15, pt.cex=2.5, adj=c(0,15), legend=rev(breaks), col=c(NA, rev(thecol)))
library('terra') data('kentucky') kentucky = terra::unwrap(kentucky) head(larynx) 10^5*larynxRates[paste(c("M","F"), 50, sep="_")] kentucky2 = getSMR(kentucky, larynxRates, larynx, regionCode="County") names(kentucky2) length(kentucky2) data('kentuckyTract') kentuckyTract = unwrap(kentuckyTract) length(kentuckyTract) if(require('mapmisc', quietly=TRUE)) { mycol = colourScale(kentucky2$SMR, breaks=10, dec=-log10(0.5), style='quantile') plot(kentucky2, col=mycol$plot, border='#00000040') legendBreaks('topright', mycol) } else { terra::plot(kentucky2) } breaks = c(0,1,seq(2, ceiling(max(kentucky2$SMR,na.rm=TRUE)),by=2)) thecol = terrain.colors(length(breaks)-1) plot(kentucky2, col = thecol[cut(kentucky2$SMR, breaks,include.lowest=TRUE)] ) legend("topleft", pch=15, pt.cex=2.5, adj=c(0,15), legend=rev(breaks), col=c(NA, rev(thecol)))
Writes a graph file from an adjacency matrix suitable for use with INLA.
nbToInlaGraph(adjMat, graphFile="graph.dat", region.id = attributes(adjMat)$region.id)
nbToInlaGraph(adjMat, graphFile="graph.dat", region.id = attributes(adjMat)$region.id)
adjMat |
An object of class |
graphFile |
name of file to save adjacency matrix to. |
region.id |
names of regions |
This function correctly handles regions which have zero neighbours.
A vector of names and indices
Patrick Brown
data('kentucky') kentucky = terra::unwrap(kentucky) # remove all the neighbours Ballard county kSub = kentucky[-c(2,20,79),] adjMat = terra::adjacent(kSub) attributes(adjMat)$region.id = kSub$County nFile = tempfile() nbRes = nbToInlaGraph(adjMat, nFile) # Ballard is region 3 nbRes['Ballard'] # note that Ballard has no neighbours table(adjMat[,'from']==3) cat(readLines(nFile, n=5), sep='\n') # there will be a warning about zero neighbours junk = bym(poverty ~ 1, data=kSub, family='gaussian', num.threads=3)
data('kentucky') kentucky = terra::unwrap(kentucky) # remove all the neighbours Ballard county kSub = kentucky[-c(2,20,79),] adjMat = terra::adjacent(kSub) attributes(adjMat)$region.id = kSub$County nFile = tempfile() nbRes = nbToInlaGraph(adjMat, nFile) # Ballard is region 3 nbRes['Ballard'] # note that Ballard has no neighbours table(adjMat[,'from']==3) cat(readLines(nFile, n=5), sep='\n') # there will be a warning about zero neighbours junk = bym(poverty ~ 1, data=kSub, family='gaussian', num.threads=3)
Data set contains the information of population, by age, sex, and census subdivision.
data(popdata)
data(popdata)
A SpatialPolygonsDataFrame object, which needs the sp
package for full functionality.
This data is from the 2006 Census of canada offering by Statistics Canada web site, www12.statcan.gc.ca/english/census06/data/highlights/agesex/Index_PR.cfm?Lang=E&Geo=CSD&Table=1
data('popdata') popdata = terra::unwrap(popdata) head(terra::values(popdata)) terra::plot(popdata)
data('popdata') popdata = terra::unwrap(popdata) head(terra::values(popdata)) terra::plot(popdata)
A data set contains population and age sex group from year 1991.
data(referencepop)
data(referencepop)
Data frame with columns POPULATION, sex, and age for the Canada 1991 population.
data frame with rows representing age-sex groups, first column giving proportion of Canada 1991 population in that group, and subsequent columns giving sex and start of age range for each group
data(referencepop) head(referencepop) sum(referencepop$POPULATION)
data(referencepop) head(referencepop) sum(referencepop$POPULATION)