Skip to content

Commit

Permalink
Upload code.
Browse files Browse the repository at this point in the history
  • Loading branch information
SharpRT committed Dec 16, 2021
1 parent e1eb4cc commit 342d615
Show file tree
Hide file tree
Showing 55 changed files with 2,609 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
25 changes: 25 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Package: NFRRPhilippines
Type: Package
Title: NFRRPhilippines
Version: 0.3.0
Author: Ryan T. Sharp
Maintainer: Ryan T. Sharp <[email protected]>
Description: A set of tools for fitting an epidemiological model to the Newton Fund Rice Research experimental data.
License: file LICENCE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.2
Imports:
reshape2
,plyr
,dplyr
,minpack.lm
,deSolve
,grid
,ggplot2
,gtable
,RColorBrewer
,scales
,utils
,DEoptim
,stats4
37 changes: 37 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by roxygen2: do not edit by hand

export(add_finalNSampleWeek)
export(calc_sumPlotWeek)
export(clean_experimentData)
export(clean_plotWeekSum)
export(fitOut_philippinesStandard)
export(load_all_experimentData)
export(load_experimentData)
export(load_sampleDatesFile)
export(modelOut_philippinesStandard)
export(model_resExpJoint2)
export(model_susExpJoint2)
export(name_basePath)
export(name_experimentDataFile)
export(name_institute)
export(opt_conjugateParams)
export(opt_get_compModDatJoint)
export(opt_get_control)
export(opt_get_fitFuncParams)
export(opt_is_converged)
export(opt_paramsNLS)
export(opt_residFunc_standard)
export(opt_residuals)
export(opt_run_fitFunc)
export(opt_tryFit)
export(setup_env)
export(solve_opt1D)
export(update_dead_experimentData)
import(bindrcpp)
import(dplyr)
importFrom(deSolve,rk)
importFrom(plyr,join)
importFrom(reshape2,melt)
importFrom(rlang,sym)
importFrom(stats,nls)
importFrom(utils,read.csv)
18 changes: 18 additions & 0 deletions NFRR_Philippines.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
43 changes: 43 additions & 0 deletions R/add_finalNSampleWeek.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' [Title]
#'
#' [Description]
#' @param data data produced from \code{\link{calc_sumPlotWeek}}
#' @param allDataTypes data frame giving a series of year/season/site combinations to load
#' @param filepath location of folder containing sample dates data files
#' @export
add_finalNSampleWeek = function(
data, allDataTypes, filepath,
country, experiment
){
oriFNSW = read.csv(
paste0(filepath, "\\", country, "\\", experiment, "\\", "Final_N_Week.csv")
)

oriDat = data
finDat = NULL
for(i in 1:nrow(allDataTypes)){ #i=1 i=2
year = allDataTypes$year[i]
season = allDataTypes$season[i]
site = allDataTypes$site[i]

finalNSampleWeek = oriFNSW[
as.character(oriFNSW$Season)==season
& oriFNSW$Year==year
& as.character(oriFNSW$Site)==site
,
]

data = oriDat[
as.character(oriDat$season)==season
& oriDat$year==year
& as.character(oriDat$site)==site
,
]
data$Final_N_Sample_Week = finalNSampleWeek$Final_N_Sample_Week[match(data$Plot_f, finalNSampleWeek$Plot)]

finDat = rbind(finDat,data)
}


return(finDat)
}
46 changes: 46 additions & 0 deletions R/calc_sumPlotWeek.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' @importFrom reshape2 melt
#' @importFrom utils read.csv
#' @importFrom rlang sym
#' @import dplyr
#' @import bindrcpp
NULL

#' Sum infected (I) and total tillers (N=H+I) from each plot-week
#'
#' Sums the number of infections and total number of tillers from each plot-week for a given site.
#' @param site site name (one of "Pila","Victoria","Malayantoc","Maligaya").
#' @param year the year the data was collected
#' @param season the season the data was collected
#' @return dataframe with the date/DAT/Week# sampled, number of tillers infected and total number of tillers.
#' @export
calc_sumPlotWeek = function(data=NULL, country=NULL, experiment=NULL, site=NULL, year=NULL, season=NULL, sumVar="numTillers", filepath){

if(is.null(data)){
datMelt = load_all_experimentData(allDataTypes = data.frame(country=country, experiment=experiment, site=site, year=year, season=season), filepath=filepath)
} else{
datMelt = data
}

datMelt[,sumVar][datMelt[,sumVar]<0] = 0 #set any negative values to 0. - any deduction of -ve counts to N must be done prior to this.

symVar=sym(sumVar)

plotWeekSum = datMelt %>%
group_by_(~Week, ~Line, ~Rep, ~type, ~site, ~year, ~season, ~country, ~DAT, ~WeekDate, ~Plot_f) %>%
summarise(test = sum(!!symVar)) %>%
as.data.frame()
names(plotWeekSum)[match("test",names(plotWeekSum))] = sumVar

country = unique(plotWeekSum$country)
if(length(country)!=1) stop("Multiple countries in plotWeekSum!")

#Add finalNSampleWeek
plotWeekSum = add_finalNSampleWeek(data=plotWeekSum, allDataTypes=data.frame(site=site,year=year,season=season), country=country, experiment=experiment, filepath=filepath)
plotWeekSum$isNRepeated = (plotWeekSum$Week > plotWeekSum$Final_N_Sample_Week) & plotWeekSum$type=="N"

#Add plot type
plotWeekSum$Plot_Type = c("sus", "res")[as.numeric(grepl("Res",plotWeekSum$Plot_f))+1]
plotWeekSum = clean_plotWeekSum(site=site, year=year, season=season, plotWeekSum=plotWeekSum)

return(plotWeekSum)
}
23 changes: 23 additions & 0 deletions R/clean_plotWeekSum.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' [Title]
#'
#' [Description]
#' @param site site name (one of "Pila","Victoria","Malayantoc","Maligaya").
#' @param year the year the data was collected
#' @param season the season the data was collected
#' @param type Either "I" or "N".
#' @param plotWeekSum data produced from \code{\link{calc_sumPlotWeek}}
#' @return plotWeekSum with compromised data removed.
#' @export
clean_plotWeekSum = function(site, year, season, type, plotWeekSum){
if(site=="Malayantoc" & year==2016 & season=="Wet"){
plotWeekSum = plotWeekSum[!(
grepl("Sus", plotWeekSum$Plot_f)
&plotWeekSum$Week==5
&plotWeekSum$type=="I"
)
,
]
}

return(plotWeekSum)
}
47 changes: 47 additions & 0 deletions R/fitOut_philippinesStandard.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Philippines - Standard Residual; Published Fit
#'
#' Code to produce model for Philippines - Standard Redisual; Published Fit
#' @param year year of experiment
#' @param season season of experiment
#' @param site site name of site where experiment was performed
#' @return model values
#' @export
fitOut_philippinesStandard = function(year, season, site, compare=F, compareA=T, residFuncName = "standard", filepath, env=NULL){

#==============================================================================#
# Fit model to epidemiological data using nls.lm (Sharp, 2017)
#==============================================================================#

library(stats4)

if(is.null(env)){
env <- new.env()
setup_env(year, season, site, filepath=filepath, env=env)
}

fitFunc = env$fitFunc
initParams = env$initParams
lower = env$lower
upper = env$upper
tColStr = env$tColStr
plotSum = env$plotSum
N0 = env$N0
args = env$args
modelFunc = env$modelFunc
joint = env$joint
solved = env$solved
aOrder = env$aOrder
control = env$control
residFunc = env$residFunc

fitval = opt_tryFit(
fitFunc=fitFunc,initParams=initParams,lower=lower,upper=upper,tColStr=tColStr,plotSum=plotSum,N0=N0,args=args,
modelFunc=modelFunc, joint=joint, solved=solved,
aOrder=aOrder,
control=control,
residFunc=residFunc
,maxRecs = 10
)

return(fitval)
}
Loading

0 comments on commit 342d615

Please sign in to comment.