Skip to content

Commit

Permalink
Merge pull request #9 from karlropkins/main
Browse files Browse the repository at this point in the history
updating pls functions
  • Loading branch information
karlropkins authored Oct 18, 2023
2 parents abd2440 + 9d00268 commit 72993d1
Show file tree
Hide file tree
Showing 16 changed files with 1,071 additions and 317 deletions.
3 changes: 2 additions & 1 deletion .Rproj.user/shared/notebooks/paths
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/ITS/projects/NERC_TRANSISTION/events/Clean Air Networks Conference/naei estimate.Rmd="1BB48545"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/_isolateContribution&breakPointAnalysis_KR_20230824.R="F18B98A3"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/pkg/respeciate/_projects/_paper_01_IntroToRespeciate/MS Access Versions/speciate_5.2_0/test.R="FCE2E494"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/pkg/respeciate/_projects/marylebone02/_marylebone_metals_03.Rmd="63CDB89D"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/pkg/respeciate/_projects/marylebone03/_marylebone_analysis_pls_01.Rmd="F2B723A3"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/pkg/respeciate/_projects/marylebone03/_marylebone_metals_03.Rmd="D2C38DFE"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/pkg/respeciate/_projects/match01/_match_notes_01.Rmd="A446C96C"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/pkg/respeciate/test/respeciate/.Rbuildignore="BAFF788D"
C:/Users/trakradmin/OneDrive - University of Leeds/Documents/pkg/respeciate/test/respeciate/.Rproj.user/shared/notebooks/paths="DB89FCB7"
Expand Down
18 changes: 10 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
Package: respeciate
Title: Speciation profiles for gases and aerosols
Version: 0.2.3
Date: 2023-09-23
Description: Acess to the US.EPA Speciate (v5.2) tool, to generate speciation profiles for
gases and particles. More details in Simon et al (2010) <doi:10.5094/APR.2010.026>.
Version: 0.2.4
Date: 2023-10-14
Description: Acess to the US.EPA Speciate (v5.2) tool, to generate speciation profiles
for gases and particles. More details in Simon et al (2010)
<doi:10.5094/APR.2010.026>.
Type: Package
Authors@R: c( person(given = "Sergio", family = "Ibarra-Espinosa", role = c("aut", "cre"),
email = "[email protected]", comment = c(ORCID = "0000-0002-3162-1905")),
person(given = "Karl", family = "Ropkins", role = c("aut"), email =
"[email protected]", comment = c(ORCID = "0000-0002-0294-6997")) )
Authors@R: c( person(given = "Sergio", family = "Ibarra-Espinosa", role = c("aut",
"cre"), email = "[email protected]", comment = c(ORCID =
"0000-0002-3162-1905")), person(given = "Karl", family = "Ropkins", role =
c("aut"), email = "[email protected]", comment = c(ORCID =
"0000-0002-0294-6997")) )
License: MIT + file LICENSE
URL: https://github.com/atmoschem/respeciate
BugReports: https://github.com/atmoschem/respeciate/issues
Expand Down
7 changes: 5 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

S3method(plot,respeciate)
S3method(print,respeciate)
S3method(print,rsp_pls)
S3method(summary,respeciate)
export(find_code)
export(pls_fit_parent)
export(pls_fit_species)
export(pls_plot)
export(pls_plot_profile)
export(pls_plot_species)
export(pls_rebuild)
export(pls_refit_species)
export(pls_report)
export(sp_average_profile)
export(sp_build_rsp_x)
export(sp_dcast)
export(sp_dcast_profile)
export(sp_dcast_species)
Expand Down Expand Up @@ -40,7 +43,7 @@ export(spq_pm.simplified)
export(spx_btex)
export(spx_copy)
export(spx_n_alkane)
import(data.table)
importFrom(data.table,":=")
importFrom(grDevices,as.graphicsAnnot)
importFrom(grDevices,cm.colors)
importFrom(grDevices,colorRampPalette)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Version 0.0 - Release Notes

* [0.2.4]
* released 2023-11-14
* added functions: pls_fit_species
* added draft rsp_pls and rsp_x object classes
* added draft sp_build_rsp_x
* moved from importing data.table to data.table::as.data.table, etc...

* [0.2.3]
* released 2023-09-26
* added functions: sp_rescale_species, sp_dcast, sp_dcast_species
Expand Down
46 changes: 43 additions & 3 deletions R/respeciate.generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# multiple functions

#' @description When supplied a \code{respeciate}
#' object, \code{\link{print}} manages its appearance.
#' object or similar, \code{\link{print}} manages its appearance.
#' @description When supplied a \code{respeciate}
#' object, \code{\link{plot}} provides a basic plot
#' output. This uses base function \code{\link{barplot}};
Expand Down Expand Up @@ -74,6 +74,43 @@ print.respeciate <- function(x, n = NULL, ...){



#############################################
#############################################
## print.rsp_pls
#############################################
#############################################

#' @rdname respeciate.generics
#' @method print rsp_pls
#' @export

# notes
############################

#currently just a print tidier
# so user not catching this does not get a
# screen dump of model lists

#needs work if we want it to actually be useful...

print.rsp_pls <- function(x, n = NULL, ...){
#expecting list of nls models
report <- "respeciate pls model:"
if(!is.list(x)){
report <- paste(report, "\n Suspect!\n", sep="")
} else{
temp <- unlist(lapply(x, function(x) !is.null(x)))
temp <- length(temp[temp])
report <- paste(report, "\n list of ", length(x), " profile models",
"\n (", temp, " good)\n", sep="")
}
cat(report)
}





#' @rdname respeciate.generics
#' @method plot respeciate
#' @export
Expand Down Expand Up @@ -278,7 +315,7 @@ summary.respeciate <-

#v0.3 summary

xx <- as.data.table(object)
xx <- data.table::as.data.table(object)

.xargs <- list(...)
.max.n <- if("max.n" %in% names(.xargs)){
Expand Down Expand Up @@ -339,7 +376,7 @@ summary.respeciate <-
if(!.silent){
if(nrow(out) > .max.n){
print(head(out[c(1,2,5,6)], n = .max.n))
cat(" [forestortened - showing ", .max.n, " of ", nrow(out), "]",
cat(" [forestortened - showing ", .max.n, " of ", nrow(out), "]\n",
sep="")
} else {
print(out[c(1,2,5,6)])
Expand Down Expand Up @@ -518,6 +555,9 @@ rsp_build_respeciate <-
}





###########################
#split respeciate by profile
###########################
Expand Down
170 changes: 163 additions & 7 deletions R/sp.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @name sp
#' @title sp_ functions
#' @aliases sp_profile
#' @aliases sp_profile sp_build_rsp_x


#' @description sp function to get profiles from the R (re)SPECIATE archive
Expand All @@ -14,17 +14,48 @@
#' the \code{code}s as a named \code{PROFILE_NAME} column.
#' @param include.refs logical, include profile reference information when
#' getting the requested profile(s) from the archive, default \code{FALSE}.
#' @param x (for \code{sp_build}s only) A \code{data.frame} or similar to
#' be converted into a \code{respeciate} object for comparison with
#' SPECIATE profiles.
#' @param profile_name,profile_code (for \code{sp_build}s only;
#' \code{character}) The name of the column in \code{x} containing
#' profile name and code, respectively. If not already named according
#' to SPECIATE conventions, at least one of these will need to be assigned.
#' @param species_name,species_id (for \code{sp_build}s only;
#' \code{character}) The name of the column in \code{x} containing
#' species name and id, respectively. If not already named according
#' to SPECIATE conventions, at least one of these will need to be assigned.
#' @param value (for \code{sp_build}s only; \code{character}) The name
#' of the column in \code{x} containing measurement values. If not already
#' named according to SPECIATE conventions, this will need to be assigned.
#' @param ... additional arguments, currently ignored.
#' @return \code{sp_profile} returns a object of
#' \code{respeciate} class, a \code{data.frame} containing a
#' (re)SPECIATE profile.
#' @note The option \code{include.refs} adds profile source reference
#'
#' \code{sp_build}s attempt to build and return a (re)SPECIATE-like profile
#' that can be compared with with data in re(SPECIATE).
#' @note With \code{sp_profile}:
#'
#' The option \code{include.refs} adds profile source reference
#' information to the returned \code{respeciate} data set. The default option
#' is to not include these because some profiles have several associated
#' references and including these replicates records, once per reference.
#' \code{respeciate} code is written to handle this but if you are developing
#' own methods or code and include references in any profile build you may be
#' biasing some analyses in favor of those multiple-reference profile unless
#' you check and account such cases.
#'
#' With \code{sp_build}s:
#'
#' It is particularly IMPORTANT that you use EPA SPECIATE conventions when
#' assign species information if you want to compare your data with SPECIATE
#' profiles. Currently, working on option to improve on this (and very happy
#' to discuss if anyone has ideas), but current best suggestion is: (1)
#' identify the SPECIATE species code for all the species in your data set,
#' and (2) assign these as \code{species_id} when \code{sp_build}ing. The
#' function will associate the \code{species_name}.
#'
#' @references
#' Simon, H., Beck, L., Bhave, P.V., Divita, F., Hsu, Y., Luecken, D.,
#' Mobley, J.D., Pouliot, G.A., Reff, A., Sarwar, G. and Strum, M., 2010.
Expand Down Expand Up @@ -94,11 +125,11 @@ sp_profile <- function(code, include.refs=FALSE) {
call.=FALSE)
}

PROFILES <- as.data.table(sysdata$PROFILES)
SPECIES <- as.data.table(sysdata$SPECIES)
SPECIES_PROPERTIES <- as.data.table(sysdata$SPECIES_PROPERTIES)
PROFILE_REFERENCE <- as.data.table(sysdata$PROFILE_REFERENCE)
REFERENCES <- as.data.table(sysdata$REFERENCES)
PROFILES <- data.table::as.data.table(sysdata$PROFILES)
SPECIES <- data.table::as.data.table(sysdata$SPECIES)
SPECIES_PROPERTIES <- data.table::as.data.table(sysdata$SPECIES_PROPERTIES)
PROFILE_REFERENCE <- data.table::as.data.table(sysdata$PROFILE_REFERENCE)
REFERENCES <- data.table::as.data.table(sysdata$REFERENCES)

##########################
#testing tolower below
Expand Down Expand Up @@ -131,6 +162,131 @@ sp_profile <- function(code, include.refs=FALSE) {



##############################
# sp_build_rsp_x
##############################

#' @rdname sp
#' @export

sp_build_rsp_x <-
function(x, profile_code, profile_name,
species_name, species_id,
value, ...){

# light build for a rsp_x data object
# might need spec_mwt

# current build rules
# profile_name: if not there, if sent in call use,
# else if there use profile_code
# profile_code: if not there, if sent in call use,
# else if there use profile_name
# species_name: if not there, if sent in call use,
# else if there use use species_id to look-up
# if any missing, warn
# species_id: if not there, if sent in call use,
# else if there use species_name to look-up
# if any missing, warn
# .value: if not there, if sent in call use.
# WEIGHT_PERCENT:if not there, if sent in call use.
# else if there use .value to look-up

# don't build/error if any of these missing and end of build

# redundant?
# currently not using ...
.x.args <- list(...)

#if not there and sent in call

#note:
#current making all BUT values, character class
if(!"PROFILE_NAME" %in% names(x) & (!missing(profile_name))){
if(!profile_name %in% names(x)){
stop("sp_build> '", as.character(profile_name)[1],
"' not in 'x'...", sep="", call. = FALSE)
}
x$PROFILE_NAME <- as.character(x[, profile_name])
}
if(!"PROFILE_CODE" %in% names(x) & (!missing(profile_code))){
if(!profile_code %in% names(x)){
stop("sp_build> '", as.character(profile_code)[1],
"' not in 'x'...", sep="", call. = FALSE)
}
x$PROFILE_CODE <- as.character(x[, profile_code])
}
if(!"SPECIES_NAME" %in% names(x) & (!missing(species_name))){
if(!species_name %in% names(x)){
stop("sp_build> '", as.character(species_name)[1],
"' not in 'x'...", sep="", call. = FALSE)
}
x$SPECIES_NAME <- as.character(x[, species_name])
}
if(!"SPECIES_ID" %in% names(x) & (!missing(species_id))){
if(!species_id %in% names(x)){
stop("sp_build> '", as.character(species_id)[1],
"' not in 'x'...", sep="", call. = FALSE)
}
x$SPECIES_ID <- as.character(x[, species_id])
}
if(!".value" %in% names(x) & (!missing(value))){
if(!value %in% names(x)){
stop("sp_build> '", as.character(value)[1],
"' not in 'x'...", sep="", call. = FALSE)
}
x$.value <- x[, value]
}

#if still not there try to assign using what is there

if("PROFILE_NAME" %in% names(x) & !"PROFILE_CODE" %in% names(x)){
x$PROFILE_CODE <- x$PROFILE_NAME
}
if("PROFILE_CODE" %in% names(x) & !"PROFILE_NAME" %in% names(x)){
x$PROFILE_NAME <- x$PROFILE_CODE
}
test <- c("SPECIES_NAME", "SPECIES_ID")[c("SPECIES_NAME", "SPECIES_ID")
%in% names(x)]
if(length(test)==1){
#one there, other as look-up
.tmp <- data.table::as.data.table(
sysdata$SPECIES_PROPERTIES[c("SPECIES_NAME", "SPECIES_ID")]
)
.tmp$SPECIES_NAME <- as.character(.tmp$SPECIES_NAME)
.tmp$SPECIES_ID <- as.character(.tmp$SPECIES_ID)
x <- merge(data.table::as.data.table(x),
data.table::as.data.table(.tmp),
all.x=TRUE, all.y=FALSE, allow.cartesian=TRUE)
x <- as.data.frame(x)
}
if(".value" %in% names(x) & !"WEIGHT_PERCENT" %in% names(x)){
x$WEIGHT_PERCENT <- x$.value
}

#test what we have

.test <- c("PROFILE_NAME", "PROFILE_CODE", "SPECIES_NAME", "SPECIES_ID",
".value", "WEIGHT_PERCENT")
.test <- .test[!.test %in% names(x)]
if(length(.test)>0){
stop("sp_build> bad data structure, expected column(s) missing/unassigned:\n",
paste(.test, sep="", collapse = ", "), "\n", sep="", call.=FALSE)
}
if(any(is.na(x$SPECIES_ID)) | any(is.na(x$SPECIES_NAMES))){
warning("sp_build> suspect species data, values missing:\n",
"(respeciate needs valid species entries)\n",
sep="", call.=FALSE)
}

x <- as.data.frame(x)
class(x) <- c("rsp_x", "respeciate", "data.frame")
x
}







Expand Down
7 changes: 5 additions & 2 deletions R/sp.average.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ sp_average_profile <- function(x, code = NULL, name = NULL, method = 1,
#save class to return as is..
# thinking about this
tmp <- class(x)
xx <- as.data.table(x)
xx <- data.table::as.data.table(x)

#save profiles
test <- unique(x$PROFILE_CODE)
Expand All @@ -109,7 +109,10 @@ sp_average_profile <- function(x, code = NULL, name = NULL, method = 1,
out <- xx[,
.(PROFILE_NAME = PROFILE_NAME[1],
SPECIES_NAME = SPECIES_NAME[1],
SPEC_MW = SPEC_MW[1],
##########################
# testing
#SPEC_MW = SPEC_MW[1],
##########################
.total = sum(.value, na.rm = TRUE),
.value = mean(.value, na.rm = TRUE),
.n = length(.value[!is.na(.value)]),
Expand Down
Loading

0 comments on commit 72993d1

Please sign in to comment.