code
stringlengths
1
13.8M
vpc_cens <- function(sim = NULL, obs = NULL, psn_folder = NULL, bins = "jenks", n_bins = 8, bin_mid = "mean", obs_cols = NULL, sim_cols = NULL, software = "auto", show = NULL, stratify = NULL, stratify_color = NULL, ci = c(0.05, 0.95), uloq = NULL, lloq = NULL, plot = FALSE, xlab = "Time", ylab = "Probability of <LOQ", title = NULL, smooth = TRUE, vpc_theme = NULL, facet = "wrap", labeller = NULL, vpcdb = FALSE, verbose = FALSE) { if(is.null(uloq) & is.null(lloq)) { stop("You have to specify either a lower limit of quantification (lloq=...) or an upper limit (uloq=...).") } if(!is.null(uloq) & !is.null(lloq)) { stop("You have to specify either a lower limit of quantification (lloq=...) or an upper limit (uloq=...), but you can't specify both.") } if(is.null(lloq)) { type <- "right-censored" } if(is.null(uloq)) { type <- "left-censored" } if(is.null(obs) & is.null(sim)) { stop("At least a simulation or an observation dataset are required to create a plot!") } if(!is.null(psn_folder)) { if(!is.null(obs)) { obs <- read_table_nm(paste0(psn_folder, "/m1/", dir(paste0(psn_folder, "/m1"), pattern="original.npctab")[1])) } if(!is.null(sim)) { sim <- read_table_nm(paste0(psn_folder, "/m1/", dir(paste0(psn_folder, "/m1"), pattern="simulation.1.npctab")[1])) } software = "nonmem" } if (!is.null(obs)) { software_type <- guess_software(software, obs) } else { software_type <- guess_software(software, sim) } if(!is.null(stratify)) { if(!is.null(obs)) { check_stratification_columns_available(obs, stratify, "observation") } if(!is.null(sim)) { check_stratification_columns_available(sim, stratify, "simulation") } } if(!is.null(stratify_color)) { if(!is.null(obs)) { check_stratification_columns_available(obs, stratify_color, "observation") } if(!is.null(obs)) { check_stratification_columns_available(sim, stratify_color, "simulation") } } show <- replace_list_elements(show_default, show) cols <- define_data_columns(sim, obs, sim_cols, obs_cols, software_type) if(!is.null(obs)) { old_class <- class(obs) class(obs) <- c(software_type, old_class) } if(!is.null(sim)) { old_class <- class(sim) class(sim) <- c(software_type, old_class) } if(!is.null(obs)) { obs <- filter_dv(obs, verbose) obs <- format_vpc_input_data(obs, cols$obs, lloq, uloq, stratify, bins, FALSE, 0, "observed", verbose) } if(!is.null(sim)) { sim <- filter_dv(sim, verbose) sim <- format_vpc_input_data(sim, cols$sim, NULL, NULL, stratify, bins, FALSE, 0, "simulated", verbose) sim$sim <- add_sim_index_number(sim) } stratify_original <- stratify if(!is.null(stratify_color)) { if (is.null(stratify)) { stratify <- stratify_color } if (length(stratify_color) > 1) { stop("Error: please specify only 1 stratification variable for color!") } if (!stratify_color %in% stratify) { stratify_original <- stratify stratify <- c(stratify, stratify_color) } } if (class(bins) != "numeric") { if(!is.null(obs)) { bins <- auto_bin(obs, type = bins, n_bins = n_bins) } else { bins <- auto_bin(sim, type = bins, n_bins = n_bins) } if (is.null(bins)) { msg("Automatic binning unsuccessful, try increasing the number of bins, or specify vector of bin separators manually.", verbose) } } bins <- unique(bins) if(!is.null(obs)) { obs <- bin_data(obs, bins, "idv") } if(!is.null(sim)) { sim <- bin_data(sim, bins, "idv") } if(!is.null(lloq)) { cens <- "left" limit <- lloq } else { cens <- "right" limit <- uloq } if (!is.null(sim)) { tmp1 <- sim %>% dplyr::group_by(strat, sim, bin) vpc_dat <- tmp1 %>% dplyr::summarise(ploq = loq_perc(dv, limit = limit, cens = cens), mn_idv = mean(idv)) %>% dplyr::group_by(strat, bin) %>% dplyr::summarise(q50.low = quantile(ploq, ci[1]), q50.med = quantile(ploq, 0.5), q50.up = quantile(ploq, ci[2]), bin_mid = mean(mn_idv)) %>% dplyr::ungroup() vpc_dat$bin_min <- rep(bins[1:(length(bins)-1)], length(unique(vpc_dat$strat)))[vpc_dat$bin] vpc_dat$bin_max <- rep(bins[2:length(bins)], length(unique(vpc_dat$strat)))[vpc_dat$bin] if(bin_mid == "middle") { vpc_dat$bin_mid <- apply(dplyr::bind_cols(vpc_dat$bin_min, vpc_dat$bin_max), 1, mean) } } else { vpc_dat <- NULL } if(!is.null(obs)) { tmp <- obs %>% dplyr::group_by(strat,bin) aggr_obs <- tmp %>% dplyr::summarise(obs50 = loq_perc(dv, limit = lloq, cens = cens), bin_mid = mean(idv)) %>% dplyr::ungroup() aggr_obs$bin_min <- rep(bins[1:(length(bins)-1)], length(unique(aggr_obs$strat)) )[aggr_obs$bin] aggr_obs$bin_max <- rep(bins[2:length(bins)], length(unique(aggr_obs$strat)) )[aggr_obs$bin] if(bin_mid == "middle") { aggr_obs$bin_mid <- apply(dplyr::bind_cols(aggr_obs$bin_min, aggr_obs$bin_max), 1, mean) } } else { aggr_obs <- NULL } if (!is.null(stratify_original)) { if (length(stratify) == 2) { vpc_dat$strat1 <- unlist(strsplit(as.character(vpc_dat$strat), ", "))[(1:length(vpc_dat$strat)*2)-1] vpc_dat$strat2 <- unlist(strsplit(as.character(vpc_dat$strat), ", "))[(1:length(vpc_dat$strat)*2)] aggr_obs$strat1 <- unlist(strsplit(as.character(aggr_obs$strat), ", "))[(1:length(aggr_obs$strat)*2)-1] aggr_obs$strat2 <- unlist(strsplit(as.character(aggr_obs$strat), ", "))[(1:length(aggr_obs$strat)*2)] } } show$obs_dv = FALSE show$obs_ci = FALSE show$obs_median = TRUE show$sim_median = FALSE show$sim_median_ci = TRUE show$pi_as_area = FALSE show$pi_ci = FALSE show$pi = FALSE vpc_db <- list(sim = sim, vpc_dat = vpc_dat, stratify = stratify, stratify_original = stratify_original, stratify_color = stratify_color, aggr_obs = aggr_obs, obs = obs, bins = bins, facet = facet, labeller = labeller, type = "censored", xlab = xlab, ylab = ylab) if(vpcdb) { return(vpc_db) } else { pl <- plot_vpc(db = vpc_db, show = show, vpc_theme = vpc_theme, smooth = smooth, log_y = FALSE, title = title) return(pl) } }
PV_pre_triang_dis=function(data,years=10){ app=rep(NA,years) for(i in 1:years) app[i]=triangular_moments_dis_U(data,i) PV=1+sum(app[1:years-1]) return(PV) }
do.dne <- function(X, label, ndim=2, numk=max(ceiling(nrow(X)/10),2), preprocess=c("center","scale","cscale","decorrelate","whiten")){ aux.typecheck(X) n = nrow(X) p = ncol(X) ndim = as.integer(ndim) if (!check_ndim(ndim,p)){stop("* do.dne : 'ndim' is a positive integer in [1, numk = as.integer(numk) if (!check_NumMM(numk,1,n/2,compact=FALSE)){stop("* do.dne : 'numk' should be an integer in [2,nrow(X)/2).")} if (missing(preprocess)){ algpreprocess = "center" } else { algpreprocess = match.arg(preprocess) } label = check_label(label, n) ulabel = unique(label) if (any(is.na(label))||(any(is.infinite(label)))){stop("* Supervised Learning : any element of 'label' as NA or Inf will simply be considered as a class, not missing entries.") } tmplist = aux.preprocess.hidden(X,type=algpreprocess,algtype="linear") trfinfo = tmplist$info pX = tmplist$pX nbdtype = c("knn",numk) nbdsymmetric = "union" nbdstruct = aux.graphnbd(pX,method="euclidean", type=nbdtype,symmetric=nbdsymmetric) nbdmask = nbdstruct$mask matF = array(0,c(n,n)) for (i in 1:(n-1)){ for (j in (i+1):n){ if (nbdmask[i,j]==TRUE){ if (label[i]==label[j]){ matF[i,j] = 1.0 matF[j,i] = 1.0 } else { matF[i,j] = -1.0 matF[j,i] = -1.0 } } } } matS = diag(rowSums(matF))-matF costobj = t(pX)%*%(matS-matF)%*%pX projection = aux.adjprojection(RSpectra::eigs(costobj, ndim, which="SR")$vectors) result = list() result$Y = pX%*%projection result$trfinfo = trfinfo result$projection = projection return(result) }
structure <- function (.Data, ...) { if(is.null(.Data)) warning("Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.\n Consider 'structure(list(), *)' instead.") attrib <- list(...) if(length(attrib)) { specials <- c(".Dim", ".Dimnames", ".Names", ".Tsp", ".Label") attrnames <- names(attrib) m <- match(attrnames, specials) ok <- !is.na(m) if(any(ok)) { replace <- c("dim", "dimnames", "names", "tsp", "levels") names(attrib)[ok] <- replace[m[ok]] } if(any(attrib[["class", exact = TRUE]] == "factor") && typeof(.Data) == "double") storage.mode(.Data) <- "integer" attributes(.Data) <- c(attributes(.Data), attrib) } .Data }
library(bmp) posey <- c(30, 167, 332, 457, 822, 1016, 1199, 1437, 1621, 1770, 1924, 2101, 2251, 2442, 2594, 2757, 2918, 3072, 3205, 3356, 3526, 3685, 4068, 4217) susac <- c(751, 1286, 1485, 1666, 2030, 2187) v <- list.files("buster_posey_catching/") v <- v[order(as.numeric(unlist(lapply(strsplit(v, split="i"), "[[", 1))))] setwd("buster_posey_catching/") full <- list() for(i in 1:length(v)) { full[[i]] <- read.bmp(v[i]) } setwd("../") posz <- vector() for(i in 2:length(full)) { posz[i] <- sum(abs(full[[i]][1:250,1:250,2:4]-full[[i-1]][1:250,1:250,2:4])>1) print(i) } posm <- list() for(i in 1:length(posey)) { posm[[i]] <- posz[posey[i]:(posey[i]+100)] } v <- list.files("andrew_susac_catching/") v <- v[order(as.numeric(unlist(lapply(strsplit(v, split="i"), "[[", 1))))] setwd("andrew_susac_catching/") full <- list() for(i in 1:length(v)) { full[[i]] <- read.bmp(v[i]) } setwd("../") susz <- vector() for(i in 2:length(full)) { susz[i] <- sum(abs(full[[i]][1:250,1:250,2:4]-full[[i-1]][1:250,1:250,2:4])>1) print(i) } susm <- list() for(i in 1:length(susac)) { susm[[i]] <- susz[susac[i]:(susac[i]+100)] } posv <- vector() susv <- vector() for(i in 1:100) { posv[i] <- mean(unlist(lapply(posm, "[[", i))) } for(i in 1:100) { susv[i] <- mean(unlist(lapply(susm, "[[", i))) } plot(posv[1:20], type="l", col="red", lwd=3, ylim=c(80000, 120000), xlab="Time", xaxt="no", ylab="Total Pixel Movement") lines(susv[1:20], type="l", col="blue", lwd=3) axis(side=1, at=c(0,5,10,15,20), labels=as.character(c(0,5,10,15,20)*.05))
QA_Results <- data.table::CJ( Group = c(0, 1, 2, 3), xregs = c(0, 1, 2, 3), Trans = c(TRUE, FALSE), Training = "Failure", Forecast = "Failure" ) for (run in seq_len(QA_Results[, .N])) { if (QA_Results[run, Group] == 0) { groupvars <- NULL ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } else if (QA_Results[run, Group] == 1) { groupvars <- "MarketingSegments" ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } else if (QA_Results[run, Group] == 2) { groupvars <- c("MarketingSegments", "MarketingSegments2") ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } else if (QA_Results[run, Group] == 3) { groupvars <- c("MarketingSegments", "MarketingSegments2", "MarketingSegments3") ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } keep <- names(LeadsData) keep <- keep[!keep %in% c(groupvars, "CalendarDateColumn")] ModelData[LeadsData, paste0(keep) := mget(paste0("i.", keep))] setwd("C:/Users/Bizon/Documents/GitHub") TestModel <- tryCatch( { RemixAutoML::AutoLightGBMFunnelCARMA( data = ModelData, GroupVariables = groupvars, BaseFunnelMeasure = keep, ConversionMeasure = "Appointments", ConversionRateMeasure = NULL, CohortPeriodsVariable = "CohortDays", CalendarDate = "CalendarDateColumn", CohortDate = "CohortDateColumn", PartitionRatios = c(0.70, 0.20, 0.10), TruncateDate = NULL, TimeUnit = "days", TransformTargetVariable = QA_Results[run, Trans], TransformMethods = c("Asinh", "Asin", "Log", "LogPlus1", "Sqrt", "Logit"), AnomalyDetection = list(tstat_high = 3, tstat_low = -2), Jobs = c("eval", "train"), SaveModelObjects = FALSE, ModelID = "ModelTest", ModelPath = getwd(), MetaDataPath = NULL, DebugMode = TRUE, NumOfParDepPlots = 1L, EncodingMethod = "credibility", NThreads = parallel::detectCores() / 2, CalendarTimeGroups = c("days", "weeks", "months"), CohortTimeGroups = c("days", "weeks"), CalendarVariables = c("wday", "mday", "yday", "week", "month", "quarter", "year"), HolidayGroups = c("USPublicHolidays", "EasterGroup", "ChristmasGroup", "OtherEcclesticalFeasts"), HolidayLookback = NULL, CohortHolidayLags = c(1L, 2L, 7L), CohortHolidayMovingAverages = c(3L, 7L), CalendarHolidayLags = c(1L, 2L, 7L), CalendarHolidayMovingAverages = c(3L, 7L), ImputeRollStats = -0.001, CalendarLags = list("day" = c(1L, 2L, 7L, 35L, 42L), "week" = c(5L, 6L, 10L, 12L, 25L, 26L)), CalendarMovingAverages = list("day" = c(7L, 14L, 35L, 42L), "week" = c(5L, 6L, 10L, 12L, 20L, 24L), "month" = c(6L, 12L)), CalendarStandardDeviations = NULL, CalendarSkews = NULL, CalendarKurts = NULL, CalendarQuantiles = NULL, CalendarQuantilesSelected = "q50", CohortLags = list("day" = c(1L, 2L, 7L, 35L, 42L), "week" = c(5L, 6L)), CohortMovingAverages = list("day" = c(7L, 14L, 35L, 42L), "week" = c(5L, 6L), "month" = c(1L, 2L)), CohortStandardDeviations = NULL, CohortSkews = NULL, CohortKurts = NULL, CohortQuantiles = NULL, CohortQuantilesSelected = "q50", PassInGrid = NULL, GridTune = FALSE, BaselineComparison = "default", MaxModelsInGrid = 25L, MaxRunMinutes = 180L, MaxRunsWithoutNewWinner = 10L, LossFunction = "regression", EvalMetric = "mae", GridEvalMetric = "mae", Device_Type = "CPU", Input_Model = NULL, Task = "train", Boosting = "gbdt", LinearTree = FALSE, Trees = 50, ETA = 0.10, Num_Leaves = 31, Deterministic = TRUE, Force_Col_Wise = FALSE, Force_Row_Wise = FALSE, Max_Depth = 6, Min_Data_In_Leaf = 20, Min_Sum_Hessian_In_Leaf = 0.001, Bagging_Freq = 1.0, Bagging_Fraction = 1.0, Feature_Fraction = 1.0, Feature_Fraction_Bynode = 1.0, Lambda_L1 = 0.0, Lambda_L2 = 0.0, Extra_Trees = FALSE, Early_Stopping_Round = 10, First_Metric_Only = TRUE, Max_Delta_Step = 0.0, Linear_Lambda = 0.0, Min_Gain_To_Split = 0, Drop_Rate_Dart = 0.10, Max_Drop_Dart = 50, Skip_Drop_Dart = 0.50, Uniform_Drop_Dart = FALSE, Top_Rate_Goss = FALSE, Other_Rate_Goss = FALSE, Monotone_Constraints = NULL, Monotone_Constraints_method = "advanced", Monotone_Penalty = 0.0, Forcedsplits_Filename = NULL, Refit_Decay_Rate = 0.90, Path_Smooth = 0.0, Max_Bin = 255, Min_Data_In_Bin = 3, Data_Random_Seed = 1, Is_Enable_Sparse = TRUE, Enable_Bundle = TRUE, Use_Missing = TRUE, Zero_As_Missing = FALSE, Two_Round = FALSE, Convert_Model = NULL, Convert_Model_Language = "cpp", Boost_From_Average = TRUE, Alpha = 0.90, Fair_C = 1.0, Poisson_Max_Delta_Step = 0.70, Tweedie_Variance_Power = 1.5, Lambdarank_Truncation_Level = 30, Is_Provide_Training_Metric = TRUE, Eval_At = c(1, 2, 3, 4, 5), Num_Machines = 1, Gpu_Platform_Id = -1, Gpu_Device_Id = -1, Gpu_Use_Dp = TRUE, Num_Gpu = 1 ) }, error = function(x) NULL ) if (!is.null(TestModel)) QA_Results[run, Training := "Success"] data.table::fwrite(QA_Results, file = "C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/Testing_Data/AutoLightGBMFunnel_QA.csv") if (!is.null(TestModel)) { if (QA_Results[run, Group] == 0) { groupvars <- NULL ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-NoGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } else if (QA_Results[run, Group] == 1) { groupvars <- "MarketingSegments" ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-OneGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } else if (QA_Results[run, Group] == 2) { groupvars <- c("MarketingSegments", "MarketingSegments2") ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-TwoGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } else if (QA_Results[run, Group] == 3) { groupvars <- c("MarketingSegments", "MarketingSegments2", "MarketingSegments3") ModelData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-ModelData.csv"), key = c(groupvars, "CalendarDateColumn")) if (QA_Results[run, xregs] == 0) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 1) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData-XREGS1.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 2) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData-XREGS2.csv"), key = c(groupvars, "CalendarDateColumn")) } else if (QA_Results[run, xregs] == 3) { LeadsData <- data.table::fread(file = file.path("C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/QA_DataSets/ChainLadder-ThreeGroup-LeadsData-XREGS3.csv"), key = c(groupvars, "CalendarDateColumn")) } } LeadsData <- LeadsData[CalendarDateColumn < "2020-01-05"] Test <- tryCatch( { RemixAutoML::AutoLightGBMFunnelCARMAScoring( TrainData = ModelData, ForwardLookingData = LeadsData, TrainEndDate = ModelData[, max(CalendarDateColumn)], ForecastEndDate = LeadsData[, max(CalendarDateColumn)], TrainOutput = TestModel$ModelOutput, ArgsList = TestModel$ArgsList, ModelPath = NULL, MaxCohortPeriod = 15, DebugMode = TRUE ) }, error = function(x) NULL ) } else { Test <- NULL } if (!is.null(Test)) QA_Results[run, Forecast := "Success"] rm(TestModel, Test) data.table::fwrite(QA_Results, file = "C:/Users/Bizon/Documents/GitHub/RemixAutoML/tests/Testing_Data/AutoLightGBMFunnel_QA.csv") Sys.sleep(5) }
catEffectBootAdaptor<-function (df, index, testFnc = sumSqCat, useResp = TRUE, ...) { if (useResp) respVal <- df$resp else respVal <- df$bkg testFnc(respVal[index], df$cat, ...) }
count_levels_num <- function(x) { .Call('_inspectdf_count_levels_num', PACKAGE = 'inspectdf', x) } count_levels_char <- function(x) { .Call('_inspectdf_count_levels_char', PACKAGE = 'inspectdf', x) } na_numeric <- function(x) { .Call('_inspectdf_na_numeric', PACKAGE = 'inspectdf', x) } na_character <- function(x) { .Call('_inspectdf_na_character', PACKAGE = 'inspectdf', x) } na_logical <- function(x) { .Call('_inspectdf_na_logical', PACKAGE = 'inspectdf', x) } na_integer <- function(x) { .Call('_inspectdf_na_integer', PACKAGE = 'inspectdf', x) }
T <- 50 m <- 10 P <- 5 H <- 2 N_min <- 20 X <- rnorm(T) mspe <- MSPE(X, m1 = T - m + 1, m2 = T, P = P, H = H, N = c(0, N_min:(T-m-H))) N <- mspe$N M <- mspe$mspe h <- 1 plot(mspe, h, N_min = N_min, legend = (h == 1)) idx1_s <- which(M[h, , N == 0] == min(M[h, , N == 0]), arr.ind = TRUE)[1] abline(h = M[h, idx1_s, N == 0], col = idx1_s, lty="dashed", lwd = 2) for (p in 1:p_max) { idx1_ls <- which(M[h, , N != 0] == min(M[h, , N != 0]), arr.ind = TRUE)[1,] idx1_ls_p <- which(M[h, p, N != 0] == min(M[h, p, N != 0]), arr.ind = TRUE)[1] abline(v = N[idx1_ls_p], col = p, lty = "dotted") }
bhl_getunpublisheditems <- function(...) { .Defunct(package = "rbhl", msg = "API method removed") }
if (requiet("testthat") && requiet("insight") && requiet("robustbase")) { data(mtcars) m1 <- lmrob(mpg ~ gear + wt + cyl, data = mtcars) test_that("model_info", { expect_true(model_info(m1)$is_linear) }) test_that("find_predictors", { expect_identical(find_predictors(m1), list(conditional = c("gear", "wt", "cyl"))) expect_identical(find_predictors(m1, flatten = TRUE), c("gear", "wt", "cyl")) expect_null(find_predictors(m1, effects = "random")) }) test_that("find_random", { expect_null(find_random(m1)) }) test_that("get_random", { expect_warning(get_random(m1)) }) test_that("find_response", { expect_identical(find_response(m1), "mpg") }) test_that("get_response", { expect_equal(get_response(m1), mtcars$mpg) }) test_that("get_predictors", { expect_equal(colnames(get_predictors(m1)), c("gear", "wt", "cyl")) }) test_that("get_data", { expect_equal(nrow(get_data(m1)), 32) expect_equal(colnames(get_data(m1)), c("mpg", "gear", "wt", "cyl")) }) test_that("find_formula", { expect_length(find_formula(m1), 1) expect_equal( find_formula(m1), list(conditional = as.formula("mpg ~ gear + wt + cyl")), ignore_attr = TRUE ) }) test_that("find_terms", { expect_equal(find_terms(m1), list( response = "mpg", conditional = c("gear", "wt", "cyl") )) expect_equal(find_terms(m1, flatten = TRUE), c("mpg", "gear", "wt", "cyl")) }) test_that("n_obs", { expect_equal(n_obs(m1), 32) }) test_that("link_function", { expect_equal(link_function(m1)(.2), .2, tolerance = 1e-5) }) test_that("link_inverse", { expect_equal(link_inverse(m1)(.2), .2, tolerance = 1e-5) }) test_that("find_parameters", { expect_equal( find_parameters(m1), list(conditional = c("(Intercept)", "gear", "wt", "cyl")) ) expect_equal(nrow(get_parameters(m1)), 4) expect_equal( get_parameters(m1)$Parameter, c("(Intercept)", "gear", "wt", "cyl") ) }) test_that("is_multivariate", { expect_false(is_multivariate(m1)) }) test_that("find_algorithm", { expect_equal(find_algorithm(m1), list(algorithm = "SM")) }) test_that("find_statistic", { expect_identical(find_statistic(m1), "t-statistic") }) }
fit_clutter <- function(df, age, dh, basal_area, volume, site, plot, .groups=NA, model = "full", keep_model = FALSE){ basal_area2<-basal_area1<-I1<-I2<-volume2<-.<-Reg<-NULL if( missing(df) ){ stop("df not set", call. = F) }else if(!is.data.frame(df)){ stop("df must be a dataframe", call.=F) }else if(length(df)<=1 | nrow(df)<=1){ stop("Length and number of rows of 'df' must be greater than 1", call.=F) } if( missing(age) ){ stop("age not set", call. = F) }else if( !is.character(age) ){ stop("'age' must be a character containing a variable name", call.=F) }else if(length(age)!=1){ stop("Length of 'age' must be 1", call.=F) }else if(forestmangr::check_names(df, age)==F){ stop(forestmangr::check_names(df, age, boolean=F), call.=F) } if( missing(dh) ){ stop("dh not set", call. = F) }else if( !is.character(dh) ){ stop("'dh' must be a character containing a variable name", call.=F) }else if(length(dh)!=1){ stop("Length of 'dh' must be 1", call.=F) }else if(forestmangr::check_names(df, dh)==F){ stop(forestmangr::check_names(df, dh, boolean=F), call.=F) } if( missing(basal_area) ){ stop("basal_area not set", call. = F) }else if( !is.character(basal_area) ){ stop("'basal_area' must be a character containing a variable name", call.=F) }else if(length(basal_area)!=1){ stop("Length of 'basal_area' must be 1", call.=F) }else if(forestmangr::check_names(df, basal_area)==F){ stop(forestmangr::check_names(df, basal_area, boolean=F), call.=F) } if( missing(volume) ){ stop("volume not set", call. = F) }else if( !is.character(volume) ){ stop("'volume' must be a character containing a variable name", call.=F) }else if(length(volume)!=1){ stop("Length of 'volume' must be 1", call.=F) }else if(forestmangr::check_names(df, volume)==F){ stop(forestmangr::check_names(df, volume, boolean=F), call.=F) } if( missing(site) ){ stop("site not set", call. = F) }else if( !is.character(site) ){ stop("'site' must be a character containing a variable name", call.=F) }else if(length(site)!=1){ stop("Length of 'site' must be 1", call.=F) }else if(forestmangr::check_names(df, site)==F){ stop(forestmangr::check_names(df, site, boolean=F), call.=F) } if(missing(plot) && is.null(dplyr::groups(df)) ){ stop("plot not set. plot must be set if data doesn't have any groups", call. = F) }else if(missing(plot) && !is.null(dplyr::groups(df)) ){ plot_syms <- rlang::syms(dplyr::groups(df)) }else if(!is.character(plot)){ stop("plot must be a character", call. = F) }else if(! length(plot)%in% 1:10){ stop("Length of 'plot' must be between 1 and 10", call.=F) }else if(forestmangr::check_names(df,plot)==F){ stop(forestmangr::check_names(df,plot, boolean=F), call.=F) }else{ plot_syms <- rlang::syms(plot) } if(missing(.groups)||any(is.null(.groups))||any(is.na(.groups))||any(.groups==F)||any(.groups=="") ){ .groups_syms <- character() }else if(!is.character(.groups)){ stop(".groups must be a character", call. = F) }else if(! length(.groups)%in% 1:10){ stop("Length of '.groups' must be between 1 and 10", call.=F) }else if(forestmangr::check_names(df,.groups)==F){ stop(forestmangr::check_names(df,.groups, boolean=F), call.=F) }else{ .groups_syms <- rlang::syms(.groups) } if(!is.character( model )){ stop( "'model' must be character", call.=F) }else if(length(model)!=1){ stop("Length of 'model' must be 1", call.=F) }else if(! model %in% c('full', 'mod') ){ stop("'model' must be equal to 'full' or 'mod' ", call. = F) } if(! keep_model %in% c(TRUE, FALSE) ){ stop("keep_model must be equal to TRUE or FALSE", call. = F) } age_sym <- rlang::sym(age) dh_sym <- rlang::sym(dh) basal_area_sym <- rlang::sym(basal_area) volume_sym <- rlang::sym(volume) site_sym <- rlang::sym(site) suppressMessages( struct_form_data <- df %>% dplyr::group_by(!!!.groups_syms, !!!plot_syms, .add=T ) %>% dplyr::transmute( I1 = !!age_sym, I2 = dplyr::lead(!!age_sym), dh = !!dh_sym, dh2 = dplyr::lead(!!dh_sym), basal_area1 = !!basal_area_sym, basal_area2 = dplyr::lead(!!basal_area_sym), volume1 = !!volume_sym, volume2 = dplyr::lead(!!volume_sym), site = !!site_sym ) %>% stats::na.omit() %>% dplyr::mutate( Y1 = log(basal_area2) , X1 = log(basal_area1) * (I1/I2), X2 = 1 - I1/I2 , X3 = (1 - I1/I2) * site , Y2 = log(volume2) , X4 = 1 / I2 , X5 = site ) %>% dplyr::ungroup() ) if(model == "full"){ eq1 <- Y2 ~ X4 + X5 + Y1 eq2 <- Y1 ~ X1 + X2 + X3 system <- list(Volume = eq1, AreaBasal = eq2) inst <- ~ X4 + X5 + X1 + X2 + X3 restrict <- matrix(0, nrow=2, ncol=8) restrict[1,5] <- 1 restrict[2,6] <- 1 restrict.rhs <- c(0, 1) model_fit <- struct_form_data %>% dplyr::group_by( !!!.groups_syms, .add=T ) %>% dplyr::do(Reg = systemfit::systemfit(system, "2SLS", inst = inst, data = ., restrict.matrix = restrict, restrict.rhs = restrict.rhs)) %>% dplyr::rowwise() %>% dplyr::mutate( b0 = stats::coef(Reg)[[1]], b1 = stats::coef(Reg)[[2]], b2 = stats::coef(Reg)[[3]], b3 = stats::coef(Reg)[[4]], a0 = stats::coef(Reg)[[7]], a1 = stats::coef(Reg)[[8]] ) %>% dplyr:: ungroup() }else if(model == "mod" ){ eq1 <- Y2 ~ X4 + X5 + Y1 eq2 <- Y1 ~ X1 + X2 system <- list(Volume = eq1, AreaBasal = eq2) inst <- ~ X4 + X5 + X1 + X2 restrict <- matrix(0, nrow=2, ncol=7) restrict[1,5] <- 1 restrict[2,6] <- 1 restrict.rhs <- c(0, 1) model_fit <- struct_form_data %>% dplyr::group_by( !!!.groups_syms, .add=T ) %>% dplyr::do(Reg = systemfit::systemfit(system, "2SLS", inst = inst, data = ., restrict.matrix = restrict, restrict.rhs = restrict.rhs)) %>% dplyr::rowwise() %>% dplyr::mutate( b0 = stats::coef(Reg)[[1]], b1 = stats::coef(Reg)[[2]], b2 = stats::coef(Reg)[[3]], b3 = stats::coef(Reg)[[4]], a0 = stats::coef(Reg)[[7]] ) %>% dplyr::ungroup() } if(keep_model == F){ model_fit <- as.data.frame(model_fit) model_fit$Reg <- NULL } model_fit$A <- NULL return(model_fit) }
wait_slurm <- function(x, ...) UseMethod("wait_slurm") wait_slurm.slurm_job <- function(x, ...) { wait_slurm.integer(get_job_id(x), ...) } wait_slurm.integer <- function(x, timeout = -1, freq = 0.1, force = TRUE, ...) { if (opts_slurmR$get_debug()) { warning("waiting is not available in debug mode.", call. = FALSE) return() } else if (!slurm_available()) stopifnot_slurm() if (!is.finite(x)) stop("The job ID is not an integer: ", x, ". Can't wait for non-integer job ids.", call. = FALSE) time0 <- Sys.time() while(TRUE) { Sys.sleep(freq) s <- status(x) if (force && s == -1L) { next } else if (!force && s == -1L) { print(s) break } njobs <- attr(s, "njobs") if (njobs > 1L) { ncompleted <- length(attr(s, "failed")) + length(attr(s, "done")) if (ncompleted == njobs) break } else if (s %in% c(0L, 99L)) break if (timeout > 0) { seconds <- difftime(Sys.time(), time0, units = "secs") if (seconds > timeout) { warning("Timeout after ", seconds, " seconds.", call. = FALSE, immediate. = TRUE) return(invisible(NULL)) } } } invisible(NULL) }
test_that("compare state works correctly", { loc <- tempfile("watcher") dir.create(loc) empty <- dir_state(loc) expect_equal(length(empty), 0) file.create(file.path(loc, "test-1.txt")) one <- dir_state(loc) expect_equal(length(one), 1) expect_equal(basename(names(one)), "test-1.txt") diff <- compare_state(empty, one) expect_equal(diff$n, 1) expect_equal(basename(diff$added), "test-1.txt") write.table(mtcars, file.path(loc, "test-1.txt")) diff <- compare_state(one, dir_state(loc)) expect_equal(diff$n, 1) expect_equal(basename(diff$modified), "test-1.txt") file.rename(file.path(loc, "test-1.txt"), file.path(loc, "test-2.txt")) diff <- compare_state(one, dir_state(loc)) expect_equal(diff$n, 2) expect_equal(basename(diff$deleted), "test-1.txt") expect_equal(basename(diff$added), "test-2.txt") diff <- compare_state( c(file1 = "62da2", file2 = "e14a6", file3 = "6e6dd"), c(file1 = "62da2", file2 = "e14a6", file21 = "532fa", file3 = "3f4sa") ) expect_equal(diff$n, 2) expect_equal(basename(diff$added), "file21") expect_equal(basename(diff$modified), "file3") }) test_that("watcher works correctly", { skip_on_ci() skip_on_os("windows") skip_on_cran() if (Sys.which("bash") == "") { skip("bash not available") } if (system("bash -c 'which touch'", ignore.stdout = TRUE) != 0L) { skip("touch (or which) not available") } loc <- tempfile("watcher") dir.create(loc) code_path <- file.path(loc, "R") test_path <- file.path(loc, "tests") dir.create(code_path) dir.create(test_path) delayed.bash.cmd <- function(command) { system(paste0("bash -c 'sleep 1;", command, "'"), wait = FALSE) } add.code.file <- function(file.name) { delayed.bash.cmd(paste0("touch ", file.path(code_path, file.name))) } remove.code.file <- function(file.name) { delayed.bash.cmd(paste0("rm ", file.path(code_path, file.name))) } test.added <- function(added, deleted, modified) { expect_equal(length(added), 1) expect_equal(grepl("test1.R", added), TRUE) expect_equal(length(deleted), 0) expect_equal(length(modified), 0) FALSE } test.removed <- function(added, deleted, modified) { expect_equal(length(added), 0) expect_equal(length(deleted), 1) expect_equal(grepl("test1.R", deleted), TRUE) expect_equal(length(modified), 0) FALSE } add.code.file("test1.R") watch(c(code_path, test_path), test.added) remove.code.file("test1.R") watch(c(code_path, test_path), test.removed) })
AutoLightGBMClassifier <- function( data = NULL, TrainOnFull = FALSE, ValidationData = NULL, TestData = NULL, TargetColumnName = NULL, FeatureColNames = NULL, PrimaryDateColumn = NULL, IDcols = NULL, WeightsColumnName = NULL, CostMatrixWeights = c(1,0,0,1), EncodingMethod = 'credibility', OutputSelection = c('Importances', 'EvalPlots', 'EvalMetrics', 'Score_TrainData'), model_path = NULL, metadata_path = NULL, DebugMode = FALSE, SaveInfoToPDF = FALSE, ModelID = 'TestModel', ReturnFactorLevels = TRUE, ReturnModelObjects = TRUE, SaveModelObjects = FALSE, NumOfParDepPlots = 3L, Verbose = 0L, GridTune = FALSE, grid_eval_metric = 'Utility', BaselineComparison = 'default', MaxModelsInGrid = 10L, MaxRunsWithoutNewWinner = 20L, MaxRunMinutes = 24L*60L, PassInGrid = NULL, input_model = NULL, task = 'train', device_type = 'CPU', NThreads = parallel::detectCores() / 2, objective = 'binary', metric = 'binary_logloss', boosting = 'gbdt', LinearTree = FALSE, Trees = 50L, eta = NULL, num_leaves = 31, deterministic = TRUE, force_col_wise = FALSE, force_row_wise = FALSE, max_depth = NULL, min_data_in_leaf = 20, min_sum_hessian_in_leaf = 0.001, bagging_freq = 0, bagging_fraction = 1.0, feature_fraction = 1.0, feature_fraction_bynode = 1.0, extra_trees = FALSE, early_stopping_round = 10, first_metric_only = TRUE, max_delta_step = 0.0, lambda_l1 = 0.0, lambda_l2 = 0.0, linear_lambda = 0.0, min_gain_to_split = 0, drop_rate_dart = 0.10, max_drop_dart = 50, skip_drop_dart = 0.50, uniform_drop_dart = FALSE, top_rate_goss = FALSE, other_rate_goss = FALSE, monotone_constraints = NULL, monotone_constraints_method = 'advanced', monotone_penalty = 0.0, forcedsplits_filename = NULL, refit_decay_rate = 0.90, path_smooth = 0.0, max_bin = 255, min_data_in_bin = 3, data_random_seed = 1, is_enable_sparse = TRUE, enable_bundle = TRUE, use_missing = TRUE, zero_as_missing = FALSE, two_round = FALSE, convert_model = NULL, convert_model_language = "cpp", boost_from_average = TRUE, is_unbalance = FALSE, scale_pos_weight = 1.0, is_provide_training_metric = TRUE, eval_at = c(1,2,3,4,5), num_machines = 1, gpu_platform_id = -1, gpu_device_id = -1, gpu_use_dp = TRUE, num_gpu = 1) { options(warn = -1) params <- LightGBMArgs(input_model.=input_model, task.=tolower(task), objective.=objective, boosting.=boosting, LinearTree.=LinearTree, Trees.=Trees, eta.=eta, num_leaves.=num_leaves, NThreads.=NThreads, device_type.=tolower(device_type), deterministic.=deterministic, force_col_wise.=force_col_wise, force_row_wise.=force_row_wise, max_depth.=max_depth, min_data_in_leaf.=min_data_in_leaf, min_sum_hessian_in_leaf.=min_sum_hessian_in_leaf, bagging_freq.=bagging_freq, bagging_fraction.=bagging_fraction, feature_fraction.=feature_fraction, feature_fraction_bynode.=feature_fraction_bynode, extra_trees.=extra_trees, early_stopping_round.=early_stopping_round, first_metric_only.=first_metric_only, max_delta_step.=max_delta_step, lambda_l1.=lambda_l1, lambda_l2.=lambda_l2, linear_lambda.=linear_lambda, min_gain_to_split.=min_gain_to_split, drop_rate_dart.=drop_rate_dart, max_drop_dart.=max_drop_dart, skip_drop_dart.=skip_drop_dart, uniform_drop_dart.=uniform_drop_dart, top_rate_goss.=top_rate_goss, other_rate_goss.=other_rate_goss, monotone_constraints.=monotone_constraints, monotone_constraints_method.=monotone_constraints_method, monotone_penalty.=monotone_penalty, forcedsplits_filename.=forcedsplits_filename, refit_decay_rate.=refit_decay_rate, path_smooth.=path_smooth, max_bin.=max_bin, min_data_in_bin.=min_data_in_bin, data_random_seed.=data_random_seed, is_enable_sparse.=is_enable_sparse, enable_bundle.=enable_bundle, use_missing.=use_missing, zero_as_missing.=zero_as_missing, two_round.=two_round, convert_model.=convert_model, convert_model_language.=convert_model_language, boost_from_average.=boost_from_average, alpha.=NULL, fair_c.=NULL, poisson_max_delta_step.=NULL, tweedie_variance_power.=NULL, lambdarank_truncation_level.=NULL, is_unbalance.=is_unbalance, scale_pos_weight.=scale_pos_weight, multi_error_top_k.=NULL, is_provide_training_metric.=is_provide_training_metric, eval_at.=eval_at, gpu_platform_id.=gpu_platform_id, gpu_device_id.=gpu_device_id, gpu_use_dp.=gpu_use_dp, num_gpu.=num_gpu) ArgsList <- c(as.list(environment())) ArgsList[['data']] <- NULL ArgsList[['ValidationData']] <- NULL ArgsList[['TestData']] <- NULL if(SaveModelObjects) { if(!is.null(metadata_path)) { save(ArgsList, file = file.path(metadata_path, paste0(ModelID, "_ArgsList.Rdata"))) } else if(!is.null(model_path)) { save(ArgsList, file = file.path(model_path, paste0(ModelID, "_ArgsList.Rdata"))) } } if(DebugMode) print("Data prep ----") Output <- XGBoostDataPrep(Algo="lightgbm", ModelType="classification", data.=data, ValidationData.=ValidationData, TestData.=TestData, TargetColumnName.=TargetColumnName, FeatureColNames.=FeatureColNames, WeightsColumnName.=WeightsColumnName, IDcols.=IDcols, TransformNumericColumns.=NULL, Methods.=NULL, ModelID.=ModelID, model_path.=model_path, TrainOnFull.=TrainOnFull, SaveModelObjects.=SaveModelObjects, ReturnFactorLevels.=ReturnFactorLevels, EncodingMethod.=EncodingMethod) TransformNumericColumns <- Output$TransformNumericColumns; Output$TransformNumericColumns <- NULL TransformationResults <- Output$TransformationResults; Output$TransformationResults <- NULL FactorLevelsList <- Output$FactorLevelsList; Output$FactorLevelsList <- NULL FinalTestTarget <- Output$FinalTestTarget; Output$FinalTestTarget <- NULL WeightsVector <- Output$WeightsVector; Output$WeightsVector <- NULL datavalidate <- Output$datavalidate; Output$datavalidate <- NULL TargetLevels <- Output$TargetLevels; Output$TargetLevels <- NULL TrainTarget <- Output$TrainTarget; Output$TrainTarget <- NULL TrainMerge <- Output$TrainMerge; Output$TrainMerge <- NULL ValidMerge <- Output$ValidMerge; Output$ValidMerge <- NULL TestTarget <- Output$TestTarget; Output$TestTarget <- NULL datatrain <- Output$datatrain; Output$datatrain <- NULL dataTrain <- Output$dataTrain; Output$dataTrain <- NULL TestMerge <- Output$TestMerge; Output$TestMerge <- NULL TestData <- Output$TestData; Output$TestData <- NULL datatest <- Output$datatest; Output$datatest <- NULL EvalSets <- Output$EvalSets; Output$EvalSets <- NULL dataTest <- Output$dataTest; Output$dataTest <- NULL IDcols <- Output$IDcols; Output$IDcols <- NULL Names <- Output$Names; rm(Output) if(!is.null(WeightsColumnName)) { params[["weight_column"]] <- which(WeightsColumnName %chin% names(dataTrain)) - 1L } ExperimentalGrid <- NULL; BestGrid <- NULL if(DebugMode) print("Grid tuning ----") if(GridTune) { Output <- LightGBMGridTuner(ModelType="classification", TrainOnFull.=TrainOnFull, DebugMode.=DebugMode, params.=params, num_iterations.=params$num_iterations, max_depth.=params$max_depth, eta.=params$eta, num_leaves.=params$num_leaves, min_data_in_leaf.=params$min_data_in_leaf, bagging_freq.=params$bagging_freq, bagging_fraction.=params$bagging_fraction, feature_fraction.=params$feature_fraction, feature_fraction_bynode.=params$feature_fraction_bynode, lambda_l1.=params$lambda_l1, lambda_l2.=params$lambda_l2, LossFunction=NULL, EvalMetric=eval_metric, grid_eval_metric.=grid_eval_metric, CostMatrixWeights=CostMatrixWeights, TargetColumnName.=TargetColumnName, datatrain.=datatrain, dataTest.=dataTest, TestData.=TestData, EvalSets.=EvalSets, TestTarget.=TestTarget, FinalTestTarget.=FinalTestTarget, TargetLevels.=NULL, MaxRunsWithoutNewWinner=MaxRunsWithoutNewWinner, MaxModelsInGrid=MaxModelsInGrid, MaxRunMinutes=MaxRunMinutes, BaselineComparison.=BaselineComparison, SaveModelObjects=SaveModelObjects, metadata_path=metadata_path, model_path=model_path, ModelID=ModelID, NumLevels.=NULL) ExperimentalGrid <- Output$ExperimentalGrid BestGrid <- Output$BestGrid } if(DebugMode) print("Final Params ----") params <- LightGBMFinalParams(params.=params, GridTune.=GridTune, PassInGrid.=PassInGrid, TrainOnFull.=TrainOnFull, BestGrid.=BestGrid, Trees.=params[["Trees"]], eta.=params[["eta"]], num_leaves.=params[["num_leaves"]], max_depth.=params[["max_depth"]], min_data_in_leaf.=params[["min_data_in_leaf"]], bagging_freq.=params[["bagging_freq"]], bagging_fraction.=params[["bagging_fraction"]], feature_fraction.=params[["feature_fraction"]], feature_fraction_bynode.=params[["feature_fraction_bynode"]]) if(DebugMode) print("Build model ----") model <- lightgbm::lgb.train(params=params, data=datatrain, valids=EvalSets, nrounds = 5L) if(DebugMode) print("Save Model ----") if(SaveModelObjects) lightgbm::lgb.save(booster=model, filename=file.path(model_path, paste0(ModelID, ".txt"))) if(DebugMode) print("TrainData + ValidationData Scoring + Shap ----") if("score_traindata" %chin% tolower(OutputSelection) && !TrainOnFull) { predict <- data.table::as.data.table(predict(model, as.matrix(dataTrain))) if(!is.null(datavalidate)) { predict_validate <- data.table::as.data.table(predict(model, as.matrix(dataTest))) predict <- data.table::rbindlist(list(predict, predict_validate)) data.table::setnames(predict, names(predict), "Predict") rm(predict_validate) } Output <- XGBoostValidationData(model.=model, TestData.=NULL, ModelType="classification", TrainOnFull.=TRUE, TestDataCheck=FALSE, FinalTestTarget.=FinalTestTarget, TestTarget.=TestTarget, TrainTarget.=TrainTarget, TrainMerge.=TrainMerge, TestMerge.=TestMerge, dataTest.=dataTest, data.=dataTrain, predict.=predict, TargetColumnName.=TargetColumnName, SaveModelObjects. = SaveModelObjects, metadata_path.=metadata_path, model_path.=model_path, ModelID.=ModelID, LossFunction.=NULL, TransformNumericColumns.=TransformNumericColumns, GridTune.=GridTune, TransformationResults.=TransformationResults, TargetLevels.=NULL) TrainData <- Output$ValidationData; rm(Output) if(!"Predict" %chin% names(TrainData)) data.table::setnames(TrainData, "V1", "Predict") } else { TrainData <- NULL } if(DebugMode) print("Grid Score Model ----") predict <- predict(object = model, if(!is.null(TestData)) as.matrix(TestData) else if(!is.null(ValidationData) && !TrainOnFull) as.matrix(dataTest) else as.matrix(dataTrain)) if(DebugMode) print("Validation, Importance, Shap data ----") Output <- XGBoostValidationData(ModelType="classification", TestDataCheck=!is.null(TestData), TrainOnFull.=TrainOnFull, model.=model, TargetColumnName.=TargetColumnName, SaveModelObjects.=SaveModelObjects, metadata_path.=metadata_path, model_path.=model_path, ModelID.=ModelID, TestData.=TestData, TestTarget.=TestTarget, FinalTestTarget.=FinalTestTarget, TestMerge.=TestMerge, dataTest.=dataTest, TrainTarget.=TrainTarget, predict.=predict, TransformNumericColumns.=TransformNumericColumns, TransformationResults.=TransformationResults, GridTune.=GridTune, data.=dataTrain) VariableImportance <- Output$VariableImportance; Output$VariableImportance <- NULL ValidationData <- Output$ValidationData; rm(Output) if(DebugMode) print("Running BinaryMetrics()") EvalMetricsList <- list() EvalMetrics2List <- list() if("evalmetrics" %chin% tolower(OutputSelection)) { if("score_traindata" %chin% tolower(OutputSelection) && !TrainOnFull) { EvalMetricsList[["TrainData"]] <- BinaryMetrics(ClassWeights.=NULL, CostMatrixWeights.=CostMatrixWeights, SaveModelObjects.=FALSE, ValidationData.=TrainData, TrainOnFull.=TrainOnFull, TargetColumnName.=TargetColumnName, ModelID.=ModelID, model_path.=model_path, metadata_path.=metadata_path, Method = "threshold") EvalMetrics2List[["TrainData"]] <- BinaryMetrics(ClassWeights.=NULL, CostMatrixWeights.=CostMatrixWeights, SaveModelObjects.=FALSE, ValidationData.=TrainData, TrainOnFull.=TrainOnFull, TargetColumnName.=TargetColumnName, ModelID.=ModelID, model_path.=model_path, metadata_path.=metadata_path, Method = "bins") if(SaveModelObjects) { if(!is.null(metadata_path)) { data.table::fwrite(EvalMetricsList[['TestData']], file = file.path(metadata_path, paste0(ModelID, "_Test_EvaluationMetrics.csv"))) } else if(!is.null(model_path)) { data.table::fwrite(EvalMetricsList[['TestData']], file = file.path(model_path, paste0(ModelID, "_Test_EvaluationMetrics.csv"))) } } } EvalMetricsList[["TestData"]] <- BinaryMetrics(ClassWeights.=NULL, CostMatrixWeights.=CostMatrixWeights, SaveModelObjects.=FALSE, ValidationData.=ValidationData, TrainOnFull.=TrainOnFull, TargetColumnName.=TargetColumnName, ModelID.=ModelID, model_path.=model_path, metadata_path.=metadata_path, Method = "threshold") EvalMetrics2List[["TestData"]] <- BinaryMetrics(ClassWeights.=NULL, CostMatrixWeights.=CostMatrixWeights, SaveModelObjects.=FALSE, ValidationData.=ValidationData, TrainOnFull.=TrainOnFull, TargetColumnName.=TargetColumnName, ModelID.=ModelID, model_path.=model_path, metadata_path.=metadata_path, Method = "bins") if(SaveModelObjects) { if(!is.null(metadata_path)) { data.table::fwrite(EvalMetricsList[['TestData']], file = file.path(metadata_path, paste0(ModelID, "_Test_EvaluationMetrics.csv"))) } else if(!is.null(model_path)) { data.table::fwrite(EvalMetricsList[['TestData']], file = file.path(model_path, paste0(ModelID, "_Test_EvaluationMetrics.csv"))) } } } if(DebugMode) print("Running ML_EvalPlots()") PlotList <- list() if("evalplots" %chin% tolower(OutputSelection)) { if("score_traindata" %chin% tolower(OutputSelection) && !TrainOnFull) { Output <- ML_EvalPlots(ModelType="classification", DataType = 'Train', TrainOnFull.=TrainOnFull, ValidationData.=TrainData, NumOfParDepPlots.=NumOfParDepPlots, VariableImportance.=VariableImportance, TargetColumnName.=TargetColumnName, FeatureColNames.=FeatureColNames, SaveModelObjects.=SaveModelObjects, ModelID.=ModelID, metadata_path.=metadata_path, model_path.=model_path, LossFunction.=NULL, EvalMetric.=NULL, EvaluationMetrics.=NULL, predict.=NULL) PlotList[["Train_EvaluationPlot"]] <- Output$EvaluationPlot; Output$EvaluationPlot <- NULL PlotList[["Train_ParDepPlots"]] <- Output$ParDepPlots; Output$ParDepPlots <- NULL PlotList[["Train_GainsPlot"]] <- Output$GainsPlot; Output$GainsPlot <- NULL PlotList[["Train_LiftPlot"]] <- Output$LiftPlot; Output$LiftPlot <- NULL PlotList[["Train_ROC_Plot"]] <- Output$ROC_Plot; rm(Output) } Output <- ML_EvalPlots(ModelType="classification", DataType = 'Test', TrainOnFull.=TrainOnFull, ValidationData.=ValidationData, NumOfParDepPlots.=NumOfParDepPlots, VariableImportance.=VariableImportance, TargetColumnName.=TargetColumnName, FeatureColNames.=FeatureColNames, SaveModelObjects.=SaveModelObjects, ModelID.=ModelID, metadata_path.=metadata_path, model_path.=model_path, LossFunction.=NULL, EvalMetric.=NULL, EvaluationMetrics.=NULL, predict.=NULL) PlotList[["Test_EvaluationPlot"]] <- Output$EvaluationPlot; Output$EvaluationPlot <- NULL PlotList[["Test_ParDepPlots"]] <- Output$ParDepPlots; Output$ParDepPlots <- NULL PlotList[["Test_GainsPlot"]] <- Output$GainsPlot; Output$GainsPlot <- NULL PlotList[["Test_LiftPlot"]] <- Output$LiftPlot; Output$LiftPlot <- NULL PlotList[["Test_ROC_Plot"]] <- Output$ROC_Plot; rm(Output) if(!is.null(VariableImportance) && "plotly" %chin% installed.packages()) PlotList[["Train_VariableImportance"]] <- plotly::ggplotly(VI_Plot(Type = "xgboost", VariableImportance)) else if(!is.null(VariableImportance)) PlotList[["Train_VariableImportance"]] <- VI_Plot(Type = "xgboost", VariableImportance) } if(DebugMode) print("Save PDF of model information ----") if("pdfs" %chin% tolower(OutputSelection) && SaveModelObjects) { CatBoostPDF(ModelType="regression", TrainOnFull.=TrainOnFull, SaveInfoToPDF.=SaveInfoToPDF, VariableImportance.=VariableImportance, Interaction.=NULL, model_path.=model_path, metadata_path.=metadata_path) } if(!exists("FactorLevelsList")) FactorLevelsList <- NULL options(warn = 1) if(DebugMode) print("Return objects ----") if(ReturnModelObjects) { return(list( Model = model, TrainData = if(exists("TrainData")) TrainData else NULL, TestData = if(exists("ValidationData")) ValidationData else NULL, PlotList = if(exists("PlotList")) PlotList else NULL, EvaluationMetrics = if(exists("EvalMetricsList")) EvalMetricsList else NULL, EvaluationMetrics2 = if(exists("EvalMetrics2List")) EvalMetrics2List else NULL, VariableImportance = if(exists("VariableImportance")) VariableImportance else NULL, GridMetrics = if(exists("ExperimentalGrid") && !is.null(ExperimentalGrid)) data.table::setorderv(ExperimentalGrid, cols = "EvalMetric", order = -1L, na.last = TRUE) else NULL, ColNames = if(exists("Names")) Names else NULL, FactorLevelsList = if(exists("FactorLevelsList")) FactorLevelsList else NULL, ArgsList = ArgsList)) } }
.build_client <- function(api, encode, version = NULL, progress = NULL, pat = getOption("osfr.pat")) { api <- match.arg(api, c("osf", "wb")) encode <- match.arg(encode, c("form", "multipart", "json", "raw")) server <- Sys.getenv("OSF_SERVER") url <- switch(api, osf = ifelse(nzchar(server), "api.%s.osf.io", "api.osf.io"), wb = ifelse(nzchar(server), "files.us.%s.osf.io", "files.osf.io") ) if (nzchar(server)) url <- sprintf(url, server) headers <- list(`User-Agent` = user_agent()) if (!is.null(pat)) { headers$Authorization <- sprintf("Bearer %s", pat) } if (api == "osf") { headers$`Accept-Header` <- sprintf( "application/vnd.api+json;version=%s", version) } crul::HttpClient$new( url = paste0("https://", url), opts = list( encode = encode ), headers = headers, hooks = list( request = log_request, response = log_response ), progress = progress ) }
BLOSUM62<-function(seqs,label=c(),outFormat="mat",outputFileDist=""){ path.pack=system.file("extdata",package="ftrCOOL") if(length(seqs)==1&&file.exists(seqs)){ seqs<-fa.read(seqs,alphabet="aa") seqs_Lab<-alphabetCheck(seqs,alphabet = "aa",label) seqs<-seqs_Lab[[1]] label<-seqs_Lab[[2]] } else if(is.vector(seqs)){ seqs<-sapply(seqs,toupper) seqs_Lab<-alphabetCheck(seqs,alphabet = "aa",label) seqs<-seqs_Lab[[1]] label<-seqs_Lab[[2]] } else { stop("ERROR: Input sequence is not in the correct format. It should be a FASTA file or a string vector.") } lenSeqs<-sapply(seqs, nchar) numSeqs<-length(seqs) featureMatrix<-matrix(0, nrow = numSeqs, ncol = (lenSeqs[1]*23)) adBlosum<-paste0(path.pack,"/BLOSUM62.csv") Blosum<-read.csv(adBlosum) row.names(Blosum)<-Blosum[,1] Blosum<-Blosum[,-1] Blosum<-as.matrix(Blosum) Blosum<-type.convert(Blosum) nameP1<-rep((1:(lenSeqs[1])),each=23) nameP1<-paste0("aa",nameP1) aaVect<-c("A","C","D","E","F","G","H","I","K","L","M","N","P","Q","R","S","T","V","W","Y","B","Z","X") if(outFormat=="mat"){ if(length(unique(lenSeqs))>1){ stop("ERROR: All sequences should have the same length in 'mat' mode. For sequences with different lengths, please use 'txt' for outFormat parameter") } nameP2<-paste("d",rep(aaVect,lenSeqs[1]),sep = "") colnames(featureMatrix)<-paste(nameP1,nameP2) for(n in 1:numSeqs){ seq=seqs[n] chars=unlist(strsplit(seq,"")) featureVector<-Blosum[,chars] featureVector<-as.vector(featureVector) featureMatrix[n,]<-featureVector } if(length(label)==numSeqs){ featureMatrix<-as.data.frame(featureMatrix) featureMatrix<-cbind(featureMatrix,label) } row.names(featureMatrix)<-names(seqs) return(featureMatrix) } else{ nameSeq<-names(seqs) for(n in 1:numSeqs){ seq<-seqs[n] chars<-unlist(strsplit(seq,split = "")) featureVector<-Blosum[,chars] vect<-as.vector(featureVector) temp<-c(nameSeq[n],vect) temp<-paste(temp,collapse = "\t") write(temp,outputFileDist,append = TRUE) } } }
list2matrix.bas <- function(x, what, which.models = NULL) { namesx <- x$namesx if (is.null(which.models)) which.models <- 1:x$n.models listobj <- x[[what]][which.models] which <- x$which[which.models] n.models <- length(which.models) p <- length(namesx) mat <- matrix(0, nrow = n.models, ncol = p) for (i in 1:n.models) { mat[i, which[[i]] + 1] <- listobj[[i]] } colnames(mat) <- namesx return(mat) } list2matrix.which <- function(x, which.models = NULL) { namesx <- x$namesx listobj <- x$which if (!is.null(which.models)) { listobj <- listobj[which.models] } p <- length(namesx) mat <- t(sapply( listobj, function(x, dimp) { xx <- rep(0, dimp) xx[x + 1] <- 1 xx }, p )) colnames(mat) <- namesx mat } which.matrix <- function(which, n.vars) { mat <- t(sapply( which, function(x, dimp) { xx <- rep(0, dimp) xx[x + 1] <- 1 xx }, n.vars )) mat }
context("elevation utils") testthat::skip_on_cran() raster_poa <- system.file("extdata/poa/poa_elevation.tif", package = "r5r") data_path <- system.file("extdata/poa", package = "r5r") r5r_core <- setup_r5(data_path = data_path, temp_dir = TRUE) test_that("tobler_hiking", { expect_error( tobler_hiking('bananas') ) expect_error( tobler_hiking() ) expect_identical( round( r5r:::tobler_hiking(1)), 33) }) test_that("apply_elevation", { if (requireNamespace("rgdal", quietly = TRUE)) { expect_silent( r5r:::apply_elevation(r5r_core, raster_poa) ) expect_silent( r5r:::apply_elevation(r5r_core, c(raster_poa,raster_poa)) ) } expect_error( r5r:::apply_elevation('bananas', raster_poa) ) expect_error( r5r:::apply_elevation(r5r_core, 'bananas') ) }) stop_r5(r5r_core)
bbase.os <- function(x, K, bdeg = 3, eps = 1e-5, intercept = TRUE) { B <- bs(x, degree = bdeg, df = K + bdeg, intercept = intercept) B }
tcplot <- function (data, u.range, cmax = FALSE, r = 1, ulow = -Inf, rlow = 1, nt = 25, which = 1:npar, conf = 0.95, lty = 1, lwd = 1, type = "b", cilty = 1, ask = nb.fig < length(which) && dev.interactive(), ...){ n <- length(data) data <- sort(data) if (missing(u.range)) { u.range <- c(data[1], data[n - 4]) u.range <- u.range - .Machine$double.eps^0.5 } u <- seq(u.range[1], u.range[2], length = nt) locs <- scls <- shps <- matrix(NA, nrow = nt, ncol = 3) dimnames(locs) <- list(round(u, 2), c("lower", "loc", "upper")) dimnames(shps) <- list(round(u, 2), c("lower", "shape", "upper")) pname <- "mscale" npar <- 2 dimnames(scls) <- list(round(u, 2), c("lower", pname, "upper")) z <- gpdmle(data, u[1], corr = TRUE, ...) stvals <- as.list(round(fitted(z), 3)) for (i in 1:nt) { z <- gpdmle(data, u[i], corr = TRUE, ...) stvals <- as.list(fitted(z)) mles <- fitted(z) stderrs <- z$std.err cnst <- qnorm((1 + conf)/2) shp <- mles["shape"] scl <- mles["scale"] shpse <- stderrs["shape"] sclse <- stderrs["scale"] scl <- scl - shp * u[i] covar <- z$corr[1, 2] * prod(stderrs) sclse <- sqrt(sclse^2 - 2 * u[i] * covar + (u[i] * shpse)^2) scls[i, ] <- c(scl - cnst * sclse, scl, scl + cnst * sclse) shps[i, ] <- c(shp - cnst * shpse, shp, shp + cnst * shpse) } show <- rep(FALSE, npar) show[which] <- TRUE nb.fig <- prod(par("mfcol")) if (ask) { op <- par(ask = TRUE) on.exit(par(op)) } if (show[1]) { matplot(u, scls, type = "n", xlab = "Threshold", ylab = "Modified Scale") lines(u, scls[, 2], lty = lty, lwd = lwd, type = type) segments(u, scls[, 1], u, scls[, 3], lty = cilty) } if (show[2]) { matplot(u, shps, type = "n", xlab = "Threshold", ylab = "Shape") lines(u, shps[, 2], lty = lty, lwd = lwd, type = type) segments(u, shps[, 1], u, shps[, 3], lty = cilty) } rtlist <- list(scales = scls, shapes = shps) invisible(rtlist) }
library(plyr) suppressPackageStartupMessages(library(dplyr)) library(ggplot2) library(readr) gap_dat <- read_tsv("05_gap-merged-with-china-1952.tsv") %>% mutate(country = factor(country), continent = factor(continent)) gap_dat %>% str() gap_dat %>% sapply(function(x) x %>% is.na() %>% sum()) gap_dat$year %>% summary() all.equal(gap_dat$year %>% unique() %>% sort(), 1950:2007) ggplot(gap_dat, aes(x = year)) + geom_histogram(binwidth = 1) gap_dat$country %>% str() country_freq <- gap_dat %>% count(country) ggplot(country_freq, aes(x = country, y = n)) + geom_bar(stat = "identity") (p <- ggplot(country_freq, aes(x = n)) + geom_histogram(binwidth = 1)) p + xlim(c(1, 16)) country_freq$n %>% table() gap_dat$continent %>% levels() gap_dat$continent %>% summary() tmp <- gap_dat %>% group_by(country) %>% summarize(n_continent = n_distinct(continent)) tmp$n_continent %>% table() gap_dat$pop %>% summary(digits = 10) gap_dat[which.min(gap_dat$pop),] gap_dat[which.max(gap_dat$pop),] ggplot(gap_dat,aes(x = pop)) + geom_density() + scale_x_log10() gap_dat$lifeExp %>% summary() ggplot(gap_dat,aes(x = lifeExp)) + geom_density() gap_dat$gdpPercap %>% summary() gap_dat[which.max(gap_dat$gdpPercap),] ggplot(gap_dat,aes(x = gdpPercap)) + geom_density()
"sdtm_ae" "sdtm_cm" "sdtm_dm" "sdtm_ds" "sdtm_ex" "sdtm_lb" "sdtm_mh" "sdtm_qs" "sdtm_relrec" "sdtm_sc" "sdtm_se" "sdtm_suppae" "sdtm_suppdm" "sdtm_suppds" "sdtm_supplb" "sdtm_sv" "sdtm_ta" "sdtm_te" "sdtm_ti" "sdtm_ts" "sdtm_tv" "sdtm_vs"
tree_add_dates <- function(dated_tree = NULL, missing_taxa = NULL, dating_method = "mrbayes", adding_criterion = "random", mrbayes_output_file = "mrbayes_tree_add_dates.nexus") { dated_tree <- tree_check(tree = dated_tree, dated = TRUE) missing_taxa <- missing_taxa_check(missing_taxa = missing_taxa, dated_tree = dated_tree) dating_method <- match.arg(dating_method, c("bladj", "mrbayes")) adding_criterion <- tryCatch(match.arg(adding_criterion, c("random", "taxonomy", "tree")), error = function(e) "random") if (dating_method == "bladj") { if (inherits(missing_taxa, "phylo")) { missing_taxa_phy <- missing_taxa } else { if (is.data.frame(missing_taxa)) { all_taxa <- unique(c(dated_tree$tip.label, levels(missing_taxa$taxon))) } if (is.vector(missing_taxa)) { all_taxa <- unique(c(dated_tree$tip.label, missing_taxa)) } missing_taxa_phy <- get_otol_synthetic_tree(input = all_taxa) } constraint_tree <- suppressWarnings(geiger::congruify.phylo( reference = phylo_tiplabel_space_to_underscore(dated_tree), target = phylo_tiplabel_space_to_underscore(missing_taxa_phy), scale = NA, ncores = 1 )) dated_tree_nodes <- sapply(seq(nrow(constraint_tree$calibrations)), function(i) { phytools::findMRCA( tree = constraint_tree$target, tips = as.character(constraint_tree$calibrations[i, c("taxonA", "taxonB")]), type = "node" ) }) dated_tree_nodes <- dated_tree_nodes - ape::Ntip(constraint_tree$target) missing_taxa_phy$node.label[dated_tree_nodes] <- paste0("cong", seq(nrow(constraint_tree$calibrations))) missing_taxa_phy <- tree_add_nodelabels(tree = missing_taxa_phy) new.phy <- make_bladj_tree(tree = missing_taxa_phy, nodenames = missing_taxa_phy$node.label[dated_tree_nodes], nodeages = sapply(seq(nrow(constraint_tree$calibrations)), function(i) sum(constraint_tree$calibrations[i, c("MinAge", "MaxAge")]) / 2)) } if (dating_method == "mrbayes") { dated_tree <- tree_add_outgroup(tree = dated_tree, outgroup = "an_outgroup") ncalibration <- tree_get_node_data(tree = dated_tree, node_data = c("node_age", "descendant_tips_label")) new.phy <- make_mrbayes_tree(constraint = dated_tree, ncalibration = ncalibration, missing_taxa = missing_taxa, mrbayes_output_file = mrbayes_output_file) new.phy <- ape::drop.tip(new.phy, "an_outgroup") } return(new.phy) } missing_taxa_check <- function(missing_taxa = NULL, dated_tree = NULL) { badformat <- TRUE if (is.data.frame(missing_taxa)) { if ("taxon" %in% names(missing_taxa)) { badformat <- FALSE if (length(missing_taxa) > 1 & !"clade" %in% names(missing_taxa)) { badformat <- TRUE } } } else { missing_taxa_phy <- input_process(missing_taxa) if (inherits(missing_taxa_phy, "phylo")) { phylo_check(phy = dated_tree, dated = TRUE) dtINmt <- dated_tree$tip.labels %in% missing_taxa$tip.labels mtINdt <- missing_taxa$tip.labels %in% dated_tree$tip.labels if (!all(dtINmt)) { warning("not all taxa from dated_tree are in missing_taxa tree") } missing_taxa_pruned <- ape::drop.tip(missing_taxa, missing_taxa$tip.labels[mtINdt]) missing_taxa <- missing_taxa_phy badformat <- FALSE } else { missing_taxa <- as.character(missing_taxa) missing_taxa[which(is.na(missing_taxa))] <- "NA" badformat <- FALSE } } if (length(missing_taxa) == 0) { missing_taxa <- NULL badformat <- FALSE } if (badformat) { stop("missing_taxa must be a character vector with species names, a data frame with taxonomic assignations, a newick character string, a phylo object, or NULL") } return(missing_taxa) }
"dataAGGR"
library(amt) data(amt_fisher) set.seed(123) tr <- make_trast(amt_fisher[1:50, ], res = 5) mini_fisher <- amt_fisher[1:40, ] mcp <- hr_mcp(mini_fisher) loc <- hr_locoh(mini_fisher) kde <- hr_kde(mini_fisher) mini_fisher1 <- amt_fisher[11:50, ] mcp1 <- hr_mcp(mini_fisher1) loc1 <- hr_locoh(mini_fisher1) kde1 <- hr_kde(mini_fisher1, tr = tr) expect_inherits(hr_overlap(mcp, mcp1), "tbl_df") expect_inherits(hr_overlap(list(mcp, mcp1)), "tbl_df") expect_inherits(hr_overlap(kde, kde1), "tbl_df") expect_inherits(hr_overlap(list(kde, kde1)), "tbl_df") expect_error(hr_overlap(kde, kde1, type = "vi")) expect_error(hr_overlap(list(kde, kde1), type = "vi"))
brute_IDs <- function(total.length, redundancy, alphabet, num.tries = 10, available.colors = NULL) { if (missing(alphabet)) { stop("Error: you need to enter an 'alphabet size,' e.g. the number of paint colors you have") } if (missing(total.length)) { stop("Error: you need to enter the total length of the ID, e.g. how many color bands or paint drops on each organism") } if (missing(redundancy)) { stop("Error: you need specify to how many erasure events the IDs should be robust. Note, an increase in robustness requires an increase in the total length of the ID. ") } if (redundancy >= total.length || redundancy == 0) { stop("Error: the code must be robust to at least one erasure. It also cannot be robust to a number of positions equal to or greater than the total length.") } if (!is(num.tries, "numeric")) { stop(paste0("Error: the variable 'num.tries' must be of the class 'numeric,' not '", class(num.tries),".'")) } tester <- function(total.length, redundancy, alphabet) { perms <- rep(list(seq_len(alphabet)),total.length) combos <- as.matrix(expand.grid(perms)) - 1 combo.list <- split(combos, 1:nrow(combos)) names(combo.list) <- NULL x <- sample(1:length(combo.list), 1) new.combs <- combo.list[x] names(new.combs) <- NULL combo.list <- combo.list[stringdist::seq_distmatrix(combo.list, new.combs, method = "hamming")[, length(new.combs)] > redundancy] names(combo.list) <- 1:length(combo.list) while (length(combo.list) > 0) { x <- sample(1:length(combo.list), 1) new.combs[length(new.combs) + 1] <- (combo.list[x]) combo.list <- combo.list[stringdist::seq_distmatrix(combo.list, new.combs, method = "hamming")[, length(new.combs)] > redundancy] if (length(combo.list) != 0) { names(combo.list) <- 1:length(combo.list) } } return(new.combs) } temp1 <- NULL temp2 <- 0 for (i in 1:num.tries) { temp1 <- invisible(tester(total.length, redundancy, alphabet)) if (length(temp1) > length(temp2)) temp2 <- temp1 } temp2 <- codes_to_colors(temp2, available.colors) return(temp2) }
context("ashr with half-uniform mixture priors") test_that("mixcompdist=+uniform gives all non-negative values for b and zero for a", { set.seed(1); z=rnorm(10); z.ash=ash(z,1,mixcompdist="+uniform") k = length(z.ash$fitted_g$pi) expect_true(all(z.ash$fitted_g$b >= rep(0,k))) expect_equal(z.ash$fitted_g$a,rep(0,k)) }) test_that("mixcompdist=-uniform gives all non-positive values for a and zero for b", { set.seed(1); z=rnorm(10); z.ash=ash(z,1,mixcompdist="-uniform") k = length(z.ash$fitted_g$pi) expect_equal(z.ash$fitted_g$b,rep(0,k)) expect_true(all(z.ash$fitted_g$a <= 0)) })
women names(women) height attach(women) height weight women$height g <- "My First List" h <- c(25, 26, 18, 39) j <- matrix(1:10, nrow=5) k <- c("one", "two", "three") mylist <- list(title=g, ages=h, j, k, women) mylist mylist[[2]] mylist[[5]] plot(x=height, y=weight, type='b', lty=5, pch=11, fg='red', bg='green', col.axis='purple', cex=1.5, cex.axis=2) title(main='Henry Harvin', sub=' MA Course')
stratsamp <- function(n, distribution, parameters, p) { lims <- find_strata(p, distribution, parameters) outmat <- matrix(data = NA, nrow = n, ncol = length(p)-1) counts <- rep(0, length(lims)-1) while (any(counts < n)) { r <- distribution_sampling(1, distribution, parameters) intvl <- findInterval(r, lims) if (counts[intvl] < n) { counts[intvl] <- counts[intvl] + 1 outmat[counts[intvl], intvl] <- r } } outmat }
context("mlc") suppressPackageStartupMessages(library(caret)) set.seed(1) mat <- matrix(rnorm(300), ncol = 3, nrow = 100) colnames(mat) <- letters[1:3] y <- sample(factor(c("a", "b")), 100, replace = TRUE) test_that("fit mlc",{ expect_is( mr <- mlc(mat,y), "list") expect_equal(names(mr), c("a", "b", "levels")) expect_equal(vapply(mr$a, length, numeric(1)), c(m=3,D=1,I=9)) }) test_that("predict mlc",{ mod <- train( mat, y, method = mlcCaret, trControl = trainControl(method = "none")) expect_is(pred <- predict.mlc(mod, mat), "factor") expect_equal(length(pred), nrow(mat)) expect_equal(levels(pred), c("a", "b")) expect_is(prob <- predict.mlc.prob(mod, mat), "matrix") expect_equal(nrow(prob), nrow(mat)) expect_equal(ncol(prob), 2) })
semprobit <- function(formula, W, data, subset, ...) { cl <- match.call() mf <- match.call(expand.dots = FALSE) m <- match(c("formula", "data", "subset"), names(mf), 0L) mf <- mf[c(1L, m)] mf$drop.unused.levels <- TRUE mf[[1L]] <- as.name("model.frame") mf <- eval(mf, parent.frame()) mt <- attr(mf, "terms") y <- model.response(mf, "numeric") if (!is.null(W) && !is.numeric(W) && !inherits(W, "sparseMatrix") && nrow(W) != NROW(y)) stop(gettextf("'W' must be a numeric square matrix, dimension %d should equal %d (number of observations)", NROW(W), NROW(y)), domain = NA) X <- model.matrix(mt, mf, contrasts) sem_probit_mcmc(y, X, W, ...) } sem_probit_mcmc <- function(y, X, W, ndraw=1000, burn.in=100, thinning=1, prior=list(a1=1, a2=1, c=rep(0, ncol(X)), T=diag(ncol(X))*1e12, nu=0, d0=0, lflag = 0), start=list(rho=0.75, beta=rep(0, ncol(X)), sige=1), m=10, showProgress=FALSE, univariateConditionals=TRUE){ timet <- Sys.time() n <- nrow( X ) n1 <- nrow( X ) n2 <- nrow( W ) k <- ncol( X ) I_n <- sparseMatrix(i=1:n, j=1:n, x=1) if (is.null(colnames(X))) colnames(X) <- paste("x",1:k,sep="") if( length(c(which(y == 0 ),which(y == 1))) != length( y ) ){ stop("semprobit: not all y-values are 0 or 1") } if( n1 != n2 && n1 != n ){ stop("semprobit: wrong size of spatial weight matrix W") } if (!inherits(W, "sparseMatrix") || any(diag(W) != 0)) { stop('sarprobit: spatial weights matrix W must be a sparse matrix with zeros in the main diagonal') } ind <- match( n, apply(X,2,sum)) if( is.na(ind) ){ cflag <- 0 p <- k }else if( ind == 1 ){ cflag <- 1 p <- k - 1 }else{ stop("semprobit: intercept term must be in first column of the X-matrix") } rho <- start$rho beta <- start$beta sige <- start$sige c <- rep(0, k) if (is.matrix(prior$T) && ncol(prior$T) == k && isSymmetric(prior$T) && det(prior$T) > 0) { T <- prior$T } else { T <- diag(k)*1e12 } if (is.numeric(prior$nu)) { nu <- prior$nu } else { nu <- 0 } if (is.numeric(prior$d0)) { d0 <- prior$d0 } else { d0 <- 0 } TI <- solve(T) TIc <- TI%*%c S <- I_n - rho * W H <- t(S) %*% S / sige lower <- ifelse(y > 0, 0, -Inf) upper <- ifelse(y > 0, Inf, 0) rmin <- -1 rmax <- 1 ldetflag <- 0 tmp <- sar_lndet(ldetflag, W, rmin, rmax) detval <- tmp$detval a1 <- 1.0 a2 <- 1.0 if(is.numeric(prior$a1)) a1 <- prior$a1 if(is.numeric(prior$a2)) a2 <- prior$a2 u <- runif(thinning * ndraw + burn.in) nrho <- nrow(detval) nmk <- (n-k)/2 detval1 <- detval[,1] detval2 <- detval[,2] B <- matrix(NA, ndraw, k+2) colnames(B) <- c(colnames(X), "sige", "rho") cc <- 0.2 acc <- 0 acc_rate <- rep(NA, thinning * ndraw + burn.in) if (showProgress) { pb <- txtProgressBar(min=0, max=(thinning * ndraw + burn.in), initial=0, style=3) } if(cflag == 0) { namesNonConstantParams <- colnames(X) } else { namesNonConstantParams <- colnames(X)[-1] } z <- y ones <- rep(1, n) W2diag <- diag(t(W)%*%W) ind0 <- which(y == 0) ind1 <- which(y == 1) for (i in (1 - burn.in):(ndraw * thinning)) { SX <- S %*% X tSX <- t(SX) tSXSX <- as.matrix(tSX %*% SX) AI <- solve(tSXSX + sige * TI) Sz <- as.double(S %*% z) b <- as.double(tSX %*% Sz + sige * TIc) b0 <- AI %*% b beta <- as.double(rmvnorm(n=1, mean=b0, sigma=sige*AI)) nu1 <- n + 2*nu e <- as.double(S %*% (z - X %*% beta)) d1 <- 2*d0 + crossprod(e) chi <- rchisq(n=1,df=nu1) sige <- as.double(d1/chi) H <- t(S) %*% S / sige if (univariateConditionals) { dsig <- 1/sige * (ones - rho * rho * W2diag) zvar <- ones/dsig; mu <- X %*% beta zmu <- z - mu A <- (1/sige)* S %*% zmu B2 <- t(S) %*% A Cz <- zmu - zvar*B2 zm <- mu + Cz; z[zvar < 0] <- 0 z[ind0] <- rtnorm(mu=zm[ind0], sd=sqrt(zvar[ind0]), a=-Inf, b=0) z[ind1] <- rtnorm(mu=zm[ind1], sd=sqrt(zvar[ind1]), a=0, b=Inf) z[is.infinite(z) | zvar < 0] <- 0 } if (!univariateConditionals) { mu <- X %*% beta H <- (1/sige)*t(S)%*%S if (m==1) { z <- as.double(rtmvnorm.sparseMatrix(n=1, mean=mu, H=H, lower=lower, upper=upper, burn.in=m, start.value=z)) } else { z <- as.double(rtmvnorm.sparseMatrix(n=1, mean=mu, H=H, lower=lower, upper=upper, burn.in=m)) } } rhox <- c_sem(rho,z,X,beta,sige,I_n,W,detval1,detval2,ones,a1,a2) accept <- 0 rho2 <- rho + cc * rnorm(1) while(accept == 0) { if ((rho2 > rmin) & (rho2 < rmax)) { accept <- 1 } else { rho2 <- rho + cc * rnorm(1) } } rhoy <- c_sem(rho2,z,X,beta,sige,I_n,W,detval1,detval2,ones,a1,a2) ru <- runif(1,0,1) if ((rhoy - rhox) > exp(1)) { p <- 1 } else { ratio <- exp(rhoy-rhox) p <- min(1,ratio) } if (ru < p) { rho <- rho2 acc <- acc + 1 } iter <- i + burn.in acc_rate[iter] <- acc/iter if (acc_rate[iter] < 0.4) { cc <- cc/1.1; } if (acc_rate[iter] > 0.6) { cc <- cc*1.1; } S <- I_n - rho * W H <- t(S) %*% S / sige if (i > 0) { if (thinning == 1) { ind <- i } else if (i%%thinning == 0) { ind <- i%/%thinning } else { next } B[ind,] <- c(beta, sige, rho) } if (showProgress) setTxtProgressBar(pb, i + burn.in) } if (showProgress) close(pb) beta <- colMeans(B)[1:k] sige <- colMeans(B)[k+1] rho <- colMeans(B)[k+2] S <- (I_n - rho * W) fitted.values <- X %*% beta fitted.response <- as.numeric(fitted.values >= 0) results <- NULL results$time <- Sys.time() - timet results$nobs <- n results$nvar <- k results$y <- y results$zip <- n - sum(y) results$beta <- colMeans(B)[1:k] results$sige <- sige results$rho <- colMeans(B)[k+2] results$coefficients <- colMeans(B) results$fitted.values <- fitted.values results$fitted.response <- fitted.response results$ndraw <- ndraw results$nomit <- burn.in results$a1 <- a1 results$a2 <- a2 results$nu <- nu results$d0 <- d0 results$rmax <- rmax results$rmin <- rmin results$tflag <- "plevel" results$lflag <- ldetflag results$cflag <- cflag results$lndet <- detval results$names <- c(colnames(X), "sige", "rho") results$B <- B results$bdraw <- B[,1:k] results$sdraw <- B[,k+1] results$pdraw <- B[,k+2] results$W <- W results$X <- X class(results) <- "semprobit" return(results) } c_sem <- function(rho,y,X,b,sige,I_n,W,detval1,detval2,vi,a1,a2) { i <- findInterval(rho,detval1) if (i == 0) index=1 else index=i detm = detval2[index] z = I_n - rho*W; e = as.double(z %*% (y - X %*% b)) ev = e * sqrt(vi) epe = (crossprod(ev))/(2*sige) cout = as.double(detm - epe) return(cout) } coef.semprobit <- function(object, ...) { if (!inherits(object, "semprobit")) stop("use only with \"semprobit\" objects") return(object$coefficients) } coefficients.semprobit <- function(object, ...) { UseMethod("coef", object) } summary.semprobit <- function(object, var_names=NULL, file=NULL, digits = max(3, getOption("digits")-3), ...){ if (!inherits(object, "semprobit")) stop("use only with \"semprobit\" objects") nobs <- object$nobs nvar <- object$nvar ndraw <- object$ndraw nomit <- object$nomit draws <- object$B bout_mean <- object$coefficients bout_sd <- apply(draws, 2, sd) bout_sig <- 1 - apply(draws, 2, function(x) { ifelse (mean(x) > 0, sum(x > 0), sum(x < 0)) }) / ndraw bout_t <- bout_mean / bout_sd bout_tPval<- (1 - pt( abs(bout_t), nobs ))*2 if( is.null(var_names)){ bout_names<- as.matrix(object$names) }else{ bout_names<- as.matrix(var_names) } if(is.null(file)){file <- ""} write(sprintf("--------MCMC probit with spatial errors ---------"), file, append=T) write(sprintf("Execution time = %6.3f %s", object$time, attr(object$time, "units")) , file, append=T) write(sprintf("N steps for TMVN= %6d" , object$nsteps), file, append=T) write(sprintf("N draws = %6d, N omit (burn-in)= %6d", ndraw, nomit), file, append=T) write(sprintf("N observations = %6d, K covariates = %6d", nobs, nvar) , file, append=T) write(sprintf(" write(sprintf("Min rho = % 6.3f, Max rho = % 6.3f", object$rmin, object$rmax), file, append=T) write(sprintf("--------------------------------------------------"), file, append=T) write(sprintf(""), file, append=T) coefficients <- cbind(bout_mean, bout_sd, bout_sig, bout_t, bout_tPval) dimnames(coefficients) <- list(bout_names, c("Estimate", "Std. Dev", "Bayes p-level", "t-value", "Pr(>|z|)")) printCoefmat(coefficients, digits = digits, signif.stars = getOption("show.signif.stars")) return(invisible(coefficients)) } plot.semprobit <- function(x, which=c(1, 2, 3), ask = prod(par("mfcol")) < length(which) && dev.interactive(), ..., trueparam=NULL) { if (!inherits(x, "semprobit")) stop("use only with \"semprobit\" objects") if (!is.numeric(which) || any(which < 1) || any(which > 3)) stop("'which' must be in 1:3") names <- x$names B <- x$B k <- ncol(B) show <- rep(FALSE, 3) show[which] <- TRUE if (ask) { oask <- devAskNewPage(TRUE) on.exit(devAskNewPage(oask)) } if (show[1L]) { for (i in 1:k) { plot(1:nrow(B), B[,i], type="l", xlab="iteration", ylab=names[i], main=substitute("Trace plot of "*x, list(x=names[i])), ...) if (!is.null(trueparam)) abline(h=trueparam[i], col="red", lty=2) } } if (show[2L]) { for (i in 1:k) { acf(B[,i], main=substitute("ACF of "*x, list(x=names[i])), ...) } } if (show[3L]) { for (i in 1:k) { plot(density(B[,i]), main=substitute("Posterior distribution of "*x, list(x=names[i])), ...) if (!is.null(trueparam)) abline(v=trueparam[i], col="red", lty=2) } } } logLik.semprobit <- function(object, ...) { X <- object$X y <- object$y n <- nrow(X) k <- ncol(X) W <- object$W beta <- object$beta rho <- object$rho sige <- object$sige I_n <- sparseMatrix(i=1:n, j=1:n, x=1) S <- I_n - rho * W D <- diag(1/sqrt(sige*diag(S %*% t(S)))) Xs <- D %*% X F <- pnorm(as.double(Xs %*% beta)) lnL <- sum(log(F[y == 1])) + sum(log((1 - F[y == 0]))) out <- lnL class(out) <- "logLik" attr(out,"df") <- k+2 return(out) } fitted.semprobit <- function(object, ...) { object$fitted.value }
tar_resources_url <- function( handle = NULL ) { out <- resources_url_init( handle = handle ) resources_validate(out) out }
export_amira.path<-function(vertices,filename,Lines=c(1:(dim(vertices)[1]-1)-1,-1),path) { while(tolower(paste(filename,".am",sep=""))%in%list.files(path)!=FALSE){ i<-1 filename<-paste(filename,"(",i,")",sep="") i<-i+1 } cat(paste(" file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "",eol="\n") cat(paste("define Lines ", length(Lines), "\n", sep = ""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste("nVertices ", dim(vertices)[1], "\n\n", sep = ""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste("Parameters {","\n",sep=""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste(' ContentType "HxLineSet"',"\n",sep=""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste("}", "\n\n", sep = ""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste("Lines { int LineIdx } @1","\n",sep=""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste("Vertices { float[3] Coordinates } @2", "\n\n", sep = ""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste(" sep = ""), append = TRUE, sep = "") cat(paste(Lines,"\n",sep=" "), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") cat(paste("\n","@2","\n",sep=""), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") write.table(format(vertices, scientific = F, trim = T), file = paste(path, "/", filename, ".am", sep = ""), sep = " ", append = TRUE, quote = FALSE, row.names = FALSE, col.names = FALSE, na = "") cat(paste("\n",sep=" "), file = paste(path, "/", filename, ".am", sep = ""), append = TRUE, sep = "") }
test_that("translations", { expect_identical(pow(5, 2), 25) expect_identical(phi(0), 0.5) expect_equal(phi(2), 0.9772499, tolerance = 0.0000001) x <- NA log(x) <- log(5) expect_equal(x, 5) expect_equal(logit(0.5), 0) expect_equal(logit(1), Inf) x <- NA logit(x) <- logit(0.75) expect_equal(x, 0.75) expect_equal(ilogit(logit(0.67)), 0.67) expect_equal(invlogit(logit(0.67)), 0.67) }) test_that("translations2", { x <- seq(0, 1, by = 0.25) expect_identical(logit(x), qlogis(x)) expect_identical(ilogit(logit(x)), x) expect_identical(invlogit(logit(x)), x) logit(x) <- c(0.5, 1) expect_identical(x, ilogit(c(0.5, 1))) log(x) <- c(0.5, 1) expect_identical(x, exp(c(0.5, 1))) expect_identical(pow(3, 4), 3^4) expect_equal(phi(0:2), c(0.5, 0.8413447, 0.9772499), tolerance = 0.0000001) })
read.cross.csv <- function(dir, file, na.strings=c("-","NA"), genotypes=c("A","H","B","D","C"), estimate.map=TRUE, rotate=FALSE, ...) { if(missing(file)) file <- "data.csv" if(!missing(dir) && dir != "") { file <- file.path(dir, file) } args <- list(...) if("" %in% na.strings) { na.strings <- na.strings[na.strings != ""] warning("Including \"\" in na.strings will cause problems; omitted.") } if(length(args) > 0 && "dec" %in% names(args)) { dec <- args[["dec"]] } else dec <- "." if(length(args) < 1 || !("sep" %in% names(args))) { if(length(args) < 1 || !("comment.char" %in% names(args))) data <- read.table(file, sep=",", na.strings=na.strings, colClasses="character", fill=TRUE, stringsAsFactors=TRUE, blank.lines.skip=TRUE, comment.char="", ...) else data <- read.table(file, sep=",", na.strings=na.strings, colClasses="character", fill=TRUE, stringsAsFactors=TRUE, blank.lines.skip=TRUE, ...) } else { if(length(args) < 1 || !("comment.char" %in% names(args))) data <- read.table(file, na.strings=na.strings, colClasses="character", fill=TRUE, stringsAsFactors=TRUE, blank.lines.skip=TRUE, comment.char="", ...) else data <- read.table(file, na.strings=na.strings, colClasses="character", fill=TRUE, stringsAsFactors=TRUE, blank.lines.skip=TRUE, ...) } if(rotate) data <- as.data.frame(t(data), stringsAsFactors=FALSE) empty <- grep("^\\s*$", data[2, ]) if( ! 1 %in% empty) stop("You must include at least one phenotype (e.g., an index). ", "There was this value in the first column of the second row '", data[2,1],"' where was supposed to be nothing.",sep="") if(length(empty)==ncol(data)) stop("Second row has all blank cells; you need to include chromosome IDs for the markers.") n.phe <- min((1:ncol(data))[-empty])-1 empty <- rep(FALSE, n.phe) empty[grep("^\\s*$", data[3,1:n.phe])] <- TRUE if(all(empty)) { map.included <- TRUE map <- asnumericwithdec(unlist(data[3,-(1:n.phe)]), dec=dec) if(any(is.na(map))) { temp <- unique(unlist(data[3,-(1:n.phe)])[is.na(map)]) stop("There are missing marker positions.\n", " In particular, we see these value(s): ", paste("\"",paste(temp,collapse="\",\"",sep=""),"\"",collapse=" ",sep=""), " at position(s): ", paste(which(is.na(map)),colapse=",",sep=""),sep="") } nondatrow <- 3 } else { map.included <- FALSE map <- rep(0,ncol(data)-n.phe) nondatrow <- 2 } data <- sapply(data,function(a) { a[!is.na(a) & a==""] <- NA; a }) pheno <- as.data.frame(data[-(1:nondatrow),1:n.phe,drop=FALSE], stringsAsFactors=TRUE) colnames(pheno) <- data[1,1:n.phe] mnames <- data[1,-(1:n.phe)] if(any(is.na(mnames))) stop("There are missing marker names. Check column(s) ",paste(which(is.na(mnames))+1+n.phe,collapse=","),sep="") chr <- data[2,-(1:n.phe)] if(any(is.na(chr))) stop("There are missing chromosome IDs. Check column(s) ",paste(which(is.na(chr))+1+n.phe,collapse=","),sep="") if(length(genotypes) > 0) { temp <- unique(as.character(data[-(1:nondatrow),-(1:n.phe),drop=FALSE])) temp <- temp[!is.na(temp)] wh <- !(temp %in% genotypes) if(any(wh)) { warn <- "The following unexpected genotype codes were treated as missing.\n " ge <- paste("|", paste(temp[wh],collapse="|"),"|",sep="") warn <- paste(warn,ge,"\n",sep="") warning(warn) } allgeno <- matrix(match(data[-(1:nondatrow),-(1:n.phe)],genotypes), ncol=ncol(data)-n.phe) } else allgeno <- matrix(as.numeric(data[-(1:nondatrow),-(1:n.phe)]), ncol=ncol(data)-n.phe) oldpheno <- pheno pheno <- data.frame(lapply(pheno, sw2numeric, dec=dec), stringsAsFactors=TRUE) if(all(chr %in% c(1:999,"X","x"))) { tempchr <- chr tempchr[chr=="X" | chr=="x"] <- 1000 tempchr <- as.numeric(tempchr) if(map.included) neworder <- order(tempchr, map) else neworder <- order(tempchr) } else { tempchr <- factor(chr, levels=unique(chr)) if(map.included) neworder <- order(tempchr, map) else neworder <- order(tempchr) } chr <- chr[neworder] map <- map[neworder] allgeno <- allgeno[,neworder,drop=FALSE] mnames <- mnames[neworder] if(!map.included) { map <- split(rep(0,length(chr)),chr)[unique(chr)] map <- unlist(lapply(map,function(a) seq(0,length=length(a),by=5))) names(map) <- NULL } uchr <- unique(chr) n.chr <- length(uchr) geno <- vector("list",n.chr) names(geno) <- uchr min.mar <- 1 allautogeno <- NULL for(i in 1:n.chr) { temp.map <- map[chr==uchr[i]] names(temp.map) <- mnames[chr==uchr[i]] data <- allgeno[,min.mar:(length(temp.map)+min.mar-1),drop=FALSE] min.mar <- min.mar + length(temp.map) colnames(data) <- names(temp.map) geno[[i]] <- list(data=data,map=temp.map) if(uchr[i] == "X" || uchr[i] == "x") class(geno[[i]]) <- "X" else { class(geno[[i]]) <- "A" if(is.null(allautogeno)) allautogeno <- data else allautogeno <- cbind(allautogeno,data) } } if(is.null(allautogeno)) allautogeno <- allgeno n.mar1 <- sapply(geno,function(a) ncol(a$data)) n.mar2 <- sapply(geno,function(a) length(a$map)) n.phe <- ncol(pheno) n.ind1 <- nrow(pheno) n.ind2 <- sapply(geno,function(a) nrow(a$data)) if(any(n.ind1 != n.ind2)) { cat(n.ind1,n.ind2,"\n") stop("Number of individuals in genotypes and phenotypes do not match.") } if(any(n.mar1 != n.mar2)) { cat(n.mar1,n.mar2,"\n") stop("Numbers of markers in genotypes and marker names files do not match.") } cat(" --Read the following data:\n") cat("\t",n.ind1," individuals\n") cat("\t",sum(n.mar1)," markers\n") cat("\t",n.phe," phenotypes\n") if(all(is.na(allgeno))) warning("There is no genotype data!\n") if(all(is.na(allautogeno)) || max(allautogeno,na.rm=TRUE)<=2) type <- "bc" else if(max(allautogeno,na.rm=TRUE)<=5) type <- "f2" else type <- "4way" cross <- list(geno=geno,pheno=pheno) class(cross) <- c(type,"cross") if(type=="f2") max.gen <- 5 else if(type=="bc") max.gen <- 2 else max.gen <- 14 for(i in 1:n.chr) { if(any(diff(cross$geno[[i]]$map)<0)) { o <- order(cross$geno[[i]]$map) cross$geno[[i]]$map <- cross$geno[[i]]$map[o] cross$geno[[i]]$data <- cross$geno[[i]]$data[,o,drop=FALSE] } } if(estimate.map && !map.included) estmap <- TRUE else estmap <- FALSE list(cross,estmap) }
gbm.perf <- function(object, plot.it=TRUE, oobag.curve=FALSE, overlay=TRUE, method, main="") { if(!is.logical(plot.it) || (length(plot.it)) > 1 || is.na(plot.it)) stop("plot.it must be a logical - excluding NA") performance <- gbmt_performance(object, method) if (plot.it) { plot(performance, out_of_bag_curve=oobag.curve, overlay=overlay, main=main) } as.numeric(performance) } gbmt_performance <- function(gbm_fit_obj, method) { check_if_gbm_fit(gbm_fit_obj) if (missing(method)) { method <- guess_error_method(gbm_fit_obj) message("Using ", method, " method...") } result <- switch(method, OOB=best_iter_out_of_bag(gbm_fit_obj), cv=best_iter_cv(gbm_fit_obj), test=best_iter_test(gbm_fit_obj), stop("method must be cv, test, or OOB")) attr(result, 'decoration') <- list(method=method, gbm_fit_obj=gbm_fit_obj) class(result) <- "GBMTPerformance" result } as.double.GBMTPerformance <- function(x, ...) { as.double(unclass(x)) } print.GBMTPerformance <- function(x, ...) { decoration <- attr(x, 'decoration') method_descriptor <- switch(decoration$method, cv="cross-validation", test="test-set", OOB="out-of-bag", stop("Unknown method.")) cat("The best ", method_descriptor, " iteration was ", x, ".\n", sep="") invisible(x) } plot.GBMTPerformance <- function(x, out_of_bag_curve=FALSE, overlay=TRUE, main="", ...) { decoration <- attr(x, 'decoration') perf_plot(decoration$gbm_fit_obj, x, out_of_bag_curve, overlay, decoration$method, main) } best_iter_test <- function(gbm_fit_obj) { check_if_gbm_fit(gbm_fit_obj) best_iter_test <- which.min(iteration_error(gbm_fit_obj, 'valid')) return(best_iter_test) } best_iter_cv <- function(gbm_fit_obj) { check_if_gbm_fit(gbm_fit_obj) if(!has_cross_validation(gbm_fit_obj)) { stop('In order to use method="cv" gbm must be called with cv_folds>1.') } best_iter_cv <- which.min(iteration_error(gbm_fit_obj, 'cv')) return(best_iter_cv) } best_iter_out_of_bag <- function(gbm_fit_obj) { check_if_gbm_fit(gbm_fit_obj) if(gbm_fit_obj$params$bag_fraction==1) stop("Cannot compute OOB estimate or the OOB curve when bag_fraction=1") if(all(!is.finite(gbm_fit_obj$oobag.improve))) stop("Cannot compute OOB estimate or the OOB curve. No finite OOB estimates of improvement") message("OOB generally underestimates the optimal number of iterations although predictive performance is reasonably competitive. Using cv_folds>1 when calling gbm usually results in improved predictive performance.") smoother <- generate_smoother_oobag(gbm_fit_obj) best_iter_oob <- smoother$x[which.min(-cumsum(smoother$y))] return(best_iter_oob) } generate_smoother_oobag <- function(gbm_fit_obj) { check_if_gbm_fit(gbm_fit_obj) smoother <- NULL x <- seq_len(gbm_fit_obj$params$num_trees) smoother <- loess(gbm_fit_obj$oobag.improve~x, enp.target=min(max(4,length(x)/10),50)) smoother$y <- smoother$fitted smoother$x <- x return(smoother) } guess_error_method <- function(gbm_fit_obj) { if (has_train_test_split(gbm_fit_obj)) { "test" } else if (has_cross_validation(gbm_fit_obj)) { "cv" } else { "OOB" } }
test_that("gives warning markers are not correct", { expect_warning(style_text(c( "1+1", " " ))) }) test_that("trailing spaces are stripped when checking marker and written back", { expect_equal( style_text(c( " "1+1", " )) %>% as.character(), c(" ) }) test_that("last stopping marker can be omitted", { expect_equal( style_text(c( " "1+1" )) %>% as.character(), c(" ) }) test_that("last stopping marker can be omitted", { expect_equal( style_text(c( " "call( 1)", " "call(2 +0)", " "x=2" )) %>% as.character(), c( " " ) ) }) test_that("works for one line", { expect_equal( style_text(c( "1+1", "1+1 "1+1" )) %>% as.character(), c("1 + 1", "1+1 ) }) test_that("works with other markers", { expect_equal( withr::with_options( list(styler.ignore_start = " { style_text(c( "1+1", "1+1 "1+1" )) %>% as.character() } ), c("1 + 1", "1+1 ) }) test_that("works for multiple markers inline", { withr::local_options(styler.ignore_start = " expect_equal( style_text(c( "1+1", "1+1 "1+1" )) %>% as.character(), c("1 + 1", "1+1 ) }) test_that("works for multiple markers inline on one line", { withr::local_options(styler.ignore_start = "nolint start|styler: off") expect_equal( style_text(c( "1+1", "1+1 "1+1" )) %>% as.character(), c("1 + 1", "1+1 ) }) test_that("works with other markers", { expect_warning( withr::with_options( list(styler.ignore_start = " { style_text(c( "1+1", " "1+1", "1+1", " )) %>% as.character() } ), "Invalid stylerignore sequence" ) }) test_that("Simple example works", { expect_warning(test_collection("stylerignore", "simple", transformer = style_text ), NA) }) test_that("stylerignore does not need coincidence with top-level expressions", { expect_warning(test_collection("stylerignore", "crossing", transformer = style_text ), NA) }) test_that("token adding or removing works in stylerignore", { expect_warning(test_collection("stylerignore", "adding-removing", transformer = style_text ), NA) }) test_that("no token added or removed in complex case", { expect_warning(test_collection("stylerignore", "braces", transformer = style_text ), NA) }) test_that("stylerignore sequences are respected in alignment detection", { expect_warning(test_collection("stylerignore", "alignment", transformer = style_text ), NA) })
genscorestat<-function(scores,group,correct=0){ N<-length(group); MV<-table(group) refg<-names(MV)[1] if(is.numeric(group)) refg<-as.numeric(refg) if(length(MV)!=2){ message("genscorestat works only for two groups") out<-NA }else{ abar<-mean(scores) ahat<-mean(scores^2) vv<-MV[2]*MV[1]*(ahat-abar^2)/(N-1) ee<-MV[1]*abar stat<-structure(sum(scores[group==refg]),.Names= "Gaussian") correct<-abs(correct)*sign(stat-ee) parameters<-structure(c(ee,vv),.Names=c("mean","variance")) out<-list(null.value=structure(0, .Names = "median difference"), alternative="two-sided",method="General Score Test",data.name=NULL, statistic=stat,parameters=parameters, p.value=2*pnorm(-abs(stat-ee-correct)/sqrt(vv))) class(out)<-"htest" } return(out) }
"dccm.nma" <- function(x, nmodes=NULL, ncore=NULL, progress = NULL, ...) { nma <- x if (missing(nma)) stop("dccm.nma: must supply a 'nma' object, i.e. from 'nma'") if(!"nma" %in% class(nma)) stop("dccm.nma: must supply 'nma' object, i.e. from 'nma'") ncore <- setup.ncore(ncore, bigmem = FALSE) if(ncore > 1) { mcparallel <- get("mcparallel", envir = getNamespace("parallel")) mccollect <- get("mccollect", envir = getNamespace("parallel")) } cross.inner.prod <- function(a, b) { mat <- apply(a, 1, "%*%", t(b)) return(mat) } corrmats <- function(r.inds, core.id, nma, corr.mat, freqs, progress) { for ( i in r.inds ) { mode <- matrix(nma$U[,i], ncol=3, byrow=TRUE) corr.mat <- corr.mat + (cross.inner.prod(mode, mode) / (freqs[i]**2)) if(core.id==1) { setTxtProgressBar(pb, i) } if(!is.null(progress)) { if(i %% 20 == 0) { progress$set(i) } } } return(corr.mat) } if(!is.null(nma$frequencies)) { freqs <- nma$frequencies } else { freqs <- nma$force.constants } if(is.null(nmodes)) nmodes <- length(nma$L) else { nmodes <- nmodes + nma$triv.modes if(nmodes>length(nma$L)) { warning("'nmodes' larger than the number of modes") nmodes <- length(nma$L) } } pbmax <- nmodes + nma$natoms pb <- txtProgressBar(min=(nma$triv.modes+1), max=pbmax, style=3) corr.mat <- matrix(0, nma$natoms, nma$natoms) mode.inds <- (nma$triv.modes+1):nmodes core.ids <- rep(1:ncore, length.out=length( mode.inds )) if(ncore>1) jobs <- list() for ( i in 1:ncore ) { rinds <- mode.inds[ which(core.ids==i) ] if(ncore>1) { q <- mcparallel(corrmats(rinds, i, nma, corr.mat, freqs, progress)) jobs[[i]] <- q } else corr.mat <- corrmats(rinds, i, nma, corr.mat, freqs, progress) } if(ncore>1) { res <- mccollect(jobs, wait=TRUE) for ( job in res ) { corr.mat <- corr.mat + job } } a <- vector('numeric', length=nrow(corr.mat)) k <- length(mode.inds) inds <- rep(1:nrow(corr.mat), each=3) for ( j in (nma$triv.modes+1):nmodes ) { v <- nma$U[, j] * nma$U[, j] a <- a + ( tapply( v, inds, sum) / (freqs[j]**2)) k <- k+1 setTxtProgressBar(pb, k) if(!is.null(progress)) { if(j %% 20 == 0) { progress$set(k) } } } close(pb) a <- sqrt(a) bn <- a%o%a corr.mat <- corr.mat / bn class(corr.mat) <- c("dccm", "matrix") return(corr.mat) }
stat.entropyFunction = function(bitString) { pT = sum(bitString)/length(bitString) pF = 1-pT if (pT==1 || pT==0) { e = 0 } else { e = -pT*log2(pT)-pF*log2(pF) } return(e) }
"ChickWeight" <- structure(list( weight = c(42, 51, 59, 64, 76, 93, 106, 125, 149, 171, 199, 205, 40, 49, 58, 72, 84, 103, 122, 138, 162, 187, 209, 215, 43, 39, 55, 67, 84, 99, 115, 138, 163, 187, 198, 202, 42, 49, 56, 67, 74, 87, 102, 108, 136, 154, 160, 157, 41, 42, 48, 60, 79, 106, 141, 164, 197, 199, 220, 223, 41, 49, 59, 74, 97, 124, 141, 148, 155, 160, 160, 157, 41, 49, 57, 71, 89, 112, 146, 174, 218, 250, 288, 305, 42, 50, 61, 71, 84, 93, 110, 116, 126, 134, 125, 42, 51, 59, 68, 85, 96, 90, 92, 93, 100, 100, 98, 41, 44, 52, 63, 74, 81, 89, 96, 101, 112, 120, 124, 43, 51, 63, 84, 112, 139, 168, 177, 182, 184, 181, 175, 41, 49, 56, 62, 72, 88, 119, 135, 162, 185, 195, 205, 41, 48, 53, 60, 65, 67, 71, 70, 71, 81, 91, 96, 41, 49, 62, 79, 101, 128, 164, 192, 227, 248, 259, 266, 41, 49, 56, 64, 68, 68, 67, 68, 41, 45, 49, 51, 57, 51, 54, 42, 51, 61, 72, 83, 89, 98, 103, 113, 123, 133, 142, 39, 35, 43, 48, 55, 62, 65, 71, 82, 88, 106, 120, 144, 157, 41, 47, 54, 58, 65, 73, 77, 89, 98, 107, 115, 117, 40, 50, 62, 86, 125, 163, 217, 240, 275, 307, 318, 331, 41, 55, 64, 77, 90, 95, 108, 111, 131, 148, 164, 167, 43, 52, 61, 73, 90, 103, 127, 135, 145, 163, 170, 175, 42, 52, 58, 74, 66, 68, 70, 71, 72, 72, 76, 74, 40, 49, 62, 78, 102, 124, 146, 164, 197, 231, 259, 265, 42, 48, 57, 74, 93, 114, 136, 147, 169, 205, 236, 251, 39, 46, 58, 73, 87, 100, 115, 123, 144, 163, 185, 192, 39, 46, 58, 73, 92, 114, 145, 156, 184, 207, 212, 233, 39, 48, 59, 74, 87, 106, 134, 150, 187, 230, 279, 309, 42, 48, 59, 72, 85, 98, 115, 122, 143, 151, 157, 150, 42, 53, 62, 73, 85, 102, 123, 138, 170, 204, 235, 256, 41, 49, 65, 82, 107, 129, 159, 179, 221, 263, 291, 305, 39, 50, 63, 77, 96, 111, 137, 144, 151, 146, 156, 147, 41, 49, 63, 85, 107, 134, 164, 186, 235, 294, 327, 341, 41, 53, 64, 87, 123, 158, 201, 238, 287, 332, 361, 373, 39, 48, 61, 76, 98, 116, 145, 166, 198, 227, 225, 220, 41, 48, 56, 68, 80, 83, 103, 112, 135, 157, 169, 178, 41, 49, 61, 74, 98, 109, 128, 154, 192, 232, 280, 290, 42, 50, 61, 78, 89, 109, 130, 146, 170, 214, 250, 272, 41, 55, 66, 79, 101, 120, 154, 182, 215, 262, 295, 321, 42, 51, 66, 85, 103, 124, 155, 153, 175, 184, 199, 204, 42, 49, 63, 84, 103, 126, 160, 174, 204, 234, 269, 281, 42, 55, 69, 96, 131, 157, 184, 188, 197, 198, 199, 200, 42, 51, 65, 86, 103, 118, 127, 138, 145, 146, 41, 50, 61, 78, 98, 117, 135, 141, 147, 174, 197, 196, 40, 52, 62, 82, 101, 120, 144, 156, 173, 210, 231, 238, 41, 53, 66, 79, 100, 123, 148, 157, 168, 185, 210, 205, 39, 50, 62, 80, 104, 125, 154, 170, 222, 261, 303, 322, 40, 53, 64, 85, 108, 128, 152, 166, 184, 203, 233, 237, 41, 54, 67, 84, 105, 122, 155, 175, 205, 234, 264, 264), Time = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21), Chick = ordered(c(15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48), levels=1:50, labels = c("18", "16", "15", "13", "9", "20", "10", "8", "17", "19", "4", "6", "11", "3", "1", "12", "2", "5", "14", "7", "24", "30", "22", "23", "27", "28", "26", "25", "29", "21", "33", "37", "36", "31", "39", "38", "32", "40", "34", "35", "44", "45", "43", "41", "47", "49", "46", "50", "42", "48")), Diet = factor(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), levels=1:4, labels = c("1", "2", "3", "4"))), row.names = 1:578, class = c("nfnGroupedData", "nfGroupedData", "groupedData", "data.frame"), formula = weight ~ Time | Chick, outer = ~ Diet, labels = list(x = "Time", y = "Body weight"), units = list(x = "(days)", y = "(gm)")) environment(attr(ChickWeight, "formula")) <- emptyenv() environment(attr(ChickWeight, "outer")) <- emptyenv()
NULL dcmp = function(x, lambda, nu, log = FALSE) { prep = prep.zicmp(length(x), lambda, nu) dcmp_cpp(x, prep$lambda, prep$nu, take_log = log) } rcmp = function(n, lambda, nu) { prep = prep.zicmp(n, lambda, nu) ymax = getOption("COMPoissonReg.ymax") rcmp_cpp(n, prep$lambda, prep$nu, ymax = ymax) } pcmp = function(x, lambda, nu) { prep = prep.zicmp(length(x), lambda, nu) pcmp_cpp(x, prep$lambda, prep$nu) } qcmp = function(q, lambda, nu, log.p = FALSE) { prep = prep.zicmp(length(q), lambda, nu) if (log.p) { log.q = q } else { log.q = log(q) } ymax = getOption("COMPoissonReg.ymax") qcmp_cpp(log.q, prep$lambda, prep$nu, ymax = ymax) } cmp.expected.value = function(lambda, nu) { n = max(length(lambda), length(nu)) prep = prep.zicmp(n, lambda, nu) res = numeric(n) for (i in 1:n) { res[i] = prep$lambda[i] * grad.fwd(z_hybrid, prep$lambda[i], nu = prep$nu[i], take_log = TRUE) } return(res) }
groupAndRename <- function(obj, var, before, after, addNA=FALSE) { groupAndRenameX(obj=obj, var=var, before=before, after=after, addNA=addNA) } setGeneric("groupAndRenameX", function(obj, var, before, after, addNA=FALSE) { standardGeneric("groupAndRenameX") }) setMethod(f="groupAndRenameX", signature=c("factor"), definition=function(obj, var, before, after, addNA=FALSE) { if (!all(before %in% levels(obj))) { stop("some elements of 'before' are not valid levels in the input factor!\n") } if (any(duplicated(before))) { stop("each level from the original factor must be listed only once in argument 'before'!") } ll <- levels(obj) ll[ll %in% before] <- after levels(obj) <- ll if (addNA) { obj[is.na(obj)] <- after } obj }) setMethod(f="groupAndRenameX", signature=c("data.frame"), definition=function(obj, var, before, after, addNA=FALSE) { if (length(var) != 1) { stop("length of input 'var' != 1!\n") } if (!var %in% colnames(obj)) { stop("variable specified in 'var' is not available in 'obj'!\n") } fac <- obj[[var]] if (!is.factor(obj[[var]]) ) { stop("check input, we do not have a factor here!\n") } obj[[var]] <- groupAndRename(obj[[var]], var=NULL, before=before, after=after, addNA=addNA) obj }) setMethod(f="groupAndRenameX", signature=c("sdcMicroObj"), definition=function(obj, var, before, after, addNA=FALSE) { obj <- nextSdcObj(obj) manipKey <- get.sdcMicroObj(obj, type="manipKeyVars") if (!var %in% colnames(manipKey)) { stop("variable specified in 'var' is not available in 'obj'!\n") } manipKey[[var]] <- groupAndRename(manipKey[[var]], var=var, before=before, after=after, addNA=addNA) obj <- set.sdcMicroObj(obj, type="manipKeyVars", input=list(manipKey)) obj <- calcRisks(obj) obj })
ans <- pl(amount = c(1, -1), timestamp = c(2, 3), price = c(1, 2), along.timestamp = 3:1, vprice = 1:3) expect_equal(ans[[1]]$timestamp , 1:3) expect_equal(unname(ans[[1]]$pl), c(0,1,1))
REND <- function(TPDc = NULL, TPDs = NULL){ if (is.null(TPDc) & is.null(TPDs)) { stop("At least one of 'TPDc' or 'TPDs' must be supplied") } if (!is.null(TPDc) & class(TPDc) != "TPDcomm"){ stop("The class of one object do not match the expectations, Please, specify if your object is a TPDc or a TPDs") } if (!is.null(TPDs) & class(TPDs) != "TPDsp"){ stop("The class of one object do not match the expectations, Please, specify if your object is a TPDc or a TPDs") } results <- list() Calc_FRich <- function(x) { results_FR <- numeric() if (class(x) == "TPDcomm") { TPD <- x$TPDc$TPDc names_aux <- names(x$TPDc$TPDc) cell_volume <- x$data$cell_volume } if (class(x) == "TPDsp") { TPD <- x$TPDs names_aux <- names(x$TPDs) cell_volume <- x$data$cell_volume } for (i in 1:length(TPD)) { TPD_aux <- TPD[[i]] TPD_aux[TPD_aux > 0] <- cell_volume results_FR[i] <- sum(TPD_aux) } names(results_FR) <- names_aux return(results_FR) } Calc_FEve <- function(x) { results_FE <- numeric() if (class(x) == "TPDcomm") { TPD <- x$TPDc$TPDc names_aux <- names(x$TPDc$TPDc) cell_volume <- x$data$cell_volume } if (class(x) == "TPDsp") { TPD <- x$TPDs names_aux <- names(x$TPDs) cell_volume <- x$data$cell_volume } for (i in 1:length(TPD)) { TPD_aux <- TPD[[i]][TPD[[i]] > 0] TPD_eve <- rep((1 / length(TPD_aux)), times = length(TPD_aux)) results_FE[i] <- sum(pmin(TPD_aux, TPD_eve)) } names(results_FE) <- names_aux return(results_FE) } Calc_FDiv <- function(x) { results_FD <- numeric() if (class(x) == "TPDcomm") { TPD <- x$TPDc$TPDc evaluation_grid<-x$data$evaluation_grid names_aux <- names(x$TPDc$TPDc) cell_volume <- x$data$cell_volume } if (class(x) == "TPDsp") { TPD <- x$TPDs evaluation_grid<-x$data$evaluation_grid names_aux <- names(x$TPDs) cell_volume <- x$data$cell_volume } for (i in 1:length(TPD)) { functional_volume <- evaluation_grid[TPD[[i]]>0 , , drop=F] for (j in 1:ncol(functional_volume)){ functional_volume[, j] <- (functional_volume[, j] - min(functional_volume[, j])) / (max(functional_volume[, j]) - min(functional_volume[, j])) } TPD_aux <- TPD[[i]][TPD[[i]] > 0] COG <- colMeans(functional_volume, na.rm=T) dist_COG <- function(x, COG) { result_aux<-stats::dist(rbind(x, COG)) return(result_aux) } COGDist <- apply(functional_volume, 1, dist_COG, COG) meanCOGDist <- mean(COGDist) distDeviances <- COGDist-meanCOGDist AWdistDeviances <- sum(TPD_aux * distDeviances) absdistDeviances <- abs(COGDist-meanCOGDist) AWabsdistDeviances <- sum(TPD_aux * absdistDeviances) results_FD[i] <- (AWdistDeviances + meanCOGDist) / ( AWabsdistDeviances + meanCOGDist) } names(results_FD) <- names_aux return(results_FD) } if (!is.null(TPDc)) { results$communities <- list() message("Calculating FRichness of communities") results$communities$FRichness <- Calc_FRich(TPDc) message("Calculating FEvenness of communities") results$communities$FEvenness <- Calc_FEve(TPDc) message("Calculating FDivergence of communities") results$communities$FDivergence <- Calc_FDiv(TPDc) } if (!is.null(TPDs)) { if (TPDs$data$type == "One population_One species" | TPDs$data$type == "One population_Multiple species") { results$species <- list() message("Calculating FRichness of species") results$species$FRichness <- Calc_FRich(TPDs) message("Calculating FEvenness of species") results$species$FEvenness <- Calc_FEve(TPDs) message("Calculating FDivergence of species") results$species$FDivergence <- Calc_FDiv(TPDs) } else { results$populations <- list() message("Calculating FRichness of populations") results$populations$FRichness <- Calc_FRich(TPDs) message("Calculating FEvenness of populations") results$populations$FEvenness <- Calc_FEve(TPDs) message("Calculating FDivergence of populations") results$populations$FDivergence <- Calc_FDiv(TPDs) } if (TPDs$data$method == "mean") { message("WARNING: When TPDs are calculated using the TPDsMean function, Evenness and Divergence are meaningless!!") } } return(results) }
r1sd <- function(x, na = TRUE) { return((x - mean(x, na.rm = na)) / (1 * sd(x, na.rm = na))) }
QRSimul <- function(VecX, tau, times, subj, X, y, d, kn, degree, lambda, gam){ dim = length(subj) X = matrix(X, nrow=dim) H = length(tau) px = ncol(X) n = length(unique(subj)) if(px != length(VecX)) stop("the length of VecX and the number of covariate(s) must match") XX = as.matrix(X) if(all(X[,1]==1)) VecX[1]=1 else VecX[1] = (VecX[1] - min(X[,1]))/(max(X[,1])-min(X[,1])) if(all(X[,1]==1)) X[,1]=X[,1] else X[,1] = (X[,1] - min(X[,1]))/(max(X[,1])-min(X[,1])) for(k in 2:px){ VecX[k] = (VecX[k] - min(X[,k]))/(max(X[,k])-min(X[,k])) X[,k] = (X[,k] - min(X[,k]))/(max(X[,k])-min(X[,k])) } lambda_all = lambdak_simul(times, subj, X, y, d, tau, kn, degree, lambda, gam) lambdasicr = lambda_all$lambdasicr lambdasic = lambda_all$lambdasic simul = ncqr_simul(times, subj, y, X, tau, kn, degree, lambda=lambdasicr, lambcross=lambdasic, d) W = simul$W alpha = simul$alpha hat_bt = simul$hat_bt Hpx = rep(seq(1,px), H) Xbeta = matrix(NA, dim, H*px) for(h in 1:(H*px)) { Xbeta[,h]=hat_bt[,h]*VecX[Hpx[h]] } qhat_h = matrix(NA, dim, H) for(h in 1:H){ qhat_h[,h] = rowSums(Xbeta[,((h-1)*px+1):(px*h)]) } HpxB = rep(px,H) cum_HpxB = cumsum(HpxB) cum_HpxA = c(1, c(cum_HpxB[seq(1:(H-1))]+1)) hat_bt0_ori = hat_bt[,cum_HpxA] hat_btk_ori = hat_bt[,seq(2,px)] hat_btk = matrix(NA, dim, (px-1)) hat_btk0 = matrix(0, dim, (px-1)) for(k in 1:(px-1)){ hat_btk[,k] = hat_btk_ori[,k]/(max(XX[,(k+1)])-min(XX[,(k+1)])) hat_btk0[,k] = hat_btk_ori[,k]*min(XX[,(k+1)])/(max(XX[,(k+1)])-min(XX[,(k+1)])) } hat_bt0 = hat_bt0_ori - rowSums(hat_btk0) out = list(W=W, alpha=alpha, hat_bt0=hat_bt0, hat_btk=hat_btk, qhat_h = qhat_h) return(out) }
writeEnvelope <- function(obj, centerfun = mean) { if(inherits(obj, c("SpatialPoints", "SpatialPointsDataFrame"), which = FALSE)) { SpatialPointsEnvelope(obj) } else if(inherits(obj, "list") && length(obj) > 0 && all(vapply( X = obj, FUN = inherits, FUN.VALUE = logical(1), c("SpatialPoints", "SpatialPointsDataFrame")) ) ) { ListOfSpatialPointsEnvelope(obj, centerfun = mean) } else if(inherits(obj, c("SpatialLines", "SpatialLinesDataFrame"), which = FALSE)) { SpatialLinesEnvelope(obj, centerfun = mean) } else if(inherits(obj, c("SpatialPolygons", "SpatialPolygonsDataFrame"), which = FALSE)) { SpatialPolygonsEnvelope(obj) } else { stop("obj must be an object of class SpatialPoints, SpatialPointsDataFrame, ", "SpatialLines, SpatialLinesDataFrame, SpatialPolygons, ", "or SpatialPolygonsDataFrame, or a list of objects of class ", "SpatialPoints or SpatialPointsDataFrame") } }
session <- function(url, ...) { session <- structure( list( handle = httr::handle(url), config = c(..., httr::config(autoreferer = 1L)), response = NULL, url = NULL, back = character(), forward = character(), cache = new_environment() ), class = "rvest_session" ) session_get(session, url) } is.session <- function(x) inherits(x, "rvest_session") print.rvest_session <- function(x, ...) { cat("<session> ", x$url, "\n", sep = "") cat(" Status: ", httr::status_code(x), "\n", sep = "") cat(" Type: ", httr::headers(x)$`Content-Type`, "\n", sep = "") cat(" Size: ", length(x$response$content), "\n", sep = "") invisible(x) } session_get <- function(x, url, ...) { resp <- httr::GET(url, x$config, ..., handle = x$handle) session_set_response(x, resp) } session_set_response <- function(x, response) { httr::warn_for_status(response) x$response <- response x$url <- response$url x$cache <- new_environment() x } session_jump_to <- function(x, url, ...) { check_session(x) url <- xml2::url_absolute(url, x$url) last_url <- x$url x <- session_get(x, url, ...) x$back <- c(last_url, x$back) x$forward <- character() x } session_follow_link <- function(x, i, css, xpath, ...) { check_session(x) url <- find_href(x, i = i, css = css, xpath = xpath) inform(paste0("Navigating to ", url)) session_jump_to(x, url, ...) } find_href <- function(x, i, css, xpath) { if (sum(!missing(i), !missing(css), !missing(xpath)) != 1) { abort("Must supply exactly one of `i`, `css`, or `xpath`") } if (!missing(i)) { stopifnot(length(i) == 1) a <- html_elements(x, "a") if (is.numeric(i)) { out <- a[[i]] } else if (is.character(i)) { text <- html_text(a) match <- grepl(i, text, fixed = TRUE) if (!any(match)) { stop("No links have text '", i, "'", call. = FALSE) } out <- a[[which(match)[[1]]]] } else { abort("`i` must a string or integer") } } else { a <- html_elements(x, css = css, xpath = xpath) if (length(a) == 0) { abort("No links matched `css`/`xpath`") } out <- a[[1]] } html_attr(out, "href") } session_back <- function(x) { check_session(x) if (length(x$back) == 0) { abort("Can't go back any further") } url <- x$back[[1]] x$back <- x$back[-1] old_url <- x$url x <- session_get(x, url) x$forward <- c(old_url, x$forward) x } session_forward <- function(x) { check_session(x) if (length(x$forward) == 0) { abort("Can't go forward any further") } url <- x$forward[[1]] old_url <- x$url x <- session_get(x, url) x$forward <- x$forward[-1] x$back <- c(old_url, x$back) x } session_history <- function(x) { check_session(x) urls <- c(rev(x$back), x$url, x$forward) prefix <- rep(c(" ", "- ", " "), c(length(x$back), 1, length(x$forward))) cat_line(prefix, urls) } session_submit <- function(x, form, submit = NULL, ...) { check_session(x) check_form(form) subm <- submission_build(form, submit) resp <- submission_submit(subm, x$config, ..., handle = x$handle) session_set_response(x, resp) } read_html.rvest_session <- function(x, ...) { if (!is_html(x$response)) { abort("Page doesn't appear to be html.") } env_cache(x$cache, "html", read_html(x$response, ..., base_url = x$url)) } is_html <- function(x) { type <- httr::headers(x)$`Content-Type` if (is.null(type)) return(FALSE) parsed <- httr::parse_media(type) parsed$complete %in% c("text/html", "application/xhtml+xml") } html_form.rvest_session <- function(x, base_url = NULL) { html_form(read_html(x), base_url = base_url) } html_table.rvest_session <- function(x, header = NA, trim = TRUE, fill = deprecated(), dec = ".", na.strings = "NA", convert = TRUE) { html_table( read_html(x), header = header, trim = trim, fill = fill, dec = dec, na.strings = na.strings, convert = convert ) } html_element.rvest_session <- function(x, css, xpath) { html_element(read_html(x), css, xpath) } html_elements.rvest_session <- function(x, css, xpath) { html_elements(read_html(x), css, xpath) } status_code.rvest_session <- function(x) { status_code(x$response) } headers.rvest_session <- function(x) { headers(x$response) } cookies.rvest_session <- function(x) { cookies(x$response) } check_form <- function(x) { if (!inherits(x, "rvest_form")) { abort("`form` must be a single form produced by html_form()") } } check_session <- function(x) { if (!inherits(x, "rvest_session")) { abort("`x` must be produced by session()") } }
context('Test the creation of custom indicators') data(forestgap) data(serengeti) datasets <- list(forestgap[3:4], forestgap[1:2]) test_methods <- function(teststring, datalength, obj) { ok_print <- any(grepl(teststring, capture.output(print(obj)))) expect_true(ok_print) ok_summary <- any(grepl(teststring, capture.output(summary(obj)))) expect_true(ok_summary) ok_as_df <- nrow(as.data.frame(obj)) == datalength expect_true(ok_as_df) return(TRUE) } test_that('Custom indicators work', { skip_on_cran() for (dataset in datasets) { testindic <- function(mat) { mat[1, 1] } indicator_mp <- create_indicator(testindic) a <- indicator_mp(dataset) test_methods("Spatial Early-Warning:", length(dataset), a) test_methods("Spatial Early-Warning:", 1, a[[1]]) if ( length(dataset) > 1 ) { suppressWarnings( plot(a) ) } indictest(a[[1]], nulln = 9) b <- indictest(a, nulln = 9) test_methods("Spatial Early-Warning:", length(dataset), b) test_methods("Spatial Early-Warning:", 1, b[[1]]) if (length(dataset) > 1) { suppressWarnings( plot(b) ) } } indicator_mp <- create_indicator(testindic) a <- indicator_mp(dataset) expect_true({ all.equal(a, compute_indicator(dataset, fun = testindic)) }) }) test_that('Custom indicators handles anonymous functions correctly', { expect_warning( anon_fun_indic <- create_indicator(function(mat) mean(mat)) ) expect_true({ anon_fun_indic <- create_indicator(function(mat) mean(mat), taskname = "TestTask") TRUE }) })
testthat::context("H2O AUTOML TEST") test_that("Fire up H2O", { testthat::skip_on_cran() h2o.init( nthreads = -1, ip = 'localhost', port = 54321 ) model_spec <<- automl_reg(mode = 'regression') %>% set_engine( engine = 'h2o', max_runtime_secs = 5, max_runtime_secs_per_model = 4, nfolds = 5, max_models = 3, exclude_algos = c("DeepLearning"), seed = 786 ) }) test_that("automl_reg: Parsnip Test", { testthat::skip_on_cran() model_fit <<- model_spec %>% fit(value ~ ., data = training(m750_splits)) predictions_tbl <- model_fit %>% modeltime_calibrate(testing(m750_splits)) %>% modeltime_forecast(new_data = testing(m750_splits)) testthat::expect_s3_class(model_fit$fit, "automl_fit_impl") testthat::expect_s3_class(model_fit$fit$data, "tbl_df") testthat::expect_equal(names(model_fit$fit$data)[1], "date") testthat::expect_equal(model_fit$preproc$y_var, "value") testthat::expect_identical(nrow(testing(m750_splits)), nrow(predictions_tbl)) testthat::expect_identical(testing(m750_splits)$date, predictions_tbl$.index) resid <- testing(m750_splits)$value - predictions_tbl$.value testthat::expect_lte(max(abs(resid)), 5000) testthat::expect_lte(mean(abs(resid)), 2500) }) test_that("automl_reg: Workflow Test", { testthat::skip_on_cran() recipe_spec <- recipe(value ~ date, data = training(m750_splits)) %>% step_log(value, skip = FALSE) %>% step_date(date, features = "month") %>% step_mutate(date_num = as.numeric(date)) wflw <- workflow() %>% add_recipe(recipe_spec) %>% add_model(model_spec) wflw_fit <<- wflw %>% fit(training(m750_splits)) predictions_tbl <- wflw_fit %>% modeltime_calibrate(testing(m750_splits)) %>% modeltime_forecast( new_data = testing(m750_splits), actual_data = training(m750_splits) ) %>% mutate_at(vars(.value), exp) testthat::expect_s3_class(wflw_fit$fit$fit$fit, "automl_fit_impl") testthat::expect_s3_class(wflw_fit$fit$fit$fit$data, "tbl_df") testthat::expect_equal(names(wflw_fit$fit$fit$fit$data)[1], "date") mld <- wflw_fit %>% workflows::pull_workflow_mold() testthat::expect_equal(names(mld$outcomes), "value") full_data <- bind_rows(training(m750_splits), testing(m750_splits)) testthat::expect_identical(nrow(full_data), nrow(predictions_tbl)) testthat::expect_identical(full_data$date, predictions_tbl$.index) predictions_tbl <- predictions_tbl %>% filter(.key == "prediction") resid <- testing(m750_splits)$value - predictions_tbl$.value testthat::expect_lte(max(abs(resid)), 10000) testthat::expect_lte(mean(abs(resid)), 5000) }) test_that("automl_leaderboard() works.", { testthat::skip_on_cran() expect_s3_class(automl_leaderboard(model_fit), "tbl_df") expect_s3_class(automl_leaderboard(wflw_fit), "tbl_df") expect_error( automl_leaderboard(workflow()) ) expect_error( workflow() %>% add_model(automl_reg() %>% set_engine("h2o")) %>% automl_leaderboard() ) expect_error( automl_leaderboard(automl_reg()) ) expect_error( automl_leaderboard("a") ) }) test_that("automl_update_model() works.", { testthat::skip_on_cran() model_ids <- automl_leaderboard(model_fit) %>% pull(model_id) model_id_1 <- model_ids[1] model_id_2 <- model_ids[2] model_fit_swapped <- automl_update_model(model_fit, model_id_2) model_2 <- h2o.getModel(model_id_2) expect_equal(model_fit_swapped$fit$models$model_1, model_2) expect_equal( model_fit_swapped$fit$desc, stringr::str_glue('H2O AutoML - {stringr::str_to_title(model_2@algorithm)}') ) model_ids <- automl_leaderboard(wflw_fit) %>% pull(model_id) model_id_1 <- model_ids[1] model_id_2 <- model_ids[2] model_fit_swapped <- automl_update_model(wflw_fit, model_id_2) model_2 <- h2o.getModel(model_id_2) expect_equal(model_fit_swapped$fit$fit$fit$models$model_1, model_2) expect_equal( model_fit_swapped$fit$fit$fit$desc, stringr::str_glue('H2O AutoML - {stringr::str_to_title(model_2@algorithm)}') ) expect_error( automl_update_model() ) expect_error( automl_update_model("a") ) expect_error( automl_update_model(wflw_fit, "A") ) }) testthat::test_that("Shutdown H2O", { testthat::skip_on_cran() h2o.shutdown(prompt = FALSE) })
library(pcalg) amat1 <- t(cbind(c(0,1,0,1,0),c(0,0,1,0,1),c(0,0,0,1,1),c(0,0,0,0,1),c(0,0,0,0,0))) amat2 <- t(cbind(c(0,1,0,1,1),c(0,0,0,0,1),c(0,0,0,0,1),c(0,0,0,0,1),c(0,0,0,0,0))) g1 <- as(amat1,"graphNEL") g2 <- as(amat2,"graphNEL") res <- compareGraphs(g1,g2) if ((round(res["tpr"],5)!=0.83333) | (round(res["fpr"],5)!=0.5) | (round(res["tdr"],5)!=0.71429)) { stop("Test of compareGraphs: Theoretical values not matched!") }
skip_tests_for_cran <- TRUE skip_maxnet <- FALSE skip_maxent.jar <- TRUE skip_bioclim <- TRUE skip_simDiff <- TRUE library(dplyr) options(warn=-1) set.seed(48) occs <- read.csv(file.path(system.file(package="dismo"), "/ex/bradypus.csv"))[,2:3] envs.orig <- raster::stack(list.files(path=paste(system.file(package='dismo'), '/ex', sep=''), pattern='grd', full.names=TRUE)) occs.z <- cbind(occs, raster::extract(envs.orig, occs)) occs.z$biome <- factor(occs.z$biome) bg <- as.data.frame(dismo::randomPoints(envs.orig, 1000)) names(bg) <- names(occs) bg.z <- cbind(bg, raster::extract(envs.orig, bg)) bg.z$biome <- factor(bg.z$biome) algorithms <- c("maxnet", "maxent.jar", "bioclim") no.iter <- 5 for(alg in algorithms) { if(alg == "maxnet" & skip_maxnet == TRUE) next if(alg == "maxent.jar" & skip_maxent.jar == TRUE) next if(alg == "bioclim" & skip_bioclim == TRUE) next if(alg == "bioclim") { envs <- envs.orig[[-9]] cats1 <- NULL occs.z$biome <- NULL bg.z$biome <- NULL extrap <- FALSE }else{ envs <- envs.orig cats1 <- "biome" extrap <- TRUE } if(alg == "bioclim") tune.args <- list(tails = c("low", "high", "both")) if(alg %in% c("maxnet", "maxent.jar")) tune.args <- list(fc = c("L"), rm = 2:3) mset <- lapply(tune.args, function(x) x[1]) context(paste("Testing ENMevaluate for", alg, "with block partitions...")) e <- ENMevaluate(occs, envs, bg, tune.args = tune.args, partitions = "block", algorithm = alg, categoricals = cats1, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "block", tune.args, 4, 4) context(paste("Testing evalplot.stats for", alg, "with block partitions...")) test_evalplot.stats(e) grps <- get.block(occs, bg) context(paste("Testing evalplot.envSim.hist for", alg, "with block partitions...")) test_evalplot.envSim.hist(e, occs.z, bg.z, grps$occs.grp, grps$bg.grp) context(paste("Testing evalplot.envSim.map for", alg, "with block partitions...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, grps$occs.grp, grps$bg.grp, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with block partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "block", mset, 4, 4) context(paste("Testing evalplot.nulls for", alg, "with block partitions...")) test_evalplot.nulls(ns) if(skip_tests_for_cran == FALSE) { context(paste("Testing ENMevaluate for", alg, "with checkerboard1 partitions...")) e <- ENMevaluate(occs, envs, bg, tune.args = tune.args, partitions = "checkerboard1", algorithm = alg, categoricals = cats1, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "checkerboard1", tune.args, 2, 2) context(paste("Testing evalplot.stats for", alg, "with checkerboard1 partitions...")) test_evalplot.stats(e) grps <- get.checkerboard1(occs, envs, bg, aggregation.factor = 2) context(paste("Testing evalplot.envSim.hist for", alg, "with checkerboard1 partitions...")) test_evalplot.envSim.hist(e, occs.z, bg.z, grps$occs.grp, grps$bg.grp) context(paste("Testing evalplot.envSim.map for", alg, "with checkerboard1 partitions...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, grps$occs.grp, grps$bg.grp, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with checkerboard1 partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "checkerboard1", mset, 2, 2) context(paste("Testing ENMnulls plotting function for", alg, "with checkerboard1 partitions...")) test_evalplot.nulls(ns) } if(skip_tests_for_cran == FALSE) { context(paste("Testing ENMevaluate for", alg, "with checkerboard2 partitions...")) e <- ENMevaluate(occs, envs, bg, tune.args = tune.args, partitions = "checkerboard2", algorithm = alg, categoricals = cats1, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "checkerboard2", tune.args, 4, 4) context(paste("Testing evalplot.stats for", alg, "with checkerboard2 partitions...")) test_evalplot.stats(e) grps <- get.checkerboard2(occs, envs, bg, aggregation.factor = 2) context(paste("Testing evalplot.envSim.hist for", alg, "with checkerboard2 partitions...")) test_evalplot.envSim.hist(e, occs.z, bg.z, grps$occs.grp, grps$bg.grp) context(paste("Testing evalplot.envSim.map for", alg, "with checkerboard2 partitions...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, grps$occs.grp, grps$bg.grp, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with checkerboard2 partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "checkerboard2", mset, 4, 4) context(paste("Testing ENMnulls plotting function for", alg, "with checkerboard2 partitions...")) test_evalplot.nulls(ns) } if(skip_tests_for_cran == FALSE) { context(paste("Testing ENMevaluate for", alg, "with random 5-fold partitions...")) e <- ENMevaluate(occs, envs, bg, tune.args = tune.args, partitions = "randomkfold", algorithm = alg, categoricals = cats1, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "randomkfold", tune.args, 5, 1) context(paste("Testing evalplot.stats for", alg, "with random 5-fold partitions...")) test_evalplot.stats(e) grps <- get.randomkfold(occs, bg, kfolds = 5) context(paste("Testing evalplot.envSim.hist for", alg, "with random 5-fold partitions...")) test_evalplot.envSim.hist(e, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0) context(paste("Testing evalplot.envSim.map for", alg, "with random 5-fold partitions...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with random 5-fold partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "randomkfold", mset, 5, 1) context(paste("Testing ENMnulls plotting function for", alg, "with random 5-fold partitions...")) test_evalplot.nulls(ns) } if(skip_tests_for_cran == FALSE) { context(paste("Testing ENMevaluate for", alg, "with jackknife partitions...")) e <- ENMevaluate(occs[1:10,], envs, bg, tune.args = tune.args, partitions = "jackknife", algorithm = alg, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "jackknife", tune.args, nrow(e@occs), 1) context(paste("Testing evalplot.stats for", alg, "with testing partition...")) test_evalplot.stats(e) context(paste("Testing ENMnulls for", alg, "with jackknife partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "jackknife", mset, nrow(e@occs), 1) context(paste("Testing ENMnulls plotting function for", alg, "with jackknife partitions...")) test_evalplot.nulls(ns) } context(paste("Testing ENMevaluate for", alg, "with testing partition...")) e <- ENMevaluate(occs[1:100,], envs, bg, tune.args = tune.args, partitions = "testing", algorithm = alg, categoricals = cats1, occs.testing = occs[101:nrow(occs),], overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "testing", tune.args, 1, 1) context(paste("Testing evalplot.stats for", alg, "with testing partition...")) test_evalplot.stats(e) grps <- list(occs.grp = rep(0, nrow(occs)), bg.grp = rep(0, nrow(bg))) context(paste("Testing evalplot.envSim.hist for", alg, "with testing partition...")) test_evalplot.envSim.hist(e, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0, occs.testing.z = e@occs.testing) context(paste("Testing evalplot.envSim.map for", alg, "with testing partition...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0, occs.testing.z = e@occs.testing, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with testing partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "testing", mset, 1, 1) context(paste("Testing ENMnulls plotting function for", alg, "with testing partition...")) test_evalplot.nulls(ns) if(skip_tests_for_cran == FALSE) { context(paste("Testing ENMevaluate for", alg, "with no partitions...")) e <- ENMevaluate(occs, envs, bg, tune.args = tune.args, partitions = "none", algorithm = alg, categoricals = cats1, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "none", tune.args, 1, 1) context(paste("Testing ENMnulls for", alg, "with no partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "none", mset, 1, 1) context(paste("Testing ENMnulls plotting function for", alg, "with no partitions...")) test_evalplot.nulls(ns) } context(paste("Testing ENMevaluate for", alg, "with user partitions...")) user.grp <- list(occs.grp = round(runif(nrow(occs), 1, 4)), bg.grp = round(runif(nrow(bg), 1, 4))) e <- ENMevaluate(occs, envs, bg, tune.args = tune.args, partitions = "user", algorithm = alg, categoricals = cats1, user.grp = user.grp, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "user", tune.args, 4, 4) context(paste("Testing evalplot.stats for", alg, "with user partitions...")) test_evalplot.stats(e) context(paste("Testing evalplot.envSim.hist for", alg, "with user partitions...")) test_evalplot.envSim.hist(e, occs.z, bg.z, user.grp$occs.grp, user.grp$bg.grp) context(paste("Testing evalplot.envSim.map for", alg, "with user partitions...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, user.grp$occs.grp, user.grp$bg.grp, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with user partitions...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, user.eval.type = "kspatial", quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "user", mset, 4, 4) context(paste("Testing ENMnulls plotting function for", alg, "with user partitions...")) test_evalplot.nulls(ns) if(skip_tests_for_cran == FALSE) { context(paste("Testing ENMevaluate for", alg, "with random 5-fold partitions and no raster environmental variables...")) e <- ENMevaluate(occs.z, bg = bg.z, tune.args = tune.args, partitions = "randomkfold", algorithm = alg, categoricals = cats1, quiet = TRUE) test_ENMevaluation(e, alg, "randomkfold", tune.args, 5, 1, type = "swd") context(paste("Testing evalplot.stats for", alg, "with random 5-fold partitions and no raster environmental variables...")) test_evalplot.stats(e) grps <- get.randomkfold(occs, bg, kfolds = 5) context(paste("Testing evalplot.envSim.hist for", alg, "with random 5-fold partitions and no raster environmental variables...")) test_evalplot.envSim.hist(e, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0, categoricals = cats1) context(paste("Testing evalplot.envSim.map for", alg, "with random 5-fold partitions and no raster environmental variables...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with random 5-fold partitions and no raster environmental variables...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "randomkfold", mset, 5, 1) context(paste("Testing ENMnulls plotting function for", alg, "with random 5-fold partitions and no raster environmental variables...")) test_evalplot.nulls(ns) } if(skip_tests_for_cran == FALSE) { context(paste("Testing ENMevaluate for", alg, "with random 5-fold partitions and no input background data...")) e <- ENMevaluate(occs, envs, tune.args = tune.args, partitions = "randomkfold", algorithm = alg, n.bg = 1000, categoricals = cats1, overlap = TRUE, quiet = TRUE) test_ENMevaluation(e, alg, "randomkfold", tune.args, 5, 1) context(paste("Testing evalplot.stats for", alg, "with random 5-fold partitions and no input background data...")) test_evalplot.stats(e) grps <- get.randomkfold(occs, bg, kfolds = 5) context(paste("Testing evalplot.envSim.hist for", alg, "with random 5-fold partitions and no input background data...")) test_evalplot.envSim.hist(e, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0) context(paste("Testing evalplot.envSim.map for", alg, "with random 5-fold partitions and no input background data...")) test_evalplot.envSim.map(e, envs, occs.z, bg.z, grps$occs.grp, grps$bg.grp, bg.sel = 0, skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with random 5-fold partitions and no input background data...")) ns <- ENMnulls(e, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e, ns, no.iter, alg, "randomkfold", mset, 5, 1) context(paste("Testing ENMnulls plotting function for", alg, "with random 5-fold partitions and no input background data...")) test_evalplot.nulls(ns) } if(skip_tests_for_cran == FALSE | alg != "bioclim") { envs.2cat <- raster::addLayer(envs, envs$biome * round(runif(raster::ncell(envs), min = 0, max = 5))) occs.z.2cat <- cbind(occs, raster::extract(envs.2cat, occs)) occs.z.2cat$biome.1 <- factor(occs.z.2cat$biome.1) occs.z.2cat$biome.2 <- factor(occs.z.2cat$biome.2) bg.z.2cat <- cbind(bg, raster::extract(envs.2cat, bg)) bg.z.2cat$biome.1 <- factor(bg.z.2cat$biome.1) bg.z.2cat$biome.2 <- factor(bg.z.2cat$biome.2) context(paste("Testing ENMevaluate for", alg, "with random 5-fold partitions and two categorical variables...")) e.2cat <- ENMevaluate(occs, envs.2cat, bg, tune.args = tune.args, partitions = "randomkfold", algorithm = alg, n.bg = 1000, categoricals = c("biome.1", "biome.2"), overlap = TRUE, quiet = TRUE) test_ENMevaluation(e.2cat, alg, "randomkfold", tune.args, 5, 1) context(paste("Testing ENMevaluate for", alg, "with random 5-fold partitions and two categorical variables and no env data...")) e.2cat.z <- ENMevaluate(occs.z.2cat, bg = bg.z.2cat, tune.args = tune.args, partitions = "randomkfold", algorithm = alg, n.bg = 1000, categoricals = c("biome.1", "biome.2"), overlap = TRUE, quiet = TRUE) test_ENMevaluation(e.2cat.z, alg, "randomkfold", tune.args, 5, 1, type = "swd") context(paste("Testing evalplot.stats for", alg, "with random 5-fold partitions and two categorical variables...")) test_evalplot.stats(e.2cat) grps <- get.randomkfold(occs, bg, kfolds = 5) context(paste("Testing evalplot.envSim.hist for", alg, "with random 5-fold partitions and two categorical variables...")) test_evalplot.envSim.hist(e.2cat, occs.z.2cat, bg.z.2cat, grps$occs.grp, grps$bg.grp, bg.sel = 0, categoricals = c("biome.1", "biome.2")) context(paste("Testing evalplot.envSim.map for", alg, "with random 5-fold partitions and two categorical variables...")) test_evalplot.envSim.map(e.2cat, envs.2cat, occs.z.2cat, bg.z.2cat, grps$occs.grp, grps$bg.grp, bg.sel = 0, categoricals = c("biome.1", "biome.2"), skip_simDiff = skip_simDiff) context(paste("Testing ENMnulls for", alg, "with random 5-fold partitions and two categorical variables...")) ns <- ENMnulls(e.2cat, mod.settings = mset, no.iter = no.iter, quiet = TRUE) test_ENMnulls(e.2cat, ns, no.iter, alg, "randomkfold", mset, 5, 1) context(paste("Testing ENMnulls plotting function for", alg, "with random 5-fold partitions and two categorical variables...")) test_evalplot.nulls(ns) } context(paste("Testing clamping function for", alg, "with...")) test_clamp(e, envs, occs.z, bg.z, categoricals = cats1, canExtrapolate = extrap) context(paste("Testing clamping function for", alg, "with two categorical variables...")) if(skip_tests_for_cran == FALSE | alg != "bioclim") test_clamp(e.2cat, envs.2cat, occs.z.2cat, bg.z.2cat, categoricals = c("biome.1", "biome.2")) }
MCpriorIntFun <- function(Nsim=200, prior, Hpar, dimData, FUN=function(par,...){as.vector(par)}, store=TRUE, show.progress = floor(seq(1, Nsim, length.out = 20 ) ), Nsim.min=Nsim, precision = 0, ...) { start.time=proc.time() not.finite=0 param = prior(type = "r", n=1, Hpar=Hpar, dimData=dimData) temp.res=FUN(param,...) dim.res=dim(temp.res) if(is.null(dim.res) || (sum(dim.res!=1) ==1) ) { emp.mean=rep(0,length(temp.res)) } else { store=FALSE emp.mean=array(0,dim=dim.res) } emp.variance= emp.mean emp.variance.unNorm=emp.variance if(store) { stored.vals=matrix(0,nrow=Nsim,ncol=length(emp.mean)) } nsim=1 while((nsim<=Nsim) && ( (nsim<=Nsim.min) || (max( sqrt(emp.variance/(nsim-1)) / abs(emp.mean) ) > precision) ) ) { if(any(nsim==show.progress)) { cat(paste((nsim-1), "iterations done", "\n", sep = " " )) } flag=TRUE count = 0 while(flag & (count<=50)) { param = prior(type = "r", n=1, Hpar=Hpar, dimData=dimData) temp.res=FUN(param,...) flag = (any(sapply(as.vector(temp.res), function(x){ ! is.finite(x) } ) ) ) if(flag) { not.finite = not.finite+1 } count = count+1 } if(flag) stop("more than 50 non finite values produced in a row") cur.res=temp.res new.emp.mean=emp.mean+1/nsim*(cur.res-emp.mean) emp.variance.unNorm=emp.variance.unNorm + (cur.res-new.emp.mean)* (cur.res- emp.mean) emp.variance = emp.variance.unNorm/(nsim-1) emp.mean = new.emp.mean if(store) { stored.vals[nsim,]= as.vector(cur.res) } nsim=nsim+1 } end.time = proc.time() elapsed=end.time-start.time print(elapsed) if(store) { returned.vals=stored.vals[1:(nsim-1),] } else { returned.vals=0 } return(list( stored.vals= returned.vals, elapsed=elapsed, nsim = nsim-1, emp.mean=emp.mean, emp.stdev=sqrt(emp.variance), est.error=sqrt(emp.variance/(nsim-1)), not.finite = not.finite)) }
NULL "ghp100k"
kkmeans <- function(K, parameters) { state <- list() state$time <- system.time({ H <- eigen(K, symmetric = TRUE)$vectors[, 1:parameters$cluster_count] objective <- sum(diag(t(H) %*% K %*% H)) - sum(diag(K)) H_normalized <- H/matrix(sqrt(rowSums(H^2, 2)), nrow(H), parameters$cluster_count, byrow = FALSE) H_normalized[sqrt(rowSums(H^2, 2)) == 0, ] <- 0 set.seed(NULL) state$clustering <- stats::kmeans(H_normalized, centers = parameters$cluster_count, iter.max = 1000, nstart = 10)$cluster state$objective <- objective state$parameters <- parameters }) state }
translogEla <- function( xNames, data, coef, coefCov = NULL, dataLogged = FALSE ) { checkNames( c( xNames ), names( data ) ) nExog <- length( xNames ) nCoef <- 1 + nExog + nExog * ( nExog + 1 ) / 2 if( nCoef > length( coef ) ) { stop( "a translog function with ", nExog, " exogenous variables", " must have at least ", nCoef, " coefficients" ) } if( dataLogged ) { logData <- data } else { logData <- logDataSet( data = data, varNames = xNames ) } result <- quadFuncDeriv( xNames = xNames, data = logData, coef = coef, coefCov = coefCov ) return( result ) }
.init_base_test_templ <- function() { templ_dir <- file.path(get_templ_dir(), "BaseTestProjectTemplate") if (dir.exists(templ_dir)) { return(templ_dir) } unzip(file.path("data", "BaseTestProjectTemplate.zip"), exdir = get_templ_dir()) build_prj <- RSuite::prj_start("BaseTestProjectBuild", skip_rc = TRUE, path = templ_dir, tmpl = templ_dir) params <- build_prj$load_params() on.exit({ unlink(params$prj_path, recursive = TRUE, force = TRUE) }, add = TRUE) dst_rmgr <- RSuite::repo_mng_start("Dir", path = normalizePath(file.path(templ_dir, "project", "repository")), rver = params$r_ver, types = params$bin_pkgs_type) RSuite::repo_upload_ext_packages(dst_rmgr, pkgs = c("logging"), prj = build_prj, pkg_type = params$bin_pkgs_type) RSuite::repo_mng_stop(dst_rmgr) return(templ_dir) } .templ_env <- new.env() assign("templates", list(), envir = .templ_env) register_project_templ <- function(templ_name, init_f) { eval_managed(sprintf("Registering project template %s", templ_name), { .init_base_test_templ() base_dir <- get_templ_dir() templ_dir <- file.path(base_dir, templ_name) if (!dir.exists(templ_dir)) { RSuite::tmpl_start(templ_name, path = base_dir, add_pkg = FALSE, base_tmpl = file.path(base_dir, "BaseTestProjectTemplate")) prj <- RSuite::prj_start(paste0(templ_name, "_Build"), skip_rc = T, path = base_dir, tmpl = templ_dir) on.exit({ unlink(prj$path, recursive = TRUE, force = TRUE) }, add = TRUE) init_f(prj) unlink(file.path(templ_dir, "project", "repository"), recursive = TRUE, force = TRUE) file.rename(file.path(prj$path, "repository"), file.path(templ_dir, "project", "repository")) } templs <- get("templates", envir = .templ_env) templs[[templ_name]] <- templ_dir assign("templates", templs, envir = .templ_env) }) } get_project_templ <- function(templ_name) { templs <- get("templates", envir = .templ_env) if (!(templ_name %in% names(templs))) { stop(sprintf("Requested non registered project template: %s", templ_name)) } return(templs[[templ_name]]) } init_test_project <- function(repo_adapters = c("Dir"), name = "TestProject", tmpl = NULL, skip_rc = T) { if (is.null(tmpl)) { tmpl <- .init_base_test_templ() } RSuite::prj_load() prj <- RSuite::prj_start(name, skip_rc = skip_rc, path = get_wspace_dir(), tmpl = tmpl) RSuite::prj_config_set_repo_adapters(repos = repo_adapters, prj = prj) unlink(file.path(prj$path, "deployment", "libs", "logging"), recursive = T, force = T) params_path <- file.path(prj$path, "PARAMETERS") params_df <- data.frame(read.dcf(file = params_path)) params_df$SnapshotDate <- NULL write.dcf(params_df, file = params_path) on_test_exit(function() { unlink(prj$path, recursive = T, force = T) }) return(prj) } remove_package_from_lrepo <- function(pkg_file, prj, type = .Platform$pkgType) { loc_repo <- .get_local_repo_path(prj, type) unlink(file.path(loc_repo, pkg_file), force = T, recursive = T) RSuite:::rsuite_write_PACKAGES(loc_repo, type = type) } create_test_package <- function(name, prj, ver = "1.0", deps = "", imps = "", sysreqs = "", tmpl = "builtin", skip_rc = T) { RSuite::prj_start_package(name, prj = prj, skip_rc = skip_rc, tmpl = tmpl) pkg_path <- file.path(prj$path, "packages", name) pkg_desc_fname <- file.path(pkg_path, "DESCRIPTION") if (file.exists(pkg_desc_fname)) { pkg_desc <- data.frame(read.dcf(file = pkg_desc_fname)) pkg_desc$Version <- ver deps <- trimws(deps) if (sum(nchar(deps))) { pkg_desc$Depends <- paste(deps, collapse = ", ") } imps <- trimws(imps) if (sum(nchar(imps))) { pkg_desc$Imports <- paste(imps, collapse = ", ") } sysreqs <- trimws(sysreqs) if (sum(nchar(sysreqs))) { pkg_desc$SystemRequirements <- sysreqs } write.dcf(pkg_desc, file = pkg_desc_fname) } invisible(pkg_path) } set_test_package_ns_imports <- function(name, prj, imps) { imp_path <- file.path(prj$path, "packages", name, "R", "packages_import.R") writeLines(c(sprintf(" } create_test_master_script <- function(code, prj) { fn <- tempfile(pattern = "test_", fileext = ".R", tmpdir = file.path(prj$path, "R")) f <- file(fn, "w") writeLines(code, con = f) close(f) invisible(fn) } create_package_deploy_to_lrepo <- function(name, prj, ver = "1.0", type = .Platform$pkgType, deps = "", sysreqs = "", imps = "logging") { pkg_path <- create_test_package(name, prj, ver, deps = deps, imps = imps, sysreqs = sysreqs) set_test_package_ns_imports(name, prj, unlist(strsplit(imps, ","))) params <- prj$load_params() on.exit({ unlink(pkg_path, recursive = T, force = T) unlink(file.path(params$lib_path, "*"), recursive = T, force = T) }, add = T) loc_repo <- .get_local_repo_path(prj, type) prj_install_deps(prj, clean = T) prj_build(prj, type = type) int_path <- RSuite:::rsuite_contrib_url(repos = params$get_intern_repo_path(), type = type) avails <- data.frame(available.packages(sprintf("file:///%s", int_path), type = type), stringsAsFactors = F) pkg_file <- avails[avails$Package == name, "File"] file.copy(from = file.path(int_path, pkg_file), to = loc_repo) RSuite:::rsuite_write_PACKAGES(loc_repo, type = type) } remove_test_packages <- function(prj) { unlink(file.path(prj$path, "packages", "*"), recursive = T, force = T) } set_test_package_deps <- function(name, prj, deps = NULL, sugs = NULL) { params <- prj$load_params() pkg_desc_fname <- file.path(params$pkgs_path, name, "DESCRIPTION") if (file.exists(pkg_desc_fname)) { pkg_desc <- data.frame(read.dcf(file = pkg_desc_fname)) if (!is.null(deps)) { pkg_desc$Depends <- paste(deps, collapse = ", ") } if (!is.null(sugs)) { pkg_desc$Suggests <- paste(sugs, collapse = ", ") } } write.dcf(pkg_desc, file = pkg_desc_fname) } .get_local_repo_path <- function(prj, type) { path <- RSuite:::rsuite_contrib_url(repos = file.path(prj$path, "repository"), type = type) stopifnot(dir.exists(path)) path <- normalizePath(path) return(path) } expect_that_packages_installed <- function(names, prj, versions = NULL, supports = FALSE) { stopifnot(is.null(versions) || length(names) == length(versions)) if (supports) { lib_path <- file.path(prj$path, "deployment", "sbox") } else { lib_path <- file.path(prj$path, "deployment", "libs") } installed <- installed.packages(lib.loc = lib_path, noCache = T)[, "Package"] pass <- setequal(installed, names) if (pass) { msg <- "" } else if (length(setdiff(names, installed)) > 0) { msg <- sprintf("Package(s) %s failed to install", paste(setdiff(names, installed), collapse = ", ")) } else if (length(setdiff(installed, names)) > 0) { msg <- sprintf("Unexpected package(s) %s installed", paste(setdiff(installed, names), collapse = ", ")) } else { stop(sprintf("Unexpected condition occured: %s != %s!!!", paste(names, collapse = ", "), paste(installed, collapse = ", "))) } if (pass && !is.null(versions)) { inst_vers <- as.data.frame(installed.packages(lib.loc = lib_path, noCache = T), stringsAsFactors = F)[, c("Package", "Version")] expt_vers <- data.frame(Package = names, Expected = versions) failed_vers <- merge(x = inst_vers, y = expt_vers, by.x = "Package", by.y = "Package") failed_vers <- failed_vers[!is.na(failed_vers$Expected) & failed_vers$Version != failed_vers$Expected, ] pass <- nrow(failed_vers) == 0 if (!pass) { msg <- sprintf("Unexpected versions installed ([pkg]ver!=exp): %s", paste(sprintf("[%s]%s!=%s", failed_vers$Package, failed_vers$Version, failed_vers$Expected), collapse = ", ")) } } expect(pass, msg) invisible(installed) } expect_that_has_docs <- function(topics, pkg_name, prj) { doc_path <- file.path(prj$path, "deployment", "libs", pkg_name, "help", "AnIndex") if (!file.exists(doc_path)) { pass <- F msg <- sprintf("No documentation index found for %s", pkg_name) } else { lines <- readLines(doc_path) all_topics <- unlist(lapply(strsplit(lines, "\t"), function(ent) { ent[1] })) pass <- all(topics %in% all_topics) if (!pass) { msg <- sprintf("Documetation topics not found in %s: %s", pkg_name, paste(setdiff(topics, all_topics), collapse = ", ")) } else { msg <- "" } } expect(pass, msg) } expect_that_packages_locked <- function(expects, params) { lock_data <- data.frame(read.dcf(params$lock_path), stringsAsFactors = FALSE) expected_data <- data.frame(Package = names(expects), Expected = expects) locked <- lock_data$Package pass <- setequal(locked, expected_data$Package) if (pass) { msg <- "" } else if (length(setdiff(expected_data$Package, locked)) > 0) { msg <- sprintf("Package(s) %s failed to lock", paste(setdiff(expected_data$Package, locked), collapse = ", ")) } else if (length(setdiff(locked, expected_data$Package)) > 0) { msg <- sprintf("Unexpected package(s) %s locked", paste(setdiff(locked, expected_data$Package), collapse = ", ")) } else { stop(sprintf("Unexpected condition occured: %s != %s!!!", paste(expected_data$Package, collapse = ", "), paste(locked, collapse = ", "))) } if (pass) { failed_vers <- merge(x = lock_data, y = expected_data, by.x = "Package", by.y = "Package") failed_vers <- failed_vers[!is.na(failed_vers$Expected) & failed_vers$Version != failed_vers$Expected, ] pass <- nrow(failed_vers) == 0 msg <- sprintf("Unexpected versions locked ([pkg]ver!=exp): %s", paste(sprintf("[%s]%s!=%s", failed_vers$Package, failed_vers$Version, failed_vers$Expected), collapse = ", ")) } expect(pass, msg) }
getGVGenotype <- function(ped) { if (hasGenotype(ped)) { genotype <- ped[ , c("id", "first", "second")] } else { genotype <- NULL } genotype }
NULL kernel <- setClass('kernel', representation(type='character', kernel='matrix', pathway='pathway')) setValidity('kernel', function(object){ msg <- NULL valid <- TRUE if( !isSymmetric(round(object@kernel),10) ){ valid <- FALSE msg <- c(msg, "kernel matrix has to be symmetric") } lambda <- min(eigen(object@kernel, only.values=TRUE, symmetric=TRUE)$values) if(lambda < -1e-6){ valid <- FALSE msg <- c(msg, "kernel matrix has to be positive semi-definite") } if(valid) TRUE else msg }) lowrank_kernel <- setClass('lowrank_kernel', slots=c(type='character', kernel='matrix', pathway='pathway')) setValidity('lowrank_kernel', function(object){ msg <- NULL valid <- TRUE if( sum(is.na(object@kernel))>0 ){ valid <- FALSE msg <- c(msg, "kernel matrix has missing values") } if(valid) TRUE else msg }) setGeneric('calc_kernel', function(object, ...) standardGeneric('calc_kernel')) setMethod('calc_kernel', signature(object = 'GWASdata'), definition = function(object, pathway, knots = NULL, type = c('lin', 'sia', 'net'), calculation = c('cpu', 'gpu'), ...) { type <- match.arg(type) calculation <- match.arg(calculation) if(!inherits(object, "GWASdata")){ stop("GWASdata must inherit from class 'GWASdata'") } if(!inherits(pathway, "pathway")){ stop("GWASdata must inherit from class 'pathway'") } if(!is.null(knots) && !inherits(knots, "GWASdata")){ stop("knots must inherit from class 'GWASdata'") } pwIdTest <- try(unique(object@anno$snp[which(object@anno$pathway == pathway@id)])) if(class(pwIdTest) == "try-error"){ print(setdiff(pathway@id, object@anno$pathway)) stop("The above-mentioned pathways cannot be found in the annotation file. \n") } k <- eval(parse(text=paste(type, "_kernel( object = object, pathway = pathway, knots = knots, calculation = calculation, ...)",sep=''))) return(k) }) setGeneric('lin_kernel', function(object, ...) standardGeneric('lin_kernel')) setMethod('lin_kernel', signature(object = 'GWASdata'), definition = function(object, pathway, knots=NULL, calculation = c('cpu', 'gpu'), ...) { calculation <- match.arg(calculation) lowrank <- !is.null(knots) SNPset <- unique(object@anno$snp[which(object@anno$pathway == pathway@id)]) Z1 <- as(object@geno[,as.character(SNPset)],'matrix') if(any(is.na(Z1))) stop("genotype information contains missing values") if(lowrank){ Z2 <- knots@geno Z2 <- as(Z2[,as.character(SNPset)],'matrix') k <- Z1 %*% t(Z2) return(new('lowrank_kernel', type='lin', kernel=k, pathway=pathway)) }else{ if(calculation=='cpu'){ k <- tcrossprod(Z1) } if(calculation=='gpu'){ if(require(gputools)){ Z <- as.numeric(Z1) k <- gputools::gpuMatMult(Z1,t(Z1)) }else{ stop("Please install package 'gputools' to run matrix multiplication on GPU") } } return(new('kernel', type='lin', kernel=k, pathway=pathway)) } }) setGeneric('sia_kernel', function(object, ...) standardGeneric('sia_kernel')) setMethod('sia_kernel', signature(object = 'GWASdata'), definition = function(object, pathway, knots=NULL, calculation = c('cpu', 'gpu'), ...) { calculation <- match.arg(calculation) if(calculation=='gpu'){ stop("GPU calculation is not available for SIA kernel") } SNPset <- unique(object@anno$snp[which(object@anno$pathway==pathway@id)]) z1 <- as(object@geno[,as.character(SNPset)],'matrix') if(any(is.na(z1))) stop("genotype information contains missing values") lowrank <- !is.null(knots) if(lowrank){ z2 <- knots@geno z2 <- as(z2[,as.character(SNPset)],'matrix') if(any(is.na(z2))) stop("genotype information for new observations contains missing values") anno <- object@anno[object@anno[,"pathway"]==pathway@id, c("gene","snp")] genes <- as.character(anno[,"gene"]) gene.counts <- table(genes) max.length <- max(gene.counts) EffectiveNumberSNPs <- function(gene.ids){ effectivesnps <- cbind(as.character(gene.ids),rep(NA, length(gene.ids))) for(g in gene.ids){ snps <- as.matrix(anno[anno$gene==g,"snp"]) ev <- eigen(stats::cor(z1), symmetric=T)$values effectivesnps[effectivesnps[,1]==g,2] <- length(ev)*(1-(length(ev)-1)*stats::var(ev)/(length(ev)^2)) } return(effectivesnps) } gene.ids <- names(gene.counts[gene.counts >= 2]) effSNPs <- EffectiveNumberSNPs(gene.ids) max.eff.length <- max(as.numeric(effSNPs[,2]), na.rm=T) kerneltimes <- matrix(rep(0,nrow(z2)*nrow(z1)),nrow=nrow(z2)) for(g in gene.ids){ g.z1 <- as(z1[,unique(anno[anno[,"gene"]==g,"snp"])],"matrix") g.z2 <- as(z2[,unique(anno[anno[,"gene"]==g,"snp"])],"matrix") n1 <- nrow(g.z1) n2 <- nrow(g.z2) distances <- matrix(rep(rowSums(g.z2^2),n1), nrow=n2) - 2*g.z2%*%t(g.z1) + matrix(rep(rowSums(g.z1^2),n2), nrow=n2, byrow=TRUE) distances <- round(distances, digits=3) length.gene <- as.numeric(ncol(g.z1)) eff.length.g <- as.numeric(effSNPs[which(effSNPs[,1]==g),2]) delta <- sqrt(eff.length.g/max.eff.length) roh <- mean(c(distances))^(-delta)*(eff.length.g/length.gene)^(-delta) kerneltimes <- kerneltimes - roh*(distances/length.gene)^(delta) } k <- exp( sqrt(1/(length(unique(anno[,"gene"])))) * kerneltimes ) return(lowrank_kernel(type='size-adjusted', kernel=k, pathway=pathway)) }else{ genemat <- function(g, object){ z <- z1 z <- z[, apply(z,2,sum)/(2*nrow(z)) >= 0.001 & apply(z,2,sum)/(2*nrow(z)) < 1] e.val <- eigen(stats::cor(z), symmetric=TRUE, only.values=TRUE)$values nn <- length(e.val) a <- matrix( rep(rowSums(z*z),nrow(z)),nrow=nrow(z)) distances <- a -2*tcrossprod(z) + t(a) distances <- round(distances, digits=3) return( list(distances, (nn*(1-(nn-1)*stats::var(e.val)/(nn^2))), ncol(z)) ) } genemat2 <- function(l, max.eff){ delta <- sqrt(l[[2]]/max.eff) roh <- (mean(c(l[[1]])))^(-delta)*(l[[2]]/l[[3]])^(-delta) return(-roh*(l[[1]]/l[[3]])^(delta)) } anno <- object@anno[object@anno[,"pathway"]==pathway@id, c("gene","snp")] gene.counts <- table(anno[,"gene"]) g.10 <- names(gene.counts[gene.counts >= 2]) liste <- lapply(g.10, genemat, object) get2 <- function(l){ return(l[[2]]) } max.eff <- max( unlist( lapply(liste, get2) ) ) kerneltimes <- matrix( rep(0,(dim(object@pheno)[1])^2), nrow=dim(object@pheno)[1]) kerneltimes <- Reduce('+', lapply(liste,genemat2,max.eff)) k <- exp( sqrt(1/(length(unique(anno[,"gene"])))) * kerneltimes ) return(kernel(type='size-adjusted',kernel=k,pathway=pathway)) } }) setGeneric('net_kernel', function(object, ...) standardGeneric('net_kernel')) setMethod('net_kernel', signature(object = 'GWASdata'), definition = function(object, pathway, knots=NULL, calculation = c('cpu', 'gpu'), ...) { calculation <- match.arg(calculation) lowrank <- !is.null(knots) SNPset <- unique(object@anno$snp[which(object@anno$pathway==pathway@id)]) Z1 <- as(object@geno[,as.character(SNPset)],'matrix') if (any(is.na(Z1))) stop("genotype information contains missing values") ANA <- get_ana(object@anno, SNPset, pathway) if(lowrank){ Z2 <- knots@geno Z2 <- as(Z2[,as.character(SNPset)],'matrix') K <- Z1 %*% ANA %*% t(Z2) return(lowrank_kernel(type='network', kernel=K, pathway=pathway)) } if(calculation=='cpu'){ K <- Z1 %*% ANA %*% t(Z1) } if(calculation=='gpu'){ K1 <- gputools::gpuMatMult(Z1,ANA) K <- gputools::gpuMatMult(K1,t(Z1)) } return(kernel(type='network',kernel=K,pathway=pathway)) }) setGeneric('rewire_network', function(object, ...) standardGeneric('rewire_network')) setMethod('rewire_network', signature(object = 'pathway'), definition = function(object, x) { remov <- x if(length(remov)==0){ return(object) } N <- as.matrix(object@adj) N[N!=0] <- object@sign if(any(is.na(N))) stop("network information contains missing values") a <- (N[remov,]!=0) if(is.null(dim(a))){ a <- rbind(rep(0,length(a)),a) } ind_sub <- which(colSums(a)!= 0) Nsub <- N[ind_sub, ind_sub] if(is.null(dim(Nsub))){ N <- N[!(colnames(N)%in%colnames(N)[remov]),!(colnames(N)%in%colnames(N)[remov])] object@adj <- abs(N) object@sign <- as.vector(N[N!=0]) return(object) } diag(Nsub) <- 0 if(sum(Nsub!=0)==0){ N <- N[!(colnames(N)%in%colnames(N)[remov]),!(colnames(N)%in%colnames(N)[remov])] object@adj <- abs(N) object@sign <- as.vector(N[N!=0]) return(object) } Nsub2step <- Nsub %*% Nsub Nsub2step[Nsub2step>0] <- 1 Nsub2step[Nsub2step<0] <- -1 check_contradicts <- ((Nsub != 0) & (Nsub + Nsub2step == 0)) if(any(check_contradicts)){ Nsub2step[(Nsub != 0) & (Nsub + Nsub2step == 0)] <- 0 message('Interaction types contradict after rewiring: Edges removed.') } N[ind_sub,ind_sub] <- Nsub2step N <- N[!(colnames(N)%in%colnames(N)[remov]),!(rownames(N)%in%rownames(N)[remov])] object@adj <- abs(N) object@sign <- as.vector(N[N!=0]) return(object) }) setGeneric('get_ana', function(x, ...) standardGeneric('get_ana')) setMethod('get_ana', signature = 'data.frame', definition = function(x, SNPset, pathway){ net_genes <- get_genes(pathway) anno_sub <- x[x$pathway==pathway@id,] anno_genes <- unique(anno_sub$gene) remov <- which(! net_genes %in% anno_genes) pathway <- rewire_network(pathway, remov) N <- as.matrix(pathway@adj) N[N!=0] <- pathway@sign if(any(is.na(N))) stop("network information contains missing values") diag(N) <- 1 N <- make_psd(N) Atab <- table(anno_sub[c('snp','gene')]) Amat <- as(Atab, 'matrix') A <- Amat[SNPset,rownames(N)] A.star <- t(t(A) / sqrt(colSums(A))) return(A.star %*% N %*% t(A.star)) }) setGeneric('make_psd', function(x, ...) standardGeneric('make_psd')) setMethod('make_psd', signature = 'matrix', definition = function(x, eps = sqrt(.Machine$double.eps)) { lambda <- min(eigen(x, only.values = TRUE, symmetric = TRUE)$values) lambda <- lambda - sqrt(.Machine$double.eps) if (lambda < -1e-10) { rho <- 1/(1-lambda) x <- rho * x + (1-rho) * diag(dim(x)[1]) x <- make_psd(x) } return(x) }) setMethod('show', signature('kernel'), definition = function(object){ cat('An object of class ', class(object), ' of type ', object@type,' for pathway ', object@pathway@id, '\n\n',sep='') print(object@kernel) invisible(NULL) }) setGeneric('summary', function(object) standardGeneric('summary')) setMethod('summary', signature='kernel', definition = function(object){ cat('An object of class ', class(object), ' of type ', object@type,' for pathway ', object@pathway@id, ' with values: \n',sep='') print(summary(as.vector(object@kernel))) cat(paste('\nNumber of Individuals:',dim(object@kernel)[1]),'\n') cat(paste('The pathway contains',dim(object@pathway@adj)[1],'genes.\n')) invisible(NULL) }) if (!isGeneric("plot")) setGeneric('plot') setMethod('plot', signature(x='kernel',y='missing'), function(x, y=NA, hclust=FALSE, ...){ if(hclust) { stats::heatmap(x@kernel, symm=TRUE, col=rev(grDevices::heat.colors(n=20)), Colv=NA,labRow=NA,labCol=NA, main=list(paste('Genetic Similarity Kernel Matrix for Pathway',x@pathway@id), cex=1.4), ...) }else{ print(lattice::levelplot(x@kernel, col.regions=rev(grDevices::heat.colors(n=20)), drop.unused.levels=FALSE, scales=list(alternating=0), main=paste('Genetic Similarity Kernel Matrix for Pathway',x@pathway@id)), ...) } invisible(NULL) })
LModularity <- function(cor.matrix, method = optimal.community, ...){ if(any(cor.matrix < 0)){ warning("Some correlations are negative. Using squared correlations.") cor.matrix = cor.matrix^2 } g = graph.adjacency(cor.matrix, weighted = TRUE, mode = 'undirected') comm = method(g, ...) modules = unique(comm$membership) mod_hypothesis = matrix(0, dim(cor.matrix)[1], length(modules)) for (mod in modules){ mod_hypothesis[comm$membership == mod, mod] = 1 } output = list("LModularity" = comm$modularity, "Modularity_hypothesis" = mod_hypothesis) return(output) }
LBRecap.custom.part=function( data, last.column.count=FALSE, partition, neval=1000, by.incr=1, output=c("base","complete")){ output=match.arg(output) if(!(any(c("data.frame","matrix","array","table") %in% class(data)))){ stop("input data must be a data.frame or a matrix object or an array") } data.matrix=data if( !("matrix" %in% class(data)) & any(c("array","table") %in% class(data))){ n.occ=length(dim(data)) mm=matrix(ncol=n.occ,nrow=2^n.occ) for(i in 1:2^n.occ){ mm[i,]=as.numeric(intToBits(i-1)>0)[1:n.occ] } data.vec=c(data) data[1]=0 dd=c() for(i in 1:length(data)){ dd=c(dd,rep(mm[i,],data.vec[i])) } data.matrix=matrix(dd,ncol=length(dim(data)),byrow=T) } if(!("matrix" %in% class(data))){ data.matrix=as.matrix(data) } if(last.column.count){ if (any(data[,ncol(data)]<0)){ stop("Last column must contain non negative frequencies/counts") } data=as.matrix(data) data.matrix=matrix(ncol=(ncol(data)-1)) for(i in 1:nrow(data)){ d=rep(data[i,1:(ncol(data)-1)],(data[i,ncol(data)])) dd=matrix(d,ncol=(ncol(data)-1),byrow=T) data.matrix=rbind(data.matrix,dd) } data.matrix=data.matrix[-1,] } if(any(data.matrix!=0 & data.matrix!=1)) stop("data must be a binary matrix") if(sum(apply(data.matrix,1,sum)==0)){ warning("input data argument contains rows with all zeros: these rows will be removed and ignored") data.matrix=data.matrix[apply(data.matrix,1,sum)!=0,] } t=ncol(data.matrix) M=nrow(data.matrix) p.p=c() for(r in 1:length(partition)){ pp=unique(partition[[r]]) p.p=c(p.p,pp) } p.p=sort(unique(p.p)) cond1=!all(sort(unlist(list.historylabels(ncol(data.matrix))))==p.p) cond2=max(table(unlist(partition)))>1 if(cond1 | cond2) stop("The input argument 'partition' does not represent a partition of the set of all partial capture histories") partial=pch(data.matrix) mm1=matrix(NA,ncol=ncol(data.matrix),nrow=nrow(data.matrix)) mm0=matrix(NA,ncol=ncol(data.matrix),nrow=nrow(data.matrix)) cc=c() n.obs.1=c() n.obs.0=c() n.unobs=c() p.1=matrix(ncol = length(partition), nrow = neval) log.lik=c() for(k in 1:length(partition)){ for(i in 1:nrow(data.matrix)){ for(j in 1:ncol(data.matrix)){ mm1[i,j]=any(partition[[k]]==partial[i,j]) & data.matrix[i,j]==1 mm0[i,j]=any(partition[[k]]==partial[i,j]) & data.matrix[i,j]==0 } } n.obs.0[k]=sum(mm0) n.obs.1[k]=sum(mm1) } for(k in 1:length(partition)){ for(j in 1:ncol(data.matrix)){ cc[j]=any(partition[[k]]==pch(matrix(rep(0,ncol(data.matrix)),nrow=1))[j]) } n.unobs[k]=sum(cc) } failure.conditions=F if(sum(as.logical(n.unobs))==1){failure.conditions=T} for(l in 1:neval){ val=seq(M,((M+(neval-1)*by.incr)),by=by.incr) nn.0=n.obs.0+(n.unobs*(l-1)*by.incr) nn.1=n.obs.1 p.1[l,]=nn.1/(nn.0+nn.1) n.00=nn.0[nn.0>0] n.10=nn.1[nn.0>0] n.01=nn.0[nn.1>0] n.11=nn.1[nn.1>0] log.lik[l]=lchoose((sum(nn.1)+sum(nn.0))/t,M)+(sum(n.11*log(n.11/(n.11+n.01)))+(sum(n.00*log(n.00/(n.10+n.00))))) } AIC=-2*max(log.lik)+2*(length(partition)+1) u=which(log.lik==max(log.lik))-1 N.hat=M+(u*by.incr) pH.hat=p.1[which(log.lik==max(log.lik)),] names(pH.hat)=paste("pH",1:length(pH.hat),sep="") z2=(qnorm(0.975))^2 Nplus=N.hat if(N.hat<(M+neval*(by.incr))){ i=1 while((2*(max(log.lik)-log.lik[(which(log.lik==(max(log.lik)))+i)])<z2 & (N.hat+i*(by.incr))< M+neval*(by.incr))) { i=i+1 } Nplus=N.hat+i*by.incr } Nminus=N.hat if(N.hat>M){ i=1 while(2*(max(log.lik)-log.lik[(which(log.lik==(max(log.lik)))-i)])<z2 &(N.hat-i*(by.incr))>M){ i=i+1 } Nminus=N.hat-i*by.incr } out=switch(output, base=list(N.hat=N.hat,CI=c(Nminus,Nplus),AIC=AIC,L.Failure=FALSE), complete=list(N.hat=N.hat,CI=c(Nminus,Nplus),pH.hat=pH.hat,AIC=AIC,L.Failure=FALSE,log.lik=log.lik,N=seq(M,neval*by.incr+M,by=by.incr)[-neval+1],Partition=partition)) if(failure.conditions==T){ n1=n.obs.1[which(n.unobs!=0)] n0=n.obs.0[which(n.unobs!=0)] if((M*(t-1)-n0<=((M-1)*(t-1))/2-1) & n1==M) { out=switch(output, base=list(N.hat=NA,CI=c(NA,NA),AIC=-Inf,L.Failure=TRUE), complete=list(N.hat=NA,CI=c(NA,NA),AIC=-Inf,L.Failure=TRUE,log.lik=log.lik,N=seq(M,neval*by.incr+M,by=by.incr)[-neval+1],Partition=partition)) } } return(out) }
plot.ref.grid = function(x, ...) { plot(x = as(x, "emmGrid"), ...) } ref.grid = function(...) { .Deprecated(new = "ref_grid", old = "ref.grid", package = "emmeans") ref_grid(...) } recover.data = function(object, ...) UseMethod("recover.data") recover.data.call = function(...) recover_data.call(...) .chk_recover_data = function(object, ...) { has.old.meth = paste0("recover.data.", class(object)[1]) %in% methods("recover.data") if (has.old.meth) { recover.data(object, ...) } else recover_data(object, ...) } lsm.basis = function(object, ...) UseMethod("lsm.basis") .chk_emm_basis = function(object, ...) { has.old.meth = paste0("lsm.basis.", class(object)[1]) %in% methods("lsm.basis") if (has.old.meth) { lsm.basis(object, ...) } else emm_basis(object, ...) } NULL
distNumeric <- function(x, y, method = "mrw", xyequal = TRUE) { if((is.matrix(x)&&is.matrix(x))==FALSE) stop("x and y must be a matrix object!") if(ncol(x)!=ncol(y)) stop(sQuote("x")," and ",sQuote("y"), " must have the same number of columns") if (xyequal == TRUE) { span <- apply(x, 2, function(x) max(x)-min(x)) variance <- apply(x, 2, var) } else { span <- apply(rbind(x,y), 2, function(x) max(x)-min(x)) variance <- apply(rbind(x,y), 2, var) } span.2 <- span^2 num_distance <- c("mrw", "sev", "ser", "ser.2", "se") method <- match.arg(method, num_distance) result <- switch(method, mrw = weightedNum(x, y, p = 1, alpha = span), ser = weightedNum(x, y, p = 2, alpha = span), ser.2 = weightedNum(x, y, p = 2, alpha = span.2), sev = weightedNum(x, y, p = 2, alpha = variance), se = weightedNum(x, y, p = 2, alpha = 1)) return(result) }
hullArea <- function (x,y) { ne <- length(x) harea <- abs (0.5 * ( (x[1:(ne-1)] %*% y[2:ne]) - ( y[1:(ne-1)] %*% x[2:ne]) ) ) harea }
loadNamespace("fields") loadNamespace("graphics") loadNamespace("ggplot2") loadNamespace("hash") map.build <- function(data, labels=NULL, xdim=10, ydim=5, alpha=.3, train=1000, normalize=FALSE, seed=NULL, minimal=FALSE) { if (alpha <= 0 || alpha > 1) stop("invalid value for alpha") if (xdim < 5 || ydim < 5) stop("map is too small.") if (!is.data.frame(data)) stop("training data has to be a data frame") if (!all(sapply(data,is.numeric))) stop("only numeric data can be used for training") if (!is.null(labels) && !is.data.frame(labels)) labels <- data.frame(labels) if (normalize) data <- map.normalize(data) if (!is.null(seed) && seed <= 0) stop("seed value has to be a positive integer value") if (!is.null(seed) && !test.integer(seed)) stop("seed value has to be a positive integer value") if (!test.integer(train)) stop("train value has to be a positive integer value") neurons <- vsom.f(data, xdim=xdim, ydim=ydim, alpha=alpha, train=train, seed=seed) neurons <- data.frame(neurons) names(neurons) <- names(data) map <- list(data=data, labels=labels, xdim=xdim, ydim=ydim, alpha=alpha, train=train, normalize=normalize, seed=seed, neurons=neurons) if (minimal) { class(map) <- "map.minimal" return(map) } else { class(map) <- "map" } map$heat <- compute.heat(map) map$fitted.obs <- map.fitted.obs(map) map$centroids <- compute.centroids(map) map$unique.centroids <- get.unique.centroids(map) map$centroid.labels <- majority.labels(map) map$label.to.centroid <- compute.label.to.centroid(map) map$centroid.obs <- compute.centroid.obs(map) map$convergence <- map.convergence(map,verb=FALSE) map$wcss <- compute.wcss(map) map$bcss <- compute.bcss(map) return(map) } map.summary <- function(map, verb=TRUE) { if (class(map) != "map") stop("first argument is not a map object.") value <- list() header <- c("xdim", "ydim", "alpha", "train", "normalize", "seed", "instances", "columns") v <- c(map$xdim, map$ydim, map$alpha, map$train, if (map$normalize) "TRUE" else "FALSE", if (is.null(map$seed)) "NULL" else map$seed, nrow(map$data), ncol(map$data)) df <- data.frame(t(v)) names(df) <- header row.names(df) <- " " value$training.parameters <- df header <- c("convergence","separation","clusters") v <- c(map$convergence, 1.0 - map$wcss/map$bcss, length(map$unique.centroids)) df <- data.frame(t(v)) names(df) <- header row.names(df) <- " " value$quality.assessments <- df class(value) <- "summary.map" if (verb) { cat("\n") cat("Training Parameters:\n") print(value$training.parameters) cat("\n") cat("Quality Assessments:\n") print(format(value$quality.assessments,digits=2)) cat("\n") } else { value } } map.starburst <- function(map) { if (class(map) != "map") stop("first argument is not a map object.") plot.heat(map) } map.significance <- function(map,graphics=TRUE,feature.labels=TRUE) { if (class(map) != "map") stop("first argument is not a map object.") data.df <- data.frame(map$data) nfeatures <- ncol(data.df) var.v <- array(data=1,dim=nfeatures) for (i in 1:nfeatures) { var.v[i] <- var(data.df[[i]]) } var.sum <- sum(var.v) prob.v <- var.v/var.sum if (graphics) { par.v <- map.graphics.set() y <- max(prob.v) plot.new() plot.window(xlim=c(1,nfeatures),ylim=c(0,y)) box() title(xlab="Features",ylab="Significance") xticks <- seq(1,nfeatures,1) yticks <- seq(0,y,y/4) if (feature.labels) xlabels <- names(data.df) else xlabels <- seq(1,nfeatures,1) ylabels <- formatC(seq(0,y,y/4),digits=2) axis(1,at=xticks,labels=xlabels) axis(2,at=yticks,labels=ylabels) points(1:nfeatures,prob.v,type="h") map.graphics.reset(par.v) } else { prob.v } } map.marginal <- function(map,marginal) { if (class(map) != "map") stop("first argument is not a map object.") if (!typeof(marginal) == "character") { train <- data.frame(points = map$data[[marginal]]) neurons <- data.frame(points = map$neurons[[marginal]]) train$legend <- 'training data' neurons$legend <- 'neurons' hist <- rbind(train,neurons) ggplot(hist, aes(points, fill = legend)) + geom_density(alpha = 0.2) + xlab(names(map$data)[marginal]) } else if (marginal %in% names(map$data)) { train <- data.frame(points = map$data[names(map$data) == marginal]) colnames(train) <- c("points") neurons <- data.frame(points = map$neurons[names(map$neurons) == marginal]) colnames(neurons) <- c("points") train$legend <- 'training data' neurons$legend <- 'neurons' hist <- rbind(train,neurons) ggplot(hist, aes(points, fill = legend)) + geom_density(alpha = 0.2) + xlab(marginal) } else { stop("second argument is not a data dimension or index") } } map.fitted <- function(map) { if (class(map) != "map") stop("first argument is not a map object.") nobs <- length(map$fitted.obs) labels <- c() for (i in 1:nobs) { nix <- map$fitted.obs[[i]] coord <- coordinate(map,nix) x <- coord$x y <- coord$y c.x <- map$centroids[[x,y]]$x c.y <- map$centroids[[x,y]]$y l <- map$centroid.labels[[c.x,c.y]] labels <- c(labels,l) } labels } map.predict<- function (map,points) { predict.point <- function (x) { if (!is.vector(x)) stop("argument has to be a vector.") if (length(x) != ncol(map$data)) stop("predict vector dimensionality is incompatible") if (map$normalize) x <- as.vector(map.normalize(x)) nix <- best.match(map,x) coord <- coordinate(map,nix) ix <- coord$x iy <- coord$y c.xy <- map$centroids[[ix,iy]] c.nix <- rowix(map,c.xy) label <- map$centroid.labels[[c.xy$x,c.xy$y]] c.ix <- find.centroidix(map,c.xy) vectors <- rbind(map$neurons[c.nix,],x) x.to.c.distance <- max(as.matrix(dist(vectors))[1,]) vectors <- map$neurons[c.nix,] for (i in 1:length(map$centroid.obs[[c.ix]])) { obs.ix <- map$centroid.obs[[c.ix]][i] vectors <- rbind(vectors,map$data[obs.ix,]) } max.o.to.c.distance <- max(as.matrix(dist(vectors))[1,]) max.o.to.c.distance <- max.o.to.c.distance + 0.05*max.o.to.c.distance conf <- 1.0 - (x.to.c.distance/max.o.to.c.distance) return (c(label,conf)) } if (is.vector(points)) points <- t(data.frame(points)) m <- data.frame(t(apply(points,1,predict.point))) names(m) <- c("Label", "Confidence") m } map.position <- function (map,points) { position.point <- function(x) { if (!is.vector(x)) stop("argument has to be a vector.") if (length(x) != ncol(map$data)) stop("vector dimensionality is incompatible") if (map$normalize) x <- as.vector(map.normalize(x)) nix <- best.match(map,x) coord <- coordinate(map,nix) return (c(coord$x,coord$y)) } if (is.vector(points)) points <- t(data.frame(points)) m <- data.frame(t(apply(points,1,position.point))) names(m) <- c("x-dim", "y-dim") m } map.convergence <- function(map,conf.int=.95,k=50,verb=TRUE,ks = TRUE) { if (ks) embed <- map.embed.ks(map,conf.int,verb=FALSE) else embed <- map.embed.vm(map,conf.int,verb=FALSE) topo <- map.topo(map,k,conf.int,verb=FALSE,interval=FALSE) if (verb) return (list(embed=embed,topo=topo)) else return (0.5*embed + 0.5*topo) } test.integer <- function(x) { all.equal(x, as.integer(x), check.attributes = FALSE) } find.centroidix <- function (map,cd) { for (i in 1:length(map$unique.centroids)) { if (cd$x == map$unique.centroids[[i]]$x && cd$y == map$unique.centroids[[i]]$y) { return (i) } } stop("coordinate not a centroid") } compute.centroid.obs <- function (map) { centroid.obs <- array(list(),dim=length(map$unique.centroids)) for (cluster.ix in 1:length(map$unique.centroids)) { c.nix <- rowix(map,map$unique.centroids[[cluster.ix]]) for (i in 1:nrow(map$data)) { coord <- coordinate(map,map$fitted.obs[i]) c.obj.nix <- rowix(map,map$centroids[[coord$x,coord$y]]) if (c.obj.nix == c.nix) { centroid.obs[[cluster.ix]] <- append(centroid.obs[[cluster.ix]],i) } } } as.vector(centroid.obs) } compute.wcss <- function (map) { clusters.ss <- c() for (cluster.ix in 1:length(map$unique.centroids)) { c.nix <- rowix(map,map$unique.centroids[[cluster.ix]]) vectors <- map$neurons[c.nix,] for (i in 1:length(map$centroid.obs[[cluster.ix]])) { obs.ix <- map$centroid.obs[[cluster.ix]][i] vectors <- rbind(vectors,map$data[obs.ix,]) } distances <- as.matrix(dist(vectors))[1,] distances.sqd <- sapply(distances,function(x){x*x}) c.ss <- sum(distances.sqd)/(length(distances.sqd)-1) clusters.ss <- c(clusters.ss,c.ss) } wcss <- sum(clusters.ss)/length(clusters.ss) wcss } compute.bcss <- function (map) { all.bc.ss <- c() c.nix <- rowix(map,map$unique.centroids[[1]]) cluster.vectors <- map$neurons[c.nix,] if (length(map$unique.centroids) > 1) { for (cluster.ix in 2:length(map$unique.centroids)) { c.nix <- rowix(map,map$unique.centroids[[cluster.ix]]) c.vector <- map$neurons[c.nix,] cluster.vectors <- rbind(cluster.vectors,c.vector) } } for (cluster.ix in 1:length(map$unique.centroids)) { c.nix <- rowix(map,map$unique.centroids[[cluster.ix]]) c.vector <- map$neurons[c.nix,] compute.vectors <- rbind(c.vector,cluster.vectors) bc.distances <- as.matrix(dist(compute.vectors))[1,] bc.distances.sqd <- sapply(bc.distances,function(x){x*x}) bc.ss <- sum(bc.distances.sqd)/(length(bc.distances.sqd)-2) all.bc.ss <- c(all.bc.ss,bc.ss) } stopifnot(length(all.bc.ss)==length(map$unique.centroids)) bcss <- sum(all.bc.ss)/length(all.bc.ss) bcss } compute.label.to.centroid <- function (map) { conv <- hash() for (i in 1:length(map$unique.centroids)) { x <- map$unique.centroids[[i]]$x y <- map$unique.centroids[[i]]$y l <- map$centroid.labels[[x,y]] if (is.null(conv[[l]])) { conv[[l]] <- list(i) } else { conv[[l]] <- append(conv[[l]],i) } } conv } map.fitted.obs <- function(map) { fitted.obs <- c() for (i in 1:nrow(map$data)) { b <- best.match(map,map$data[i,]) fitted.obs <- c(fitted.obs,b) } fitted.obs } map.topo <- function(map,k=50,conf.int=.95,verb=FALSE,interval=TRUE) { if (class(map) != "map") stop("first argument is not a map object.") data.df <- as.matrix(map$data) k <- min(k,nrow(data.df)) data.sample.ix <- sample(1:nrow(data.df),size=k,replace=FALSE) acc.v <- c() for (i in 1:k) { acc.v <- c(acc.v, accuracy(map, data.df[data.sample.ix[i],], data.sample.ix[i])) } if (interval) bval <- bootstrap(map,conf.int,data.df,k,acc.v) if (verb) { acc.v } else { val <- sum(acc.v)/k if (interval) list(val=val,lo=bval$lo,hi=bval$hi) else val } } map.embed.vm <- function(map,conf.int=.95,verb=FALSE) { if (class(map) != "map") stop("first argument is not a map object.") map.df <- data.frame(map$neurons) data.df <- data.frame(map$data) vl <- df.var.test(map.df,data.df,conf=conf.int) ml <- df.mean.test(map.df,data.df,conf=conf.int) nfeatures <- ncol(map.df) prob.v <- map.significance(map,graphics=FALSE) var.sum <- 0 for (i in 1:nfeatures) { if (vl$conf.int.lo[i] <= 1.0 && vl$conf.int.hi[i] >= 1.0 && ml$conf.int.lo[i] <= 0.0 && ml$conf.int.hi[i] >= 0.0) { var.sum <- var.sum + prob.v[i] } else { prob.v[i] <- 0 } } if (verb) prob.v else var.sum } map.embed.ks <- function(map,conf.int=.95,verb=FALSE) { if (class(map) != "map") { stop("first argument is not a map object.") } map.df <- data.frame(map$neurons) data.df <- data.frame(map$data) nfeatures <- ncol(map.df) ks.vector <- NULL for(i in 1:nfeatures){ ks.vector[[i]] <- suppressWarnings(ks.test(map.df[[i]], data.df[[i]])) } prob.v <- map.significance(map,graphics=FALSE) var.sum <- 0 for (i in 1:nfeatures) { if (ks.vector[[i]][[2]] > (1 - conf.int)) { var.sum <- var.sum + prob.v[i] } else { prob.v[i] <- 0 } } if (verb) prob.v else var.sum } map.normalize <- function (x) { if (is.vector(x)) { return (scale(x)) } else if (is.data.frame(x)) { df <- data.frame(t(apply(x,1,scale))) names(df) <- names(x) return (df) } else { stop("'x' is not a vector or dataframe.\n") } } bootstrap <- function(map,conf.int,data.df,k,sample.acc.v) { ix <- as.integer(100 - conf.int*100) bn <- 200 bootstrap.acc.v <- c(sum(sample.acc.v)/k) for (i in 2:bn) { bs.v <- sample(1:k,size=k,replace=TRUE) a <- sum(sample.acc.v[bs.v])/k bootstrap.acc.v <- c(bootstrap.acc.v,a) } bootstrap.acc.sort.v <- sort(bootstrap.acc.v) lo.val <- bootstrap.acc.sort.v[ix] hi.val <- bootstrap.acc.sort.v[bn-ix] list(lo=lo.val,hi=hi.val) } best.match <- function(map,obs,full=FALSE) { obs.m <- matrix(as.numeric(obs), nrow(map$neurons), ncol(map$neurons), byrow=TRUE) diff <- map$neurons - obs.m squ <- diff * diff s <- rowSums(squ) d <- sqrt(s) o <- order(d) if (full) o else o[1] } accuracy <- function(map,sample,data.ix) { o <- best.match(map,sample,full=TRUE) best.ix <- o[1] second.best.ix <- o[2] coord <- coordinate(map,best.ix) coord.x <- coord$x coord.y <- coord$y map.ix <- map$fitted.obs[data.ix] coord <- coordinate(map,map.ix) map.x <- coord$x map.y <- coord$y if (coord.x != map.x || coord.y != map.y || best.ix != map.ix) { cat("best.ix: ",best.ix," map.rix: ",map.ix,"\n") stop("problems with coordinates") } best.xy <- coordinate(map,best.ix) second.best.xy <- coordinate(map,second.best.ix) diff.map <- c(best.xy$x,best.xy$y) - c(second.best.xy$x,second.best.xy$y) diff.map.sq <- diff.map * diff.map sum.map <- sum(diff.map.sq) dist.map <- sqrt(sum.map) if (dist.map < 2) 1 else 0 } coord <- function (x=-1,y=-1) { l <- list(x=x,y=y) class(l) <- "coord" l } coordinate <- function(map,rowix) { x <- (rowix-1) %% map$xdim + 1 y <- (rowix-1) %/% map$xdim + 1 coord(x,y) } rowix <- function(map,cd) { if (class(cd) != "coord") stop("expected a coord object") rix <- cd$x + (cd$y-1)*map$xdim rix } map.graphics.set <- function() { par.v <- par() par(ps=6) par.v } map.graphics.reset <- function(par.vector) { par(ps=par.vector$ps) } plot.heat <- function(map) { x <- map$xdim y <- map$ydim centroids <- map$centroids if (x <= 1 || y <= 1) { stop("map dimensions too small") } heat.v <- as.vector(map$heat) heat.v <- cut(heat.v,breaks=100,labels=FALSE) heat <- array(data=heat.v,dim=c(x,y)) colors<- heat.colors(100) par.v <- map.graphics.set() plot.new() plot.window(xlim=c(0,x),ylim=c(0,y)) box() title(xlab="x",ylab="y") xticks <- seq(0.5,x-0.5,1) yticks <- seq(0.5,y-0.5,1) xlabels <- seq(1,x,1) ylabels <- seq(1,y,1) axis(1,at=xticks,labels=xlabels) axis(3,at=xticks,labels=xlabels) axis(2,at=yticks,labels=ylabels) axis(4,at=yticks,labels=ylabels) for (ix in 1:x) { for (iy in 1:y) { rect(ix-1,iy-1,ix,iy,col=colors[100 - heat[ix,iy] + 1],border=NA) } } for(ix in 1:x) { for (iy in 1:y) { cx <- centroids[[ix,iy]]$x cy <- centroids[[ix,iy]]$y points(c(ix,cx)-.5,c(iy,cy)-.5,type="l",col="grey") } } centroid.labels <- majority.labels(map) for(ix in 1:x) { for (iy in 1:y) { lab <- centroid.labels[[ix,iy]] if (lab != none.label) { text(ix-.5,iy-.5,labels=lab) } } } map.graphics.reset(par.v) } compute.centroids <- function(map) { heat <- map$heat xdim <- map$xdim ydim <- map$ydim max.val <- max(heat) centroids <- array(data=list(coord()),dim=c(xdim,ydim)) compute.centroid <- function(ix,iy) { if ((centroids[[ix,iy]])$x > -1 && (centroids[[ix,iy]])$y > -1) { centroids[[ix,iy]] } min.val <- heat[ix,iy] min.x <- ix min.y <- iy if (ix > 1 && ix < xdim && iy > 1 && iy < ydim) { if (heat[ix-1,iy-1] < min.val) { min.val <- heat[ix-1,iy-1] min.x <- ix-1 min.y <- iy-1 } if (heat[ix,iy-1] < min.val) { min.val <- heat[ix,iy-1] min.x <- ix min.y <- iy-1 } if (heat[ix+1,iy-1] < min.val) { min.val <- heat[ix+1,iy-1] min.x <- ix+1 min.y <- iy-1 } if (heat[ix+1,iy] < min.val) { min.val <- heat[ix+1,iy] min.x <- ix+1 min.y <- iy } if (heat[ix+1,iy+1] < min.val) { min.val <- heat[ix+1,iy+1] min.x <- ix+1 min.y <- iy+1 } if (heat[ix,iy+1] < min.val) { min.val <- heat[ix,iy+1] min.x <- ix min.y <- iy+1 } if (heat[ix-1,iy+1] < min.val) { min.val <- heat[ix-1,iy+1] min.x <- ix-1 min.y <- iy+1 } if (heat[ix-1,iy] < min.val) { min.val <- heat[ix-1,iy] min.x <- ix-1 min.y <- iy } } else if (ix == 1 && iy == 1) { if (heat[ix+1,iy] < min.val) { min.val <- heat[ix+1,iy] min.x <- ix+1 min.y <- iy } if (heat[ix+1,iy+1] < min.val) { min.val <- heat[ix+1,iy+1] min.x <- ix+1 min.y <- iy+1 } if (heat[ix,iy+1] < min.val) { min.val <- heat[ix,iy+1] min.x <- ix min.y <- iy+1 } } else if (ix == xdim && iy == 1) { if (heat[ix,iy+1] < min.val) { min.val <- heat[ix,iy+1] min.x <- ix min.y <- iy+1 } if (heat[ix-1,iy+1] < min.val) { min.val <- heat[ix-1,iy+1] min.x <- ix-1 min.y <- iy+1 } if (heat[ix-1,iy] < min.val) { min.val <- heat[ix-1,iy] min.x <- ix-1 min.y <- iy } } else if (ix == xdim && iy == ydim) { if (heat[ix-1,iy-1] < min.val) { min.val <- heat[ix-1,iy-1] min.x <- ix-1 min.y <- iy-1 } if (heat[ix,iy-1] < min.val) { min.val <- heat[ix,iy-1] min.x <- ix min.y <- iy-1 } if (heat[ix-1,iy] < min.val) { min.val <- heat[ix-1,iy] min.x <- ix-1 min.y <- iy } } else if (ix == 1 && iy == ydim) { if (heat[ix,iy-1] < min.val) { min.val <- heat[ix,iy-1] min.x <- ix min.y <- iy-1 } if (heat[ix+1,iy-1] < min.val) { min.val <- heat[ix+1,iy-1] min.x <- ix+1 min.y <- iy-1 } if (heat[ix+1,iy] < min.val) { min.val <- heat[ix+1,iy] min.x <- ix+1 min.y <- iy } } else if (ix == 1 && iy > 1 && iy < ydim) { if (heat[ix,iy-1] < min.val) { min.val <- heat[ix,iy-1] min.x <- ix min.y <- iy-1 } if (heat[ix+1,iy-1] < min.val) { min.val <- heat[ix+1,iy-1] min.x <- ix+1 min.y <- iy-1 } if (heat[ix+1,iy] < min.val) { min.val <- heat[ix+1,iy] min.x <- ix+1 min.y <- iy } if (heat[ix+1,iy+1] < min.val) { min.val <- heat[ix+1,iy+1] min.x <- ix+1 min.y <- iy+1 } if (heat[ix,iy+1] < min.val) { min.val <- heat[ix,iy+1] min.x <- ix min.y <- iy+1 } } else if (ix > 1 && ix < xdim && iy == 1 ) { if (heat[ix+1,iy] < min.val) { min.val <- heat[ix+1,iy] min.x <- ix+1 min.y <- iy } if (heat[ix+1,iy+1] < min.val) { min.val <- heat[ix+1,iy+1] min.x <- ix+1 min.y <- iy+1 } if (heat[ix,iy+1] < min.val) { min.val <- heat[ix,iy+1] min.x <- ix min.y <- iy+1 } if (heat[ix-1,iy+1] < min.val) { min.val <- heat[ix-1,iy+1] min.x <- ix-1 min.y <- iy+1 } if (heat[ix-1,iy] < min.val) { min.val <- heat[ix-1,iy] min.x <- ix-1 min.y <- iy } } else if (ix == xdim && iy > 1 && iy < ydim) { if (heat[ix-1,iy-1] < min.val) { min.val <- heat[ix-1,iy-1] min.x <- ix-1 min.y <- iy-1 } if (heat[ix,iy-1] < min.val) { min.val <- heat[ix,iy-1] min.x <- ix min.y <- iy-1 } if (heat[ix,iy+1] < min.val) { min.val <- heat[ix,iy+1] min.x <- ix min.y <- iy+1 } if (heat[ix-1,iy+1] < min.val) { min.val <- heat[ix-1,iy+1] min.x <- ix-1 min.y <- iy+1 } if (heat[ix-1,iy] < min.val) { min.val <- heat[ix-1,iy] min.x <- ix-1 min.y <- iy } } else if (ix > 1 && ix < xdim && iy == ydim) { if (heat[ix-1,iy-1] < min.val) { min.val <- heat[ix-1,iy-1] min.x <- ix-1 min.y <- iy-1 } if (heat[ix,iy-1] < min.val) { min.val <- heat[ix,iy-1] min.x <- ix min.y <- iy-1 } if (heat[ix+1,iy-1] < min.val) { min.val <- heat[ix+1,iy-1] min.x <- ix+1 min.y <- iy-1 } if (heat[ix+1,iy] < min.val) { min.val <- heat[ix+1,iy] min.x <- ix+1 min.y <- iy } if (heat[ix-1,iy] < min.val) { min.val <- heat[ix-1,iy] min.x <- ix-1 min.y <- iy } } if (min.x != ix || min.y != iy) { compute.centroid(min.x,min.y) } else { coord(ix,iy) } } for (i in 1:xdim) { for (j in 1:ydim) { centroids[[i,j]] <- compute.centroid(i,j) } } centroids } compute.heat <- function(map) { d <- as.matrix(dist(data.frame(map$neurons))) x <- map$xdim y <- map$ydim heat <- array(data=0,dim=c(x,y)) if (x == 1 || y == 1) stop("heat map cannot be computed for a map of dimension 1") xl <- function(ix,iy) { rowix(map,coord(ix,iy)) } if (x > 2 && y > 2) { for (ix in 2:(x-1)) { for (iy in 2:(y-1)) { sum <- d[xl(ix,iy),xl(ix-1,iy-1)] + d[xl(ix,iy),xl(ix,iy-1)] + d[xl(ix,iy),xl(ix+1,iy-1)] + d[xl(ix,iy),xl(ix+1,iy)] + d[xl(ix,iy),xl(ix+1,iy+1)] + d[xl(ix,iy),xl(ix,iy+1)] + d[xl(ix,iy),xl(ix-1,iy+1)] + d[xl(ix,iy),xl(ix-1,iy)] heat[ix,iy] <- sum/8 } } for (ix in 2:(x-1)) { iy <- 1 sum <- d[xl(ix,iy),xl(ix+1,iy)] + d[xl(ix,iy),xl(ix+1,iy+1)] + d[xl(ix,iy),xl(ix,iy+1)] + d[xl(ix,iy),xl(ix-1,iy+1)] + d[xl(ix,iy),xl(ix-1,iy)] heat[ix,iy] <- sum/5 } for (ix in 2:(x-1)) { iy <- y sum <- d[xl(ix,iy),xl(ix-1,iy-1)] + d[xl(ix,iy),xl(ix,iy-1)] + d[xl(ix,iy),xl(ix+1,iy-1)] + d[xl(ix,iy),xl(ix+1,iy)] + d[xl(ix,iy),xl(ix-1,iy)] heat[ix,iy] <- sum/5 } for (iy in 2:(y-1)) { ix <- 1 sum <- d[xl(ix,iy),xl(ix,iy-1)] + d[xl(ix,iy),xl(ix+1,iy-1)] + d[xl(ix,iy),xl(ix+1,iy)] + d[xl(ix,iy),xl(ix+1,iy+1)] + d[xl(ix,iy),xl(ix,iy+1)] heat[ix,iy] <- sum/5 } for (iy in 2:(y-1)) { ix <- x sum <- d[xl(ix,iy),xl(ix-1,iy-1)] + d[xl(ix,iy),xl(ix,iy-1)] + d[xl(ix,iy),xl(ix,iy+1)] + d[xl(ix,iy),xl(ix-1,iy+1)] + d[xl(ix,iy),xl(ix-1,iy)] heat[ix,iy] <- sum/5 } } if (x >= 2 && y >= 2) { ix <- 1 iy <- 1 sum <- d[xl(ix,iy),xl(ix+1,iy)] + d[xl(ix,iy),xl(ix+1,iy+1)] + d[xl(ix,iy),xl(ix,iy+1)] heat[ix,iy] <- sum/3 ix <- x iy <- 1 sum <- d[xl(ix,iy),xl(ix,iy+1)] + d[xl(ix,iy),xl(ix-1,iy+1)] + d[xl(ix,iy),xl(ix-1,iy)] heat[ix,iy] <- sum/3 ix <- 1 iy <- y sum <- d[xl(ix,iy),xl(ix,iy-1)] + d[xl(ix,iy),xl(ix+1,iy-1)] + d[xl(ix,iy),xl(ix+1,iy)] heat[ix,iy] <- sum/3 ix <- x iy <- y sum <- d[xl(ix,iy),xl(ix-1,iy-1)] + d[xl(ix,iy),xl(ix,iy-1)] + d[xl(ix,iy),xl(ix-1,iy)] heat[ix,iy] <- sum/3 } xcoords <- c() ycoords <- c() for (i in 1:y) { for (j in 1:x) { ycoords <- c(ycoords, i) xcoords <- c(xcoords, j) } } xycoords <- data.frame(xcoords,ycoords) heat <- smooth.2d(as.vector(heat), x=as.matrix(xycoords), nrow=x,ncol=y, surface=FALSE, theta=2) heat } df.var.test <- function(df1,df2,conf = .95) { if (length(df1) != length(df2)) stop("cannot compare variances of data frames") var.ratio.v <- array(data=1,dim=length(df1)) var.confintlo.v <- array(data=1,dim=length(df1)) var.confinthi.v <- array(data=1,dim=length(df1)) for (i in 1:length(df1)) { t <- var.test(df1[[i]],df2[[i]],conf.level=conf) var.ratio.v[i] <- t$estimate var.confintlo.v[i] <- t$conf.int[1] var.confinthi.v[i] <- t$conf.int[2] } list(ratio=var.ratio.v, conf.int.lo=var.confintlo.v, conf.int.hi=var.confinthi.v) } df.mean.test <- function(df1,df2,conf = .95) { if (ncol(df1) != ncol(df2)) stop("cannot compare means of data frames") mean.diff.v <- array(data=1,dim=ncol(df1)) mean.confintlo.v <- array(data=1,dim=ncol(df1)) mean.confinthi.v <- array(data=1,dim=ncol(df1)) for (i in 1:ncol(df1)) { t <- t.test(x=df1[[i]],y=df2[[i]],conf.level=conf) mean.diff.v[i] <- t$estimate[1] - t$estimate[2] mean.confintlo.v[i] <- t$conf.int[1] mean.confinthi.v[i] <- t$conf.int[2] } list(diff=mean.diff.v, conf.int.lo=mean.confintlo.v, conf.int.hi=mean.confinthi.v) } vsom.f <- function(data,xdim,ydim,alpha,train,seed) { dr <- nrow(data) dc <- ncol(data) nr <- xdim*ydim nc <- dc if (!is.null(seed)) { set.seed(seed) } else { seed <- -1 } cells <- nr * nc v <- runif(cells,-1,1) neurons <- matrix(v,nrow=nr,ncol=nc) result <- .Fortran("vsom", as.single(neurons), as.single(data.matrix(data)), as.integer(dr), as.integer(dc), as.integer(xdim), as.integer(ydim), as.single(alpha), as.integer(train), as.integer(seed), PACKAGE="popsom") v <- result[1] neurons <- matrix(v[[1]],nrow=nr,ncol=nc,byrow=FALSE) neurons } get.unique.centroids <- function(map) { centroids <- map$centroids xdim <- map$xdim ydim <- map$ydim cd.list <- c() for(ix in 1:xdim) { for(iy in 1:ydim) { c.xy <- centroids[[ix, iy]] b <- sapply(cd.list, function (x) {x$x == c.xy$x && x$y == c.xy$y}) if (!any(b)) { cd.list <- c(cd.list,list(c.xy)) } } } as.vector(cd.list) } none.label <- "<None>" majority.labels <- function(map) { if (is.null(map$labels)) { return (numerical.labels(map)) } x <- map$xdim y <- map$ydim centroids <- map$centroids nobs <- nrow(map$data) centroid.labels <- array(data=list(),dim=c(x,y)) majority.labels <- array(data=none.label,dim=c(x,y)) for(i in 1:nobs) { lab <- as.character(map$labels[i,1]) nix <- map$fitted.obs[i] c <- coordinate(map,nix) ix <- c$x iy <- c$y cx <- centroids[[ix,iy]]$x cy <- centroids[[ix,iy]]$y centroid.labels[[cx,cy]] <- append(centroid.labels[[cx,cy]],lab) } for (ix in 1:x) { for (iy in 1:y) { label.v <- centroid.labels[[ix,iy]] if (length(label.v)!=0) { majority <- data.frame(sort(table(label.v),decreasing=TRUE)) if (nrow(majority) == 1) { majority.labels[[ix,iy]] <- label.v[1] } else { majority.labels[[ix,iy]] <- levels(majority[1,1])[1] } } } } majority.labels } numerical.labels <- function(map) { label_cnt <- 1 centroids <- map$centroids unique.centroids <- map$unique.centroids centroid.labels <- array(data=none.label,dim=c(map$xdim,map$ydim)) for (i in 1:length(unique.centroids)) { label <- paste("centroid",label_cnt,sep=" ") label_cnt <- label_cnt+1 ix <- unique.centroids[[i]]$x iy <- unique.centroids[[i]]$y centroid.labels[[ix,iy]] <- label } centroid.labels } compute.nwcss <- function (map) { clusters.ss <- c() for (cluster.ix in 1:length(map$unique.centroids)) { c.nix <- rowix(map,map$unique.centroids[[cluster.ix]]) vectors <- map$neurons[c.nix,] for (i in 1:length(map$centroid.obs[[cluster.ix]])) { obs.ix <- map$centroid.obs[[cluster.ix]][i] obs.nix <- map$fitted[[obs.ix]] obs.coord <- coordinate(map,obs.nix) centroid.coord <- map$centroids[[obs.coord$x,obs.coord$y]] centroid.nix <- rowix(map,centroid.coord) if (centroid.nix == c.nix){ vectors <- rbind(vectors,map$neurons[obs.nix,]) } } distances <- as.matrix(dist(vectors))[1,] distances.sqd <- sapply(distances,function(x){x*x}) c.ss <- sum(distances.sqd)/(length(distances.sqd)-1) clusters.ss <- c(clusters.ss,c.ss) } wcss <- sum(clusters.ss)/length(clusters.ss) wcss } avg.homogeneity <- function(map) { if (is.null(map$labels)) { stop("you need to attach labels to the map") } if (map$xdim <= 1 || map$ydim <= 1) { stop("map dimensions too small") } x <- map$xdim y <- map$ydim centroids <- map$centroids nobs <- nrow(map$data) centroid.labels <- array(data=list(),dim=c(x,y)) for(i in 1:nobs) { lab <- as.character(map$labels[i,1]) nix <- map$fitted.obs[i] c <- coordinate(map,nix) ix <- c$x iy <- c$y cx <- centroids[[ix,iy]]$x cy <- centroids[[ix,iy]]$y centroid.labels[[cx,cy]] <- append(centroid.labels[[cx,cy]],lab) } sum.majority <- 0 n.centroids <- 0 for (ix in 1:x) { for (iy in 1:y) { label.v <- centroid.labels[[ix,iy]] if (length(label.v)!=0) { n.centroids <- n.centroids + 1 majority <- data.frame(sort(table(label.v),decreasing=TRUE)) if (nrow(majority) == 1) { m.val <- length(label.v) } else { m.val <- majority[1,2] } sum.majority <- sum.majority + m.val } } } list(homog=sum.majority/nobs, nclust=n.centroids) }
roc_aunu <- function(data, ...) { UseMethod("roc_aunu") } roc_aunu <- new_prob_metric( roc_aunu, direction = "maximize" ) roc_aunu.data.frame <- function(data, truth, ..., options = list(), na_rm = TRUE) { estimate <- dots_to_estimate(data, !!! enquos(...)) metric_summarizer( metric_nm = "roc_aunu", metric_fn = roc_aunu_vec, data = data, truth = !!enquo(truth), estimate = !!estimate, estimator = NULL, na_rm = na_rm, event_level = NULL, metric_fn_options = list(options = options) ) } roc_aunu_vec <- function(truth, estimate, options = list(), na_rm = TRUE, ...) { estimator <- "macro" roc_aunu_impl <- function(truth, estimate) { roc_auc_vec( truth = truth, estimate = estimate, options = options, estimator = estimator, na_rm = FALSE, event_level = "first" ) } metric_vec_template( metric_impl = roc_aunu_impl, truth = truth, estimate = estimate, estimator = estimator, na_rm = na_rm, cls = c("factor", "numeric") ) }
getAlbumInfo<-function(album_id,token){ req<-httr::GET(paste0("https://api.spotify.com/v1/albums/",album_id),httr::config(token = token)) json1<-httr::content(req) json2<-jsonlite::fromJSON(jsonlite::toJSON(json1)) df <- data.frame("id" = json2$id, "artist" = as.character(json2$artists$name),"name" = json2$name, "label" = json2$label, "popularity" = json2$popularity, "release_date" = json2$release_date, "album_type" = json2$album_type, "track_total" = json2$tracks$total) return(df) }
lsv <- function(data,k1,p = 6,q = 0,interval = c(0.001,0.999)) { n <- length(data) k <- 1:k1 sdrunning <- function(kscale) { sd(running(data,fun = sum,width = kscale,by = kscale)) } s <- sapply(k,sdrunning) d1 <- sum((s^4)/(k^p)) g2 <- function(H) { ckH <- ((n/k) - (n/k)^(2*H - 1))/(n/k - 1) a1H <- sum(((ckH^2) * (k^(4 * H)))/(k^p)) a2H <- sum((ckH * (k^(2 * H)) * (s^2))/(k^p)) d1 - (a2H^2)/a1H } g3 <- function(H) {g2(H) + (H^(q+1))/(q+1)} Hest <- ifelse(q == 0,optimize(g2,interval = interval), optimize(g3,interval = interval))[[1]] ckHest <- ((n/k) - (n/k)^(2*Hest - 1))/(n/k - 1) a1Hest <- sum(((ckHest^2) * (k^(4 * Hest)))/(k^p)) a2Hest <- sum((ckHest * (k^(2 * Hest)) * (s^2))/(k^p)) sigmaest <- sqrt(a2Hest/a1Hest) return(setNames(c(sigmaest,Hest),c("sigma_estimate","H_estimate"))) }
add.column <- function(lprec, x, indices) { if(missing(indices)) { if(length(x) != dim(lprec)[1]) stop("the length of ", sQuote("x"), " is not equal to the number of ", "constraints in the model") epsel <- .Call(RlpSolve_get_epsel, lprec) indices <- which(abs(x) > epsel) x <- x[indices] } if(length(x) != length(indices)) stop(sQuote("x"), " and ", sQuote("indices"), " are not the same length") .Call(RlpSolve_add_columnex, lprec, as.double(x), as.integer(indices)) invisible() }
higherMomentsIV <- function(formula, data, verbose=TRUE){ cl <- match.call() check_err_msg(checkinput_highermomentsiv_formula(formula=formula)) check_err_msg(checkinput_highermomentsiv_data(data=data)) check_err_msg(checkinput_highermomentsiv_formulaVSdata(formula=formula, data=data)) check_err_msg(checkinput_highermomentsiv_verbose(verbose=verbose)) F.formula <- as.Formula(formula) l.IIV.args <- formula_readout_special(F.formula = F.formula, name.special = "IIV", from.rhs = 3, params.as.chars.only = FALSE) check_err_msg(checkinput_highermomentsiv_docalllist(l.args = l.IIV.args)) l.res.IIVs <- lapply(l.IIV.args, function(iiv.args){ do.call(what = higherMomentsIV_IIV, args = c(alist(data=data, F.formula=F.formula), iiv.args))}) l.data.IIVs <- lapply(l.res.IIVs, "[[","df.IIV") vec.desc.IIVs <- unique(unlist(lapply(l.res.IIVs, "[[","desc.IIV"))) df.data.IIVs <- do.call(cbind, l.data.IIVs) l.F <- formula_build_ivreg(F.formula=F.formula, df.data.iv=df.data.IIVs, vec.desc.IVs = vec.desc.IIVs) F.ivreg <- l.F[["F.ivreg"]] model.desc <- l.F[["model.desc"]] df.data.ivreg <- cbind(data, df.data.IIVs) if(verbose){ message("The following internal instruments were built: ", paste0(vec.desc.IIVs, collapse = ", "),".") message("Fitting an instrumental variable regression with model ", model.desc,".") } res.ivreg <- ivreg(formula = F.ivreg, data = df.data.ivreg) return(new_rendo_ivreg(F.formula = F.formula, res.ivreg = res.ivreg, call = cl)) }
NULL fclust_read <- function(filename = "") { if (nchar(filename) == 0) stop("'filename' cannot be an empty string") tmp <- c("nbElt", "nbAss", "nbXpr", "opt.method", "opt.mean", "opt.model", "opt.jack", "jack", "opt.na", "opt.repeat", "affectElt", "fobs", "mOccur", "xpr", "tree.I", "tree.II", "nbOpt", "mCal", "mPrd", "mMotifs", "mStats", "tCal", "tPrd", "tNbcl", "tStats") fres <- vector(mode = "list", length = length(tmp)) names(fres) <- tmp tmp <- read_foptions(filename) fres$nbElt <- tmp$nbElt fres$nbAss <- tmp$nbAss fres$nbXpr <- tmp$nbXpr fres$opt.method <- tmp$opt.method fres$opt.mean <- tmp$opt.mean fres$opt.model <- tmp$opt.model fres$opt.jack <- tmp$opt.jack fres$jack <- tmp$jack fres$opt.na <- tmp$opt.na fres$opt.repeat <- tmp$opt.repeat fres$affectElt <- tmp$affectElt tmp <- read_finputs(filename, fres$nbElt) fres$fobs <- tmp$fobs fres$mOccur <- tmp$mOccur fres$xpr <- tmp$xpr tmp <- read_ftrees(filename, fres$nbElt) fres$tree.I <- tmp$tree.I fres$tree.II <- tmp$tree.II fres$nbOpt <- tmp$nbOpt tmp1 <- read_fmatrices(filename, fres$nbElt) tmp2 <- read_fstats(filename, fres$nbElt) fres$mCal <- tmp1$mCal fres$mPrd <- tmp1$mPrd fres$mMotifs <- tmp1$mMotifs fres$mStats <- tmp2$mStats fres$tCal <- tmp1$tCal fres$tPrd <- tmp1$tPrd fres$tNbcl <- tmp1$tNbcl fres$tStats <- tmp2$tStats attributes(fres)$class <- "list" return(fres) } fclust_write <- function(fres = NULL, filename = "") { if (nchar(filename) == 0) stop("'filename' cannot be an empty string") if (length(fres) == 0) stop("'fres' cannot be NULL") tmp <- cbind(fres$nbElt, fres$nbAss, fres$nbXpr, fres$opt.method, fres$opt.mean, fres$opt.model, fres$opt.jack, fres$jack[1], fres$jack[2], fres$opt.na, fres$opt.repeat, matrix(fres$affectElt, nrow = 1)) colnames(tmp) <- c("nbElt", "nbAss", "nbXpr", "opt.method", "opt.mean", "opt.model", "opt.jack", "jack.1", "jack.2", "opt.na", "opt.repeat", names(fres$affectElt)) utils::write.table(x = tmp, file = paste(filename, "options", "csv", sep = "."), append = FALSE, col.names = TRUE, row.names = FALSE, sep = ",") tmp <- cbind(names(fres$fobs), fres$fobs, names(fres$xpr), fres$xpr, fres$mOccur) colnames(tmp) <- c("names.fobs", "fobs", "names.xpr", "xpr", colnames(fres$mOccur)) utils::write.table(x = tmp, file = paste(filename, "inputs", "csv", sep = "."), append = FALSE, col.names = TRUE, row.names = FALSE, sep = ",") tmp <- cbind(fres$nbOpt, seq_len(fres$nbElt), fres$tree.I$aff, fres$tree.I$cor, fres$tree.II$aff, fres$tree.II$cor) colnames(tmp) <- c("nbOpt", "nbClu", paste0(colnames(fres$tree.I$aff), ".I"), "R2.I", paste0(colnames(fres$tree.II$aff), ".II"), "R2.II") utils::write.table(x = tmp, file = paste(filename, "trees", "csv", sep = "."), append = FALSE, col.names = TRUE, row.names = FALSE, sep = ",") tmp <- cbind(c(rep("mCal", fres$nbElt), rep("mPrd", fres$nbElt), rep("mMotifs", fres$nbElt), rep("tCal", fres$nbElt), rep("tPrd", fres$nbElt), rep("tNbcl", fres$nbElt)), c(rep(seq_len(fres$nbElt), 6)), rbind(fres$mCal, fres$mPrd, fres$mMotifs, fres$tCal, fres$tPrd, fres$tNbcl)) colnames(tmp) <- c("mat", "nbClu", names(fres$fobs)) utils::write.table(x = tmp, file = paste(filename, "matrices", "csv", sep = "."), append = FALSE, col.names = TRUE, row.names = FALSE, sep = ",") tmp <- cbind(c(rep("mStats", fres$nbElt), rep("tStats", fres$nbElt)), rep(seq_len(fres$nbElt), 2), rbind(fres$mStats, fres$tStats) ) colnames(tmp)[c(1, 2)] <- c("mat", "nbClu") utils::write.table(x = tmp, file = paste(filename, "stats", "csv", sep = "."), append = FALSE, col.names = TRUE, row.names = FALSE, sep = ",") } fclust_plot <- function(fres, nbcl = 0, main = "Title", opt.tree = NULL, opt.perf = NULL, opt.ass = NULL, opt.motif = NULL, opt.comp = NULL, opt.all = NULL ) { if ( is.null(opt.tree) & is.null(opt.perf) & is.null(opt.ass) & is.null(opt.motif) & is.null(opt.comp) & is.null(opt.all) ) { opt.tree <- list("prd", window = Inf, "leg") opt.perf <- list("prd", "pub") opt.motif <- list("obs", "hor", "leg") } if (!is.null(opt.all)) if ((opt.all == "all") | (opt.all == TRUE)) { opt.tree <- list("all") opt.perf <- list("all") opt.motif <- list("all") opt.comp <- list("all") } if (!is.null(opt.tree)) plot_ftrees(fres, nbcl, main, opt.tree) if (!is.null(opt.perf)) plot_fperf( fres, nbcl, main, opt.perf) if (!is.null(opt.ass)) plot_fass( fres, nbcl, main, opt.ass) if (!is.null(opt.motif)) plot_fmotif(fres, nbcl, main, opt.motif) if (!is.null(opt.comp)) plot_fcomp( fres, nbcl, main, opt.comp) } fclust <- function(dat, nbElt, weight = rep(1, dim(dat)[2] - nbElt - 1), opt.na = FALSE, opt.repeat = FALSE, opt.method = "divisive", affectElt = rep(1, nbElt), opt.mean = "amean", opt.model = "byelt", opt.jack = FALSE, jack = c(3,4) ) { dat <- format_fclust(dat, nbElt, weight, opt.na, opt.repeat) tree.I <- fit_ftree(dat$fobs, dat$mOccur, dat$xpr, affectElt, opt.method, opt.mean, opt.model) res <- validate_ftree(tree.I, dat$fobs, dat$mOccur, dat$xpr, opt.method, opt.mean, opt.model, opt.jack, jack) fres <- make_fclust(res, opt.na, opt.repeat, affectElt) return(fres) } ftest_read <- function(filename, opt.var = c("components", "assemblages", "performances")) { all.var <- c("components", "assemblages", "performances") if (is.na(pmatch(opt.var, all.var))) stop("'opt.var' should be ", list_in_quote(all.var)) opt.var <- match.arg(opt.var, all.var) file <- paste(filename, "test", opt.var, "csv", sep = ".") if (!file.exists(file)) stop("file '", file, "' does not exist.") tmp <- utils::read.table(file = file, header = TRUE, sep = ",", check.names = FALSE, stringsAsFactors = FALSE) lnames <- unique(tmp[ , 1]) index <- c(1:2) rtest <- vector(mode = "list", length = length(lnames)) names(rtest) <- lnames for (i in seq_along(lnames)) { rtest[[i]] <- as.matrix(tmp[tmp$mat == lnames[i], -index]) rownames(rtest[[i]]) <- seq_len(dim(rtest[[i]])[1]) } return(rtest) } ftest_write <- function(fres, rtest, filename, opt.var = c("components", "assemblages", "performances")) { all.var <- c("components", "assemblages", "performances") if (is.na(pmatch(opt.var, all.var))) stop("'opt.var' should be ", list_in_quote(all.var)) opt.var <- match.arg(opt.var, all.var) col1 <- NULL for (i in seq_along(names(rtest))) col1 <- c(col1, rep(names(rtest)[i], fres$nbElt)) col2 <- rep(seq_len(fres$nbElt), length(names(rtest))) col3 <- NULL for (i in seq_along(names(rtest))) col3 <- rbind(col3, rtest[[i]]) tmp <- cbind(col1, col2, col3) ttt <- switch(opt.var, components = colnames(fres$mOccur), assemblages = unique(rownames(fres$mOccur)), performances = unique(names(fres$xpr)) ) colnames(tmp) <- c("mat", "nbClu", ttt) rownames(tmp) <- as.character(seq_len(length(col1))) utils::write.table(x = tmp, file = paste(filename, "test", opt.var, "csv", sep = "."), append = FALSE, col.names = TRUE, row.names = FALSE, sep = ",") } ftest_plot <- function(fres, rtest, main = "Title", opt.var = c("components", "assemblages", "performances"), opt.crit = "Jaccard", opt.comp = NULL, opt.ass = NULL, opt.perf = NULL ) { if (is.null(opt.var)) { if (is.null(opt.comp) & is.null(opt.ass) & is.null(opt.perf)) stop("'opt.var' or other options should be non-null") if (!is.null(opt.comp)) { option <- "components" } else { if (!is.null(opt.ass)) { option <- "assemblages" } else { option <- "performances" } } } else { all <- c("components", "assemblages", "performances") option <- check_foption(opt.var, all) } if (option == "components") { if (is.null(opt.comp)) opt.comp <- list("all.together", "sorted.tree", "sorted.leg") ftest_plot_components(fres, rtest, main, opt.crit, opt.comp) } if (option == "assemblages") { if (is.null(opt.ass)) opt.ass <- list("all.together", "sorted.leg") ftest_plot_assemblages(fres, rtest, main, opt.crit, opt.ass) } if (option == "performances") { if (is.null(opt.perf)) opt.perf <- list("all.together", "sorted.leg") ftest_plot_performances(fres, rtest, main, opt.crit, opt.perf) } } ftest <- function(fres, opt.var = c("components", "assemblages", "performances"), opt.nbMax = fres$nbOpt, opt.R2 = FALSE, opt.plot = FALSE ) { option <- check_foption(opt.var, c("components", "assemblages", "performances")) rtest <- switch(option, components = ftest_components( fres, opt.nbMax, opt.R2, opt.plot), assemblages = ftest_assemblages( fres, opt.nbMax, opt.R2, opt.plot), performances = ftest_performances(fres, opt.nbMax, opt.R2, opt.plot) ) return(rtest) } fboot_read <- function(filename, opt.var = c("assemblages", "performances")) { i <- 1 while (file.exists( paste(filename, "boot", opt.var, i, "csv", sep = "."))) i <- i + 1 j <- i - 1 lboot <- vector(mode = "list", length = j) for (i in seq_len(j)) { file <- paste(filename, "boot", opt.var, i, "csv", sep = ".") lboot[[i]] <- fboot_read_one_point(filename = file) } return(lboot) } fboot_write <- function(fres, lboot, filename, opt.var = c("assemblages", "performances")) { all.var <- c("assemblages", "performances") if (is.na(pmatch(opt.var, all.var))) stop("'opt.var' should be ", list_in_quote(all.var)) opt.var <- match.arg(opt.var, all.var) i <- 1 for (i in seq_along(lboot)) { file <- paste(filename, "boot", opt.var, i, "csv", sep = ".") fboot_write_one_point(fres, lboot[[i]], filename = file) } } fboot_plot <- function(fres, lboot, main = "", opt.crit = "Jaccard", opt.var = c("assemblages", "performances")) { opt.var <- check_foption(opt.var, c("assemblages", "performances")) if (opt.var == "assemblages") nbItem <- fres$nbAss if (opt.var == "performances") nbItem <- fres$nbXpr rboot <- lboot[[1]] setCrit <- names(rboot)[names(rboot) %in% opt.crit] nrows <- dim(rboot$Jaccard)[1] ncols <- dim(rboot$Jaccard)[2] cols <- extend_vector(fcolours(), nbItem) pchs <- extend_vector(fsymbols(), nbItem) matPlot <- matrix(NA, nrow = ncols, ncol = (nbItem - 1)) crit <- 1 for (crit in seq_along(setCrit)) { main <- paste0(setCrit[crit], " index") for (j in 1:fres$nbOpt) { for (i in 1:(nbItem - 1)) { mat <- matrix(as.numeric(unlist(lboot[[i]][setCrit[crit]])), ncol = ncols, nrow = nrows, byrow = FALSE) matPlot[1:ncols, i] <- mat[j, ] } matPlot[matPlot == 0] <- NA boxplot(matPlot, main = paste(main, paste0("nbcl=", j), sep = " / "), ylim = c(0, 1), xlim = c(1, (nbItem - 1)), ylab = "Jaccard index", xlab = paste("number of", opt.var, "removed", sep = " ")) } for (j in 1:fres$nbOpt) { for (i in 1:(nbItem - 1)) { mat <- matrix(as.numeric(unlist(lboot[[i]][setCrit[crit]])), ncol = ncols, nrow = nrows, byrow = FALSE) matPlot[1:ncols, i] <- mat[j, ] } matPlot[matPlot == 0] <- NA plot(y = apply(matPlot, 2, median, na.rm = TRUE), ylab = "median Jaccard index", x = 1:(nbItem - 1), xlab = paste("number of", opt.var, "removed", sep = " "), main = paste(main, paste0("nbcl=", j), sep = " / "), ylim = c(0, 1), xlim = c(1, (nbItem - 1)), cex = 4, pch = fsymbols()[j], type = "b", bg = "white") } } } fboot <- function(fres, opt.var = c("assemblages", "performances"), nbIter = 1, opt.nbMax = fres$nbOpt, opt.R2 = FALSE, opt.plot = FALSE, filename = "") { opt.var <- check_foption(opt.var, c("assemblages", "performances")) if (nbIter < 1) stop("'nbIter' should be higher than 1") if (opt.var == "assemblages") dimMat <- fres$nbAss - 1 if (opt.var == "performances") dimMat <- fres$nbXpr - 1 rboot <- vector(mode = "list", length = dimMat) for (i in seq_len(dimMat)) { rboot[[i]] <- fboot_one_point(fres, opt.var, nbIter, rm.number = i, seed = i, opt.plot = opt.plot, opt.nbMax = opt.nbMax) if (filename != "") { file <- paste(filename, "boot", opt.var, i, "csv", sep = ".") fboot_write_one_point(fres, rboot[[i]], filename = file) } } return(rboot) }
gh_whoami <- function(.token = NULL, .api_url = NULL, .send_headers = NULL) { .token <- .token %||% gh_token(.api_url) if (isTRUE(.token == "")) { message("No personal access token (PAT) available.\n", "Obtain a PAT from here:\n", "https://github.com/settings/tokens\n", "For more on what to do with the PAT, see ?gh_whoami.") return(invisible(NULL)) } res <- gh(endpoint = "/user", .token = .token, .api_url = .api_url, .send_headers = .send_headers) scopes <- attr(res, "response")[["x-oauth-scopes"]] res <- res[c("name", "login", "html_url")] res$scopes <- scopes res$token <- format(gh_pat(.token)) class(res) <- c("gh_response", "list") res }
.viridisOpts <- function (n, alpha = 1, begin = 0, end = 1, option = "D",...) { if (begin < 0 | end < 0 | begin > 1 | end > 1) { stop("begin and end must be in [0,1]") } option <- switch(option, A = "A", magma = "A", B = "B", inferno = "B", C = "C", plasma = "C", D = "D", viridis = "D", { warning(paste0("Option '", option, "' does not exist. Defaulting to 'viridis'.")) "D" }) map <- viridis::viridis.map[viridis::viridis.map$opt == option, ] map_cols <- grDevices::rgb(map$R, map$G, map$B) fn_cols <- grDevices::colorRamp(map_cols, space = "Lab", interpolate = "spline",...) cols <- fn_cols(seq(begin, end, length.out = n))/255 grDevices::rgb(cols[, 1], cols[, 2], cols[, 3], alpha = alpha) } .gridfn <- function(values, gridsteps=40, margefrac=NULL) { if (is.null(margefrac)) margefrac <- 1/(2*(gridsteps-1)) if(margefrac>=0.5) stop("'margefrac' argument too large") .range <- range(values) marge <- margefrac*diff(.range) Grid <- seq(.range[1L]+marge, .range[2L]-marge, length.out=gridsteps) return(Grid) } plot.SLik <-function(x,y,filled=FALSE, decorations=NULL, color.palette=NULL, plot.axes=NULL, plot.title=NULL, ...) { object <- x fittedPars <- object$colTypes$fittedPars maxlogL <- object$MSL$maxlogL if (is.null(maxlogL)) stop("plot.SLik plots likelihood ratios, hence it requires the ML to have been computed by MSL(.)\n") Ztransf <- function(Z) {exp(Z-maxlogL)} np <- length(fittedPars) has_CI_info <- ( ( ! is.null(object$CIobject)) && is.null(object$CIobject$warn)) if (np==1L) { xf <- object$obspred[,fittedPars] yf <- Ztransf(object$obspred[,attr(object$obspred,"fittedName")]) x <- object$logLs[,fittedPars] y <- Ztransf(object$logLs[,object$colTypes$logLname]) ylim <- max(1,y) if (is.infinite(ylim)) { ylim <- NULL } else {ylim <- c(0,ylim)} plot(xf,yf,main="Summary Likelihood Ratio",xlab=fittedPars,ylab="SL ratio", xlim=range(x), ylim=ylim) points(x=x,y=y,pch=20,cex=0.5,col="red") points(object$MSL$MSLE,1,pch="+") if (has_CI_info) { yci <- rep(exp(-qchisq(0.95,1)/2),NROW(object$CIobject$bounds)) points(object$CIobject$bounds,y=yci,pch="+") } } else if (np==2L) { if (inherits(object$fit,"HLfit")) { if (is.null(plot.title)) { plot.title <- substitute(title(main="Summary-likelihood-ratio surface", xlab=xlabv,ylab=ylabv),list(xlabv=fittedPars[1],ylabv=fittedPars[2])) } if (filled) { decos <- quote({if (!is.null(object$latestPoints)) points(object$logLs[object$latestPoints,fittedPars],pch=".",cex=2); points(object$logLs[ ! object$logLs[,"isValid"],fittedPars],pch=20,cex=0.8); eval(decorations); if (has_CI_info) points(object$CIobject$bounds,pch=19,col="red",cex=1.50); if (has_CI_info) points(object$CIobject$bounds,pch=20,col="white",cex=0.75); points(t(object$MSL$MSLE),pch=19,cex=1.50,col="cyan"); points(t(object$MSL$MSLE),pch=20,cex=0.75,col="white"); } ) if(is.null(plot.axes)) { plot.axes <- quote({axis(1);axis(2); contour(x=xGrid,y=yGrid,z=Zvalues,add=TRUE, nlevels=1, levels=c(0.05))}) } if (is.null(color.palette)) { color.palette <- function(n){adjustcolor(.viridisOpts(n,bias=2),offset = c(0.5, 0.5, 0.3, 0))} } decosf <- substitute({x;y},list(x=quote(points(object$logLs[,fittedPars],cex=0.8)), y=decos)) filled.mapMM(object$fit,Ztransf=Ztransf, color.palette=color.palette,nlevels=50, plot.title=plot.title, plot.axes=plot.axes, decorations=eval(decosf), ...) } else { decos <- quote({if (!is.null(object$latestPoints)) points(object$logLs[object$latestPoints,fittedPars],pch=".",cex=2); points(object$logLs[ ! object$logLs[,"isValid"],fittedPars],pch=20,cex=0.8); eval(decorations); if (has_CI_info) points(object$CIobject$bounds,pch="+",col="black",cex=1.50); points(t(object$MSL$MSLE),pch=3,cex=1.5,lwd=3); points(t(object$MSL$MSLE),pch=3,col="white",cex=1.25); } ) if(is.null(plot.axes)) { plot.axes <- quote({axis(1);axis(2)}) } if (is.null(color.palette)) { color.palette <- function(n){spaMM.colors(n,redshift=3)} } mapMM(object$fit,Ztransf=Ztransf, color.palette=color.palette,nlevels=50, plot.axes=plot.axes, plot.title=plot.title, decorations=decos, ...) } } } else if (np>2L) {.calc_all_slices(object,fittedPars,color.palette,plot.axes=plot.axes)} invisible(object) }
get_committee_by_name <- function(NAME, cycle=2018, page = 1, myAPI_Key){ API = 'campaign-finance' if(!validate_cycle(cycle)) stop("Incorrect cycle") if(is.character(NAME)){ NAME <- gsub(' ', '%20', NAME) query <- sprintf("%s/committees/search.json?query=%s", cycle, NAME) pp_query(query, API, page = page, myAPI_Key = myAPI_Key) } else {stop("The NAME of the committee has to be character")} }
activity_id <- function(x) { UseMethod("activity_id") } activity_id.eventlog <- function(x){ return(attr(x, "activity_id")) } activity_id.eventlog_mapping <- function(x) { return(x$activity_identifier) } activity_id.activitylog <- function(x){ return(attr(x, "activity_id")) } activity_id.activitylog_mapping <- function(x) { return(x$activity_identifier) }
SHASH <- function (mu.link="identity", sigma.link="log", nu.link ="log", tau.link="log") { mstats <- checklink( "mu.link", "Sinh-Arcsinh", substitute(mu.link), c("inverse", "log", "identity", "own")) dstats <- checklink("sigma.link", "Sinh-Arcsinh", substitute(sigma.link), c("inverse", "log", "identity", "own")) vstats <- checklink( "nu.link", "Sinh-Arcsinh", substitute(nu.link), c("inverse", "log", "identity", "own")) tstats <- checklink( "tau.link", "Sinh-Arcsinh", substitute(tau.link), c("inverse", "log", "identity", "own")) structure( list(family = c("SHASH", "Sinh-Arcsinh"), parameters = list(mu=TRUE, sigma=TRUE, nu=TRUE, tau=TRUE), nopar = 4, type = "Continuous", mu.link = as.character(substitute(mu.link)), sigma.link = as.character(substitute(sigma.link)), nu.link = as.character(substitute(nu.link)), tau.link = as.character(substitute(tau.link)), mu.linkfun = mstats$linkfun, sigma.linkfun = dstats$linkfun, nu.linkfun = vstats$linkfun, tau.linkfun = tstats$linkfun, mu.linkinv = mstats$linkinv, sigma.linkinv = dstats$linkinv, nu.linkinv = vstats$linkinv, tau.linkinv = tstats$linkinv, mu.dr = mstats$mu.eta, sigma.dr = dstats$mu.eta, nu.dr = vstats$mu.eta, tau.dr = tstats$mu.eta, dldm = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdm <- -1/sigma dldm <- (1/sigma)*((1+(z^2))^(-1/2))*((-h/c)+(r*c)+z*((1+(z^2))^(-1/2))) dldm <- (dldr*drdz+dldc*dcdz+dldz)*dzdm dldm }, d2ldm2 = function(y,mu,sigma,nu,tau){ z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdm <- -1/sigma dldm <- (1/sigma)*((1+(z^2))^(-1/2))*((-h/c)+(r*c)+z*((1+(z^2))^(-1/2))) dldm <- (dldr*drdz+dldc*dcdz+dldz)*dzdm d2ldm2 <- -dldm*dldm d2ldm2 <- ifelse(d2ldm2 < -1e-15, d2ldm2,-1e-15) d2ldm2 }, dldd = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdd <- -z/sigma dldd <- (dldr*drdz+dldc*dcdz+dldz)*dzdd-1/sigma dldd } , d2ldd2 = function(y,mu,sigma,nu,tau){ z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdd <- -z/sigma dldd <- (dldr*drdz+dldc*dcdz+dldz)*dzdd-1/sigma d2ldd2 <- -dldd*dldd d2ldd2 <- ifelse(d2ldd2 < -1e-15, d2ldd2,-1e-15) d2ldd2 }, dldv = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) dldr <- -r dldc <- 1/c drdv <- (1/2)*asinh(z)*exp(-nu*asinh(z)) dcdv <- (1/2)*(1-nu*asinh(z))*exp(-nu*asinh(z)) dldv <- dldr*drdv+dldc*dcdv dldv } , d2ldv2 = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) dldr <- -r dldc <- 1/c drdv <- (1/2)*asinh(z)*exp(-nu*asinh(z)) dcdv <- (1/2)*(1-nu*asinh(z))*exp(-nu*asinh(z)) dldv <- dldr*drdv+dldc*dcdv d2ldv2 <- -dldv*dldv d2ldv2 <- ifelse(d2ldv2 < -1e-15, d2ldv2,-1e-15) d2ldv2 }, dldt = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) dldr <- -r dldc <- 1/c drdt <- (1/2)*asinh(z)*exp(tau*asinh(z)) dcdt <- (1/2)*(1+tau*asinh(z))*exp(tau*asinh(z)) dldt <- dldr*drdt+dldc*dcdt dldt } , d2ldt2 = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) dldr <- -r dldc <- 1/c drdt <- (1/2)*asinh(z)*exp(tau*asinh(z)) dcdt <- (1/2)*(1+tau*asinh(z))*exp(tau*asinh(z)) dldt <- dldr*drdt+dldc*dcdt d2ldt2 <- -dldt*dldt d2ldt2 <- ifelse(d2ldt2 < -1e-15, d2ldt2,-1e-15) d2ldt2 } , d2ldmdd = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdm <- -1/sigma dldm <- (1/sigma)*((1+(z^2))^(-1/2))*((-h/c)+(r*c)+z*((1+(z^2))^(-1/2))) dldm <- (dldr*drdz+dldc*dcdz+dldz)*dzdm dzdd <- -z/sigma dldd <- (dldr*drdz+dldc*dcdz+dldz)*dzdd-1/sigma d2ldmdd <- -(dldm*dldd) d2ldmdd }, d2ldmdv = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdm <- -1/sigma dldm <- (1/sigma)*((1+(z^2))^(-1/2))*((-h/c)+(r*c)+z*((1+(z^2))^(-1/2))) dldm <- (dldr*drdz+dldc*dcdz+dldz)*dzdm drdv <- (1/2)*asinh(z)*exp(-nu*asinh(z)) dcdv <- (1/2)*(1-nu*asinh(z))*exp(-nu*asinh(z)) dldv <- dldr*drdv+dldc*dcdv d2ldmdv <- -(dldm*dldv) d2ldmdv }, d2ldmdt = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdm <- -1/sigma dldm <- (1/sigma)*((1+(z^2))^(-1/2))*((-h/c)+(r*c)+z*((1+(z^2))^(-1/2))) dldm <- (dldr*drdz+dldc*dcdz+dldz)*dzdm drdt <- (1/2)*asinh(z)*exp(tau*asinh(z)) dcdt <- (1/2)*(1+tau*asinh(z))*exp(tau*asinh(z)) dldt <- dldr*drdt+dldc*dcdt d2ldmdt <- -(dldm*dldt) d2ldmdt }, d2ldddv = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdd <- -z/sigma dldd <- (dldr*drdz+dldc*dcdz+dldz)*dzdd-1/sigma drdv <- (1/2)*asinh(z)*exp(-nu*asinh(z)) dcdv <- (1/2)*(1-nu*asinh(z))*exp(-nu*asinh(z)) dldv <- dldr*drdv+dldc*dcdv d2ldddv <- -(dldd*dldv) d2ldddv }, d2ldddt = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) h <- (1/2)*((tau^2)*exp(tau*asinh(z))-(nu^2)*exp(-nu*asinh(z))) dldz <- -z/(1+(z^2)) dldr <- -r dldc <- 1/c dcdz <- h*((1+(z^2))^(-1/2)) drdz <- c*((1+(z^2))^(-1/2)) dzdd <- -z/sigma dldd <- (dldr*drdz+dldc*dcdz+dldz)*dzdd-1/sigma drdt <- (1/2)*asinh(z)*exp(tau*asinh(z)) dcdt <- (1/2)*(1+tau*asinh(z))*exp(tau*asinh(z)) dldt <- dldr*drdt+dldc*dcdt d2ldddt <- -(dldd*dldt) d2ldddt }, d2ldvdt = function(y,mu,sigma,nu,tau) { z <- (y-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) dldr <- -r dldc <- 1/c drdv <- (1/2)*asinh(z)*exp(-nu*asinh(z)) dcdv <- (1/2)*(1-nu*asinh(z))*exp(-nu*asinh(z)) dldv <- dldr*drdv+dldc*dcdv drdt <- (1/2)*asinh(z)*exp(tau*asinh(z)) dcdt <- (1/2)*(1+tau*asinh(z))*exp(tau*asinh(z)) dldt <- dldr*drdt+dldc*dcdt d2ldvdt <- -(dldv*dldt) d2ldvdt }, G.dev.incr = function(y,mu,sigma,nu,tau,...) -2*dSHASH(y,mu,sigma,nu,tau,log=TRUE), rqres = expression( rqres(pfun="pSHASH", type="Continuous", y=y, mu=mu, sigma=sigma, nu=nu, tau=tau) ), mu.initial = expression(mu <- (y+mean(y))/2), sigma.initial = expression(sigma<- rep(sd(y)/5, length(y))), nu.initial = expression(nu <- rep(.5, length(y))), tau.initial = expression(tau <-rep(.5, length(y))), mu.valid = function(mu) TRUE, sigma.valid = function(sigma) all(sigma > 0), nu.valid = function(nu) TRUE , tau.valid = function(tau) all(tau > 0), y.valid = function(y) TRUE ), class = c("gamlss.family","family")) } dSHASH <- function(x, mu = 0, sigma = 1, nu = .5, tau = .5, log = FALSE) { if (any(sigma < 0)) stop(paste("sigma must be positive", "\n", "")) if (any(tau < 0)) stop(paste("tau must be positive", "\n", "")) if (any(nu < 0)) stop(paste("nu must be positive", "\n", "")) z <- (x-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) c <- (1/2)*(tau*exp(tau*asinh(z))+nu*exp(-nu*asinh(z))) loglik <- -log(sigma)-log(2*pi)/2-log(1+(z^2))/2+log(c)-(r^2)/2 if(log==FALSE) ft <- exp(loglik) else ft <- loglik ft } pSHASH <- function(q, mu = 0, sigma = 1, nu = .5, tau = .5, lower.tail = TRUE, log.p = FALSE) { if (any(sigma < 0)) stop(paste("sigma must be positive", "\n", "")) if (any(tau < 0)) stop(paste("tau must be positive", "\n", "")) if (any(nu < 0)) stop(paste("nu must be positive", "\n", "")) z <- (q-mu)/sigma r <- (1/2)*(exp(tau*asinh(z))-exp(-nu*asinh(z))) p <- pNO(r) if(lower.tail==TRUE) p <- p else p <- 1-p if(log.p==FALSE) p <- p else p <- log(p) p } qSHASH <- function(p, mu=0, sigma=1, nu=.5, tau=.5, lower.tail = TRUE, log.p = FALSE) { h1 <- function(q) { pSHASH(q , mu = mu[i], sigma = sigma[i], nu = nu[i], tau = tau[i]) - p[i] } h <- function(q) { pSHASH(q , mu = mu[i], sigma = sigma[i], nu = nu[i], tau = tau[i]) } if (any(sigma <= 0)) stop(paste("sigma must be positive", "\n", "")) if (log.p==TRUE) p <- exp(p) else p <- p if (lower.tail==TRUE) p <- p else p <- 1-p if (any(p < 0)|any(p > 1)) stop(paste("p must be between 0 and 1", "\n", "")) lp <- max(length(p),length(mu),length(sigma),length(nu), length(tau)) p <- rep(p, length = lp) sigma <- rep(sigma, length = lp) mu <- rep(mu, length = lp) nu <- rep(nu, length = lp) tau <- rep(tau, length = lp) q <- rep(0,lp) for (i in 1:lp) { if (h(mu[i])<p[i]) { interval <- c(mu[i], mu[i]+sigma[i]) j <-2 while (h(interval[2]) < p[i]) {interval[2]<- mu[i]+j*sigma[i] j<-j+1 } } else { interval <- c(mu[i]-sigma[i], mu[i]) j <-2 while (h(interval[1]) > p[i]) {interval[1]<- mu[i]-j*sigma[i] j<-j+1 } } q[i] <- uniroot(h1, interval)$root } q } rSHASH <- function(n, mu=0, sigma=1, nu=.5, tau=.5) { if (any(sigma <= 0)) stop(paste("sigma must be positive", "\n", "")) n <- ceiling(n) p <- runif(n) r <- qSHASH(p,mu=mu,sigma=sigma,nu=nu,tau=tau) r }
proximity_builder <- function(im.res, neighborhood = "ar1", type = c("sparse", "full"), weight = "binary", phi = 1, r = NULL, h = NULL, w = NULL, include.coords = FALSE, print.im = FALSE) { nb.y <- c() J = prod(im.res) row.id <-rep(1, im.res[2]) for (i in 2:im.res[1]) { row.id <- c(row.id, rep(i, im.res[2])) } coords <- data.frame(index = 1:J, row.id = row.id, col.id = rep(c(1:im.res[2]), im.res[1]) ) sparse.adj <- matrix(0, nrow = 4 * 2 + 3 * (2 * im.res[1] + 2 * im.res[2]) + 4 * (J - 4 + 2 * im.res[1] + 2 * im.res[2]), ncol = 2) for (x in 1:im.res[1]) { for (y in 1:im.res[2]) { if (neighborhood == "ar1") { nb.xy <- data.frame(nb.x = c(x - 1, x + 1, x , x), nb.y = c(y, y, y - 1, y + 1)) } if (neighborhood == "rectangle") { xn <- seq(x - w, x + w) yn <- seq(y - h, y + h) nb.x <- rep(xn[1], length(yn)) for (i in 2:length(xn)) { nb.x <- c(nb.x, rep(xn[i], length(yn))) } nb.xy <- data.frame(cbind(nb.x, nb.y = rep(yn, length(xn)))) nb.xy <- dplyr::filter(nb.xy, !(nb.x == x & nb.y == y)) } if (neighborhood == "round") { nb.xy <- neighbors_by_dist(x = x, y = y, r = r, coords = coords, im.res = im.res) nb.xy <- nb.xy[, -3] } nb.xy <- dplyr::filter(nb.xy, dplyr::between(nb.x, 1, im.res[1]) & dplyr::between(nb.y, 1, im.res[2])) nb.index <- coords$index[coords$row.id == nb.xy$nb.x[1] & coords$col.id == nb.xy$nb.y[1]] for (i in 2:nrow(nb.xy)) { nb.index <- c(nb.index, coords$index[coords$row.id == nb.xy$nb.x[i] & coords$col.id == nb.xy$nb.y[i]]) } nb.xy <- cbind(location.index = rep(coords$index[coords$row.id == x & coords$col.id == y], nrow(nb.xy)), x = x, y = y, nb.xy, nb.index) if (x == 1 & y == 1) { nb.xy.old <- nb.xy } else { nb.xy.old <- rbind(nb.xy.old, nb.xy) } } } if (weight == "distance") { nb.xy.old <- dplyr::mutate(nb.xy.old, weights = phi / sqrt((x - nb.x) ^ 2 + (y - nb.y) ^ 2) ) } else { nb.xy.old$weights <- 1 } if (type == "sparse") { if (include.coords == FALSE) { nb.xy.old <- nb.xy.old[ , -c(2:5)] } return(nb.xy.old) } else { adj.mat <- matrix(0, nrow = J, ncol = J) for (i in 1:nrow(nb.xy.old)) { adj.mat[nb.xy.old$location.index[i], nb.xy.old$nb.index[i]] <- nb.xy.old$weights[i] } return(adj.mat) } if (print.im == TRUE) { im <- matrix(1:J, byrow = TRUE, nrow = im.res[1], ncol = im.res[2]) print(im) } }
.getPQ <- function(X, V, Lambda, Sigma, Psi) { p <- NA q <- NA if(!.minu(X)) { if(is.vector(X)) { p <- 1 q <- 1 } else { p <- dim(X)[1] q <- dim(X)[2] } } if(!.minu(V)) { if(is.na(q)) { q <- ifelse(is.vector(V), 1, dim(V)[1]) } } if(!.minu(Lambda)) { if(is.na(p)) p <- ifelse(is.vector(Lambda), 1, dim(Lambda)[1]) if(is.na(q)) q <- ifelse(is.vector(Lambda), 1, dim(Lambda)[2]) } if(!.minu(Sigma)) { if(is.na(p)) p <- ifelse(is.vector(Sigma), 1, dim(Sigma)[1]) } if(!.minu(Psi)) { if(is.na(q)) q <- ifelse(is.vector(Psi), 1, dim(Psi)[1]) } as.numeric(c(p, q)) } .minu <- function(x) missing(x) || is.null(x) .setDims <- function(X, p, q) { var.X <- missing(p) || missing(q) if(var.X) { if(missing(p)) p <- q if(missing(q)) q <- p } if(.minu(X)) { if(!var.X) { X <- matrix(0,p,q) } else { X <- diag(p) } } if(is.vector(X)) X <- array(X, dim = c(1,1,length(X))) if(!all(dim(X)[1:2] == c(p,q))) { X <- NA } else { X <- matrix(X,p) } X } .getN <- function(p, q, X, V, Lambda, Sigma, Psi, nu) { N <- NULL if(!.minu(X)) N <- c(N, ncol(X)/q) if(!.minu(V)) N <- c(N, ncol(V)/q) if(!.minu(Lambda)) N <- c(N, ncol(Lambda)/q) if(!.minu(Sigma)) N <- c(N, ncol(Sigma)/p) if(!.minu(Psi)) N <- c(N, ncol(Psi)/q) if(!.minu(nu)) N <- c(N, length(nu)) N <- unique(sort(N)) c(1, N[N>1]) } .vec2mn <- function(x) { if(is.vector(x)) { x <- matrix(x, ncol = 1) } else { x <- t(x) x <- array(x, dim = c(dim(x)[1], 1, dim(x)[2])) } x }
round_df_char <- function(df, digits, pad = " ", na_vals = NA) { nas <- is.na(df) if (!is.data.frame(df)) { df <- as.data.frame.matrix(df, stringsAsFactors = FALSE) } rn <- rownames(df) cn <- colnames(df) df <- as.data.frame(lapply(df, function(col) { if (suppressWarnings(all(!is.na(as.numeric(as.character(col)))))) { as.numeric(as.character(col)) } else { col } }), stringsAsFactors = FALSE) nums <- vapply(df, is.numeric, FUN.VALUE = logical(1)) df <- as.data.frame(lapply(df, num_print, digits = digits), stringsAsFactors = FALSE) if (any(nas)) { df[nas] <- "" } for (i in which(nums)) { if (any(grepl(".", df[[i]], fixed = TRUE))) { s <- strsplit(df[[i]], ".", fixed = TRUE) lengths <- lengths(s) digits.r.of.. <- sapply(seq_along(s), function(x) { if (lengths[x] > 1) { nchar(s[[x]][lengths[x]]) } else { 0 } }) df[[i]] <- sapply(seq_along(df[[i]]), function(x) { if (df[[i]][x] == "") { "" } else if (lengths[x] <= 1) { paste0(c(df[[i]][x], rep(".", pad == 0), rep(pad, max(digits.r.of..) - digits.r.of..[x] + as.numeric(pad != 0))), collapse = "") } else { paste0(c(df[[i]][x], rep(pad, max(digits.r.of..) - digits.r.of..[x])), collapse = "") } }) } } if (length(rn) > 0) rownames(df) <- rn if (length(cn) > 0) names(df) <- cn df[df == ""] <- na_vals return(df) } get_robust_se <- function(model, type = "HC3", cluster = NULL, data = model.frame(model), vcov = NULL) { if (!requireNamespace("sandwich", quietly = TRUE)) { stop_wrap("When using robust SEs you need to have the \'sandwich\' package.", call. = FALSE) } if (type == TRUE) { type <- "HC3" } if (is.character(cluster)) { call <- getCall(model) if (is.null(data)) { d <- eval(call$data, envir = environment(formula(model))) } else { d <- data } cluster <- d[[cluster]] use_cluster <- TRUE } else if (length(cluster) > 1) { if (!is.factor(cluster) & !is.numeric(cluster)) { warn_wrap("Invalid cluster input. Either use the name of the variable in the input data frame or provide a numeric/factor vector. Cluster is not being used in the reported SEs.") cluster <- NULL use_cluster <- FALSE } else { use_cluster <- TRUE } } else { use_cluster <- FALSE } if (is.null(vcov) & type %in% c("HC4", "HC4m", "HC5") & is.null(cluster)) { coefs <- sandwich::vcovHC(model, type = type) } else if (is.null(vcov) & type %in% c("HC4", "HC4m", "HC5") & !is.null(cluster)) { stop_wrap("If using cluster-robust SEs, robust.type must be HC3 or lower.") } else if (is.null(vcov)) { coefs <- sandwich::vcovCL(model, cluster = cluster, type = type) } else {coefs <- vcov} vcov <- coefs coefs <- coeftest(model, coefs) ses <- coefs[,2] ts <- coefs[,3] ps <- coefs[,4] out <- list(coefs = coefs, ses = ses, ts = ts, ps = ps, use_cluster = use_cluster, type = type, cluster = cluster, vcov = vcov) class(out) <- "robust_info" return(out) } print.robust_info <- function(x, ...) { print(md_table(x$coefs)) } do_robust <- function(model, robust, cluster, data, vcov = NULL) { out <- get_robust_se(model = model, type = robust, cluster = cluster, data = data, vcov = vcov) names(out)[names(out) == "type"] <- "robust" return(out) } create_table <- function(params, which.cols, ivs) { if (any(which.cols %nin% names(params))) { stop_wrap("One of the requested columns does not exist.") } coefs <- params[["Est."]] params <- params[unlist(which.cols)] mat <- matrix(nrow = length(ivs), ncol = length(which.cols)) rownames(mat) <- ivs colnames(mat) <- which.cols for (i in seq_along(params)) { if (length(coefs) > length(params[[i]])) { temp_vec <- rep(NA, times = length(coefs)) temp_vec[which(!is.na(coefs))] <- params[[i]] params[[i]] <- temp_vec } mat[,i] <- params[[i]] } return(mat) } which_columns <- function(which.cols, margins = FALSE, confint, ci.labs, vifs, pvals, t.col, exp = NULL, df = FALSE, others = NULL) { if (!is.null(which.cols)) { return(which.cols) } else { if (is.null(exp) || exp == FALSE) { cols <- c("Est.") } else { cols <- c("exp(Est.)") } if (confint == TRUE) { cols <- c(cols, ci.labs) } else { cols <- c(cols, "S.E.") } if (margins == TRUE) { cols <- c(cols, "A.M.E.") } cols <- c(cols, t.col) if (df == TRUE) {cols <- c(cols, "d.f.")} if (pvals == TRUE) {cols <- c(cols, "p")} if (vifs == TRUE) {cols <- c(cols, "VIF")} if (!is.null(others)) {cols <- c(cols, others)} return(cols) } } part_corr <- function(ts, df.int, rsq, robust, n) { p.df <- length(ts) - df.int df.resid <- n - p.df - 1 partial_corrs <- ts / sqrt(ts^2 + df.resid) if (df.int == 1) { partial_corrs[1] <- NA } semipart_corrs <- (ts * sqrt(1 - rsq))/sqrt(df.resid) if (df.int == 1) { semipart_corrs[1] <- NA } if (!identical(FALSE, robust)) { warning("Partial/semipartial correlations calculated based on robust", " t-statistics.\n See summ.lm documentation for cautions on", " \ninterpreting partial and semipartial correlations alongside", " robust standard errors.") } list(partial_corrs = partial_corrs, semipart_corrs = semipart_corrs) } dep_checks <- function(dots) { scale <- transform.response <- robust <- odds.ratio <- NULL if ("standardize" %in% names(dots)) { warn_wrap("The standardize argument is deprecated. Please use 'scale' instead.", call. = FALSE) scale <- dots$standardize } if ("standardize.response" %in% names(dots)) { warn_wrap("The standardize.response argument is deprecated. Please use 'transform.response' instead.", call. = FALSE) transform.response <- dots$standardize.response } if ("scale.response" %in% names(dots)) { warn_wrap("The scale.response argument is deprecated. Please use 'transform.response' instead.", call. = FALSE) transform.response <- dots$scale.response } if ("center.response" %in% names(dots)) { warn_wrap("The center.response argument is deprecated. Please use 'transform.response' instead.", call. = FALSE) transform.response <- dots$center.response } if ("robust.type" %in% names(dots)) { warn_wrap("The robust.type argument is deprecated. Please specify the type as the value for the 'robust' argument instead.", call. = FALSE) robust <- dots$robust.type } if ("odds.ratio" %in% names(dots)) { warn_wrap("The odds.ratio argument is deprecated. Use 'exp' instead.", call. = FALSE) exp <- dots$odds.ratio } if ("model.check" %in% names(dots)) { warn_wrap("The model.check argument is deprecated and has been removed.", call. = FALSE) } if ("stars" %in% names(dots)) { warn_wrap("The 'stars' argument is deprecated and has been removed.") } list(scale = scale, transform.response = transform.response, robust = robust, exp = odds.ratio) } scale_statement <- function(scale, center, transform.response, n.sd) { part_1 <- "Continuous " part_2 <- ifelse(transform.response, "variables", "predictors") part_3 <- " are mean-centered" part_4 <- if (scale) { " and scaled by " } else { NULL } part_5 <- if (scale) { paste(n.sd, "s.d") } else { NULL } if (scale == FALSE & center == FALSE) { return(NULL) } else { paste0(part_1, part_2, part_3, part_4, part_5, ".") } } vif <- function(mod, vcov = NULL, mod.matrix = NULL, ...) { if (any(is.na(coef(mod)))) { stop_wrap("VIFs cannot be calculated because there are aliased coefficients in the model.") } if (is.null(vcov)) { v <- vcov(mod) } else {v <- vcov} assign <- if (is.null(mod.matrix)) { attr(model.matrix(mod), "assign") } else { attr(mod.matrix, "assign") } if ("(Intercept)" %in% names(coefficients(mod))) { v <- v[-1, -1] assign <- assign[-1] } else { warning("No intercept: VIFs may not be sensible.") } terms <- labels(terms(mod)) n.terms <- length(terms) if (n.terms < 2) { stop_wrap("VIFS cannot be calculated because the model contains fewer than 2 terms.") } R <- cov2cor(v) detR <- det(R) result <- matrix(0, n.terms, 3) rownames(result) <- terms colnames(result) <- c("GVIF", "Df", "GVIF^(1/(2*Df))") for (term in 1:n.terms) { subs <- which(assign == term) result[term, 1] <- det(as.matrix(R[subs, subs])) * det(as.matrix(R[-subs, -subs])) / detR result[term, 2] <- length(subs) } if (all(result[, 2] == 1)) { result <- result[, 1] } else { result[, 3] <- result[, 1]^(1/(2 * result[, 2])) expand_fun <- function(x) { if ("(Intercept)" %in% names(coefficients(mod))) { rep(x, times = table(attr(model.matrix(mod), "assign")[-1])) } else { rep(x, times = table(attr(model.matrix(mod), "assign"))) } } result <- apply(result, MARGIN = 2, FUN = expand_fun) } return(result) } print_mod_info <- function(missing, n, dv, type) { if (is.null(missing) || missing == 0) { cat(underline("MODEL INFO:"), "\n", italic("Observations:"), " ", n, "\n", italic("Dependent Variable:"), " ", dv, "\n", sep = "") } else { cat(underline("MODEL INFO:"), "\n", italic("Observations:"), " ", n, " (", missing, " missing obs. deleted)", "\n", italic("Dependent Variable:"), " ", dv, "\n", sep = "") } cat(italic("Type:"), type, "\n\n") } mod_info_list <- function(missing, n, dv, type) { out <- list(dv = dv, n = n, type = type) if (!is.null(missing) && missing != 0) { out$missing <- missing } return(out) } print_mod_fit <- function(stats) { cat(underline("MODEL FIT:"), "\n", sep = "") cat(stats, "\n\n") } print_se_info <- function(robust, use_cluster, manual = NULL, vcov = NULL, ...) { if (identical(FALSE, robust) & is.null(vcov)) { cat(italic("Standard errors:", ifelse(is.null(manual), no = manual, yes = "MLE")), "\n", sep = "") } else if (is.null(vcov)) { if (robust == TRUE) {robust <- "HC3"} cat(italic("Standard errors:"), sep = "") if (use_cluster == FALSE) { cat(" Robust, ", italic("type = "), robust, "\n", sep = "") } else if (use_cluster == TRUE) { cat(" Cluster-robust, ", italic("type = "), robust, "\n", sep = "") } } else { "User-specified" } } get_se_info <- function(robust, use_cluster, manual = NULL, vcov = NULL, ...) { if (identical(FALSE, robust) & is.null(vcov)) { ifelse(is.null(manual), no = manual, yes = "MLE") } else if (is.null(vcov)) { if (robust == TRUE) {robust <- "HC3"} if (use_cluster == FALSE) { paste0("Robust, type = ", robust) } else if (use_cluster == TRUE) { paste0("Cluster-robust, type = ", robust) } } else { "User-specified" } } make_ci_labs <- function(ci.width) { alpha <- (1 - ci.width) / 2 lci_lab <- 0 + alpha lci_lab <- paste(round(lci_lab * 100, 1), "%", sep = "") uci_lab <- 1 - alpha uci_lab <- paste(round(uci_lab * 100, 1), "%", sep = "") list(lci = lci_lab, uci = uci_lab) } format_percentiles <- function(probs, digits) { paste0(format(100 * probs, trim = TRUE, scientific = FALSE, digits = digits), "%") } confint_linear <- function (object, parm, level = 0.95, cov = NULL, df.residual = NULL, ...) { cf <- coef(object) pnames <- names(cf) if (missing(parm)) parm <- pnames else if (is.numeric(parm)) parm <- pnames[parm] a <- (1 - level)/2 a <- c(a, 1 - a) pct <- format_percentiles(a, 3) if (is.null(df.residual)) { fac <- qnorm(a) } else { fac <- qt(a, df.residual) } ci <- array(NA, dim = c(length(parm), 2L), dimnames = list(parm, pct)) if (is.null(vcov)) { ses <- sqrt(diag(vcov(object)))[parm] } else { ses <- sqrt(diag(cov))[parm] } ci[] <- cf[parm] + ses %o% fac ci } mod_rank <- function(model) { preds <- length(attr(terms(model), "order")) int <- attr(terms(model), "intercept") return(preds + int) } to_kable <- function(t, html = !knitr::is_latex_output(), caption = NULL, cols = NULL, footnote = NULL, row.names = FALSE, col.names = NA, escape = knitr::is_latex_output(), format = NULL) { if (row.names == TRUE) { numerics <- names(t) %nin% "" align <- ifelse(numerics, yes = "r", no = "l") } else if (ncol(t) == 2) { align <- c("l", "r") } else { align <- NULL } t %<>% knitr::kable(format = format, row.names = row.names, col.names = col.names, escape = escape, booktabs = TRUE, align = align) if (length(caption) > 0) { head <- cols names(head) <- caption t %<>% kableExtra::add_header_above(header = head) } t %<>% kableExtra::kable_styling( bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = FALSE, position = "center", latex_options = c("hold_position", "striped") ) if (html == TRUE) { t %<>% kableExtra::column_spec(1, bold = TRUE) } if (length(footnote) > 0) { t %<>% kableExtra::footnote(general = footnote, general_title = "", threeparttable = TRUE) } return(t) } kableExtra_latex_deps <- list( list(name = "booktabs"), list(name = "longtable"), list(name = "array"), list(name = "multirow"), list(name = "xcolor", options = "table"), list(name = "wrapfig"), list(name = "float"), list(name = "colortbl"), list(name = "pdflscape"), list(name = "tabu"), list(name = "threeparttable"), list(name = "threeparttablex"), list(name = "ulem", options = "ulem"), list(name = "makecell") ) escape_stars <- function(t) { if (any(colnames(t) == "")) { t[,which(colnames(t) == "")] <- gsub("^\\*$", "\\\\*", t[,which(colnames(t) =="")]) } return(t) }
Qstat.reg.sb = function(DATA1, DATA2, vecA, Psize, gamma, Bsize, sigLev) { Tsize = nrow(DATA1) Nsize = Tsize - Psize vecQ.BP = matrix(0,Psize,1) vecQ.LB = matrix(0,Psize,1) vecCRQ = crossqreg.max(DATA1, DATA2, vecA, Psize) for (k in 1:Psize){ RES = Qstat(vecCRQ[1:k], Tsize) vecQ.BP[k] = RES$Q.BP vecQ.LB[k] = RES$Q.LB } matD1 = DATA1[(Psize+1):Tsize,] matCRQ = matrix(0,Bsize,Psize) matQ.BP = matrix(0,Bsize,Psize) matQ.LB = matrix(0,Bsize,Psize) for (b in 1:Bsize){ vecI = sb.index(Nsize, gamma) matD1.SB = matD1[vecI,] for (k in 1:Psize){ matD2 = DATA2[(Psize-k+1):(Tsize-k),] matD2.SB = matD2[vecI,] matCRQ[b,k] = crossqreg(matD1.SB, matD2.SB, vecA, k) } vecTest = matCRQ[b,] - vecCRQ for (k in 1:Psize){ Res1 = Qstat(vecTest[1:k], Tsize) matQ.BP[b,k] = Res1$Q.BP matQ.LB[b,k] = Res1$Q.LB } } vecCV.BP = matrix(0, Psize, 1) vecCV.LB = matrix(0, Psize, 1) for (k in 1:Psize){ vecCV.BP[k] = quantile(matQ.BP[,k], probs = (1 - sigLev) ) vecCV.LB[k] = quantile(matQ.LB[,k], probs = (1 - sigLev) ) } list(vecQ.BP = vecQ.BP, vecCV.BP=vecCV.BP, vecQ.LB=vecQ.LB, vecCV.LB=vecCV.LB) }
plots <- function(..., n_rows = NULL, n_columns = NULL, guides = NULL, tags = FALSE, tag_prefix = NULL, tag_suffix = NULL, tag_sep = NULL, title = NULL, subtitle = NULL, caption = NULL, theme = NULL) { insight::check_if_installed("patchwork") if (!is.null(tags)) { if (length(tags) == 1) { if (isTRUE(tags)) { tags <- "A" } else if (isFALSE(tags) || is.na(tags)) { tags <- NULL } else if (!tags %in% c("1", "A", "a", "I", "i")) { tags <- list(tags) } } else { tags <- list(tags) } } pw <- patchwork::wrap_plots(..., nrow = n_rows, ncol = n_columns, guides = guides) + patchwork::plot_annotation( tag_levels = tags, tag_prefix = tag_prefix, tag_suffix = tag_suffix, tag_sep = tag_sep, title = title, subtitle = subtitle, caption = caption, theme = theme ) return(pw) }
"model.comp.bayes.parobs" <- function(object, type="lpml", verbose=FALSE, ncores=NULL) { nkeep <- object$mcmc$nkeep Sigmahat <- apply(object$mcmc.draws$Sigma, c(1,2), mean) Omegahat <- apply(object$mcmc.draws$Omega, c(1,2), mean) thetahat <- rowMeans(object$mcmc.draws$theta) if (!is.null(ncores)) { ncores_ <- parallel::detectCores() if (ncores > ncores_) { stop(paste0("The number of cores must not exceed ", ncores_)) } } else { ncores <- min(2, parallel::detectCores()) } if (is.null(object$group)) { second <- numeric(1) } else { second <- object$group } if (type == "dic") { gof <- .Call(`_metapack_dic_parcov`, as.matrix(object$Outcome), as.matrix(object$XCovariate), as.matrix(object$WCovariate), as.vector(object$Npt), as.array(object$mcmc.draws$Sigma), as.array(object$mcmc.draws$Omega), as.matrix(object$mcmc.draws$theta), as.double(thetahat), as.matrix(Sigmahat), as.matrix(Omegahat), as.integer(object$fmodel), as.integer(nkeep), as.logical(verbose), as.logical(!is.null(object$group)), as.integer(second), as.integer(ncores)) } else if (type == "lpml") { gof <- .Call(`_metapack_lpml_parcov`, as.matrix(object$Outcome), as.matrix(object$XCovariate), as.matrix(object$WCovariate), as.vector(object$Npt), as.array(object$mcmc.draws$Sigma), as.array(object$mcmc.draws$Omega), as.matrix(object$mcmc.draws$theta), as.double(thetahat), as.matrix(Sigmahat), as.matrix(Omegahat), as.integer(object$fmodel), as.integer(nkeep), as.logical(verbose), as.logical(!is.null(object$group)), as.integer(second), as.integer(ncores)) } else if (type == "pearson") { gof <- .Call(`_metapack_pearson_parcov`, as.array(object$mcmc.draws$resid), as.vector(object$Npt), as.array(object$mcmc.draws$Sigma), as.integer(object$fmodel), as.integer(nkeep), as.logical(verbose)) } class(gof) <- "gofparobs" gof }
copy_filt <- function(abund,threshold){ if(is.null(dim(abund)) == FALSE){ sapply(1:ncol(abund), function(colnum){temp = abund[,colnum] if(threshold == round(threshold)){ rownums = which(temp < threshold) }else{ rownums = which(temp < sum(temp)*threshold) } abund[rownums, colnum] <<- 0}) }else{ if(threshold == round(threshold)){ abund[abund < threshold] <- 0 }else{ abund[abund < sum(abund)*threshold] <- 0 } } return(abund) }
shiftid <- function (e_ij1, e_ij2, e_i1, e_i2, time1, time2, industry.names = NULL, shift.method = "Dunn", gerfin.shifts = "mean", print.results = TRUE, plot.results = FALSE, plot.colours = NULL, plot.title = NULL, plot.portfolio = FALSE, ...) { no_years <- time2-time1 industries <- length(e_ij1) if (is.null(industry.names)) { industry.names <- as.character(1:industries) } e_j1 <- sum(e_ij1) e_j2 <- sum(e_ij2[,ncol(e_ij2)]) e1 <- sum(e_i1) e2 <- sum(e_i2[,ncol(e_i2)]) growth <- shift.growth(e_ij1 = e_ij1, e_ij2 = e_ij2, e_i1 = e_i1, e_i2 = e_i2, industry.names = industry.names) shift_all <- shiftd (e_ij1, e_ij2, e_i1, e_i2, time1, time2, shift.method = shift.method, gerfin.shifts = gerfin.shifts, print.results = FALSE) components.industry <- matrix(ncol = industries, nrow = nrow(shift_all$components)) i <- 0 for (i in 1:industries) { shift_industry <- shiftd ((e_ij1[i]), (e_ij2[i,]), (e_i1[i]), (e_i2[i,]), time1, time2, shift.method = shift.method, gerfin.shifts = gerfin.shifts, print.results = FALSE) components.industry[,i] <- shift_industry$components[,1] } colnames(components.industry) <- industry.names rownames(components.industry) <- rownames(shift_industry$components) components.industry <- components.industry[rownames(components.industry) != "Industrial mix",] if (print.results == TRUE) { cat ("\n") cat ("Dynamic Shift-Share Analysis", "\n") cat ("Method:", shift.method, "\n") cat ("\n") cat ("Shift-share components", "\n") print(as.data.frame(components.industry)) cat ("\n") cat ("Calculation for", industries, "industries", "\n") cat ("Regional employment at time t: ", e_j1, ", at time t+1: ", e_j2, " (", growth(e_j1, e_j2, growth.type = "abs"), " / ", growth(e_j1, e_j2, growth.type = "rate", rate.perc = TRUE), " %)", sep="", "\n") cat ("National employment at time t: ", e1, ", at time t+1: ", e2, " (", growth(e1, e2, growth.type = "abs"), " / ", growth(e1, e2, growth.type = "rate", rate.perc = TRUE), " %)", sep="", "\n") cat ("\n") } if (plot.results == TRUE) { if (is.null(plot.colours)) { plot.colours <- sample(colours(), ncol(components.industry)) } if (is.null(plot.title)) { plot.title <- "Shift-share analysis" } shiftplot <- barplot (components.industry, names.arg = NULL, col = plot.colours, legend = NULL, main = plot.title, beside = TRUE) legend("topright", legend = rownames(components.industry), fill = plot.colours, cex = 0.5) text(shiftplot, components.industry/2, labels = round(components.industry, 2), cex = 0.8) } if (plot.portfolio == TRUE) { portfolio (e_ij1, e_ij2, e_i1, e_i2, industry.names = industry.names, ...) } results <- list (components = shift_all$components, components.industry = components.industry, growth = growth, method = shift.method) invisible(results) }
do.findmain <- function ( ramclustObj = NULL, cmpd = NULL, mode = "positive", mzabs.error = 0.005, ppm.error = 10, ads = NULL, nls = NULL, scoring = "auto", plot.findmain = TRUE, writeMat = TRUE, writeMS = TRUE, use.z = TRUE) { if(is.null(ramclustObj)) { stop("must supply ramclustObj as input. i.e. ramclustObj = RC", '\n') } score.options <- c("auto", "imss", "ramclustr") if (!any(scoring == score.options)) { stop("scoring must be set to one of 'auto', 'imss', or 'ramclustr' ") } if (use.z & any(names(ramclustObj) == "fm")) { use.mass <- "fm" } else { use.mass <- "fmz" } if (is.null(ads)) { if (grepl("p", mode)) { ads <- c("[M+H]+", "[M+Na]+", "[M+K]+", "[M+NH4]+", "[2M+H]+", "[2M+Na]+", "[2M+K]+", "[2M+NH4]+", "[3M+H]+", "[3M+Na]+", "[3M+K]+", "[3M+NH4]+") } if (grepl("n", mode)) { ads <- c("[M-H]-", "[M+Na-2H]-", "[M+K-2H]-", "[M+CH2O2-H]-", "[2M-H]-", "[2M+Na-2H]-", "[2M+K-2H]-", "[2M+CH2O2- H]-", "[3M-H]-", "[3M+Na-2H]-", "[3M+K-2H]-", "[3M+CH2O2- H]-") } if (is.null(ads)) { stop("please define adducts using 'ads' or set mode to either'positive' or 'negative'") } } if (is.null(nls)) { if (grepl("p", mode)) { nls <- c("[M+H-COCH2]+", "[M+H-C2H3NO]+", "[M+H-H2O]+", "[M+H-NH3]+", "[M+H-CO2]+", "[M+H-NH3-CO2]+", "[M+H-NH3-HCOOH]+", "[M+H-NH3-H2O]+", "[M+H-NH3-COCH2]+", "[M+H-S]+", "[M+H-S-NH3-HCOOH]+", "[M+H-H4O2]+", "[M+H-CH2]+", "[M+H-CH4O3]+", "[M+H-O]+", "[M+H-C2H2]+", "[M+H-C2H4]+", "[M+H-CO]+", "[M+H-C3H6]+", "[M+H-C2H4O]+", "[M+H-C4H6]+", "[M+H-C3H4O]+", "[M+H-C4H8]+", "[M+H-C5H8]+", "[M+H-C4H6O]+", "[M+H-C5H10]+", "[M+H-C6H12]+", "[M+H-C4H8O2]+", "[M+H-H2O-HCOOH]+", "[M+H-CH4]+", "[M+H-CH2O]+", "[M+H-C2H6]+", "[M+H-CH3OH]+", "[M+H-C3H4]+", "[M+H-C3H6O]+", "[M+H-CO2-C3H6]+", "[M+H-SO3]+", "[M+H-SO3-H2O]+", "[M+H-SO3-H2O-NH3]+", "[M+H-NH3-C3H4]+", "[M+H-H2O-CO2]+", "[M+H-H2O-H2O-C2H4O]+", "[M+H-NH3-CO-CO]+", "[M+H-NH3-CO-C2OH2]+", "[M+H-C8H6O]+", "[M+H-C8H6O-NH3]+", "[M+H-C8H6O-H2O]+", "[M+H-C2H2O2]+", "[M+H-C2H4O2]+", "[M+H-C5H8O]+", "[M+H-NH3-CO2-CH2O]+", "[M+H-N2H6-CO2-H2O]+", "[M+H-NH3-CO2-C3H4O]+", "[M+H-NH3-CO2-C5H8]+", "[M+H-C2O4H4]+", "[M+H-C2H4-CO2]+", "[M+H-C2H4-HCOOH]+", "[M+H-NH3-H4O2]+", "[M+H-H2O-C2H2O2]+", "[M+H-COCH2-C4H8]+", "[M+H-NH3-NH3-C3H4]+", "[M+H-C2H4O2-CH3OH]+", "[M+H-C3H6O-CH3OH]+", "[M+H-NH3-CO-COCH2-C4H6O]+", "[M+H-C4H6-H2O]+", "[M+H-C4H6-C2H4]+", "[M+H-C4H6-NH3-H2O]+", "[M+H-C4H6-COCH2]+", "[M+H-C8H12O]+", "[M+H-C3H4O-C4H6]+", "[M+H-C3H4O-C4H8O2]+", "[M+H-C4H8-C4H6]+", "[M+H-NH3-HCOOH-CH3OH]+", "[M+H-NH3-C2H6]+", "[M+H-NH3-C8H6O-CH2]+", "[M+H-NH3-C3H4-COCH2]+", "[M+H-C3H9N]+", "[M+H-C3H9N-C2H4O2]+", "[M+H-C6H10O7]+", "[M+H-C6H10O7+H2O]+", "[M+H-C6H10O7-H4O2]+", "[M+H-C6H12O6]+", "[M+H-C6H12O6+H2O]+", "[M+H-C6H12O6-H2O]+", "[M+H-C5H10O5]+", "[M+H- C5H10O5+H2O]+", "[M+H- C5H10O5-H2O]+", "[M+H-C2H8NO4P]+", "[M+H-H6O3-CO]+", "[M+H-C6H13NO2]+", "[M+H-C5H11NO2]+", "[M+H-CH3S]+", "[M+H-C8H8O2]+", "[M+H-C12H22O11]+", "[M+H-C12H24O12]+", "[M+H-C12H20O10]+") } if (grepl("n", mode)) { nls <- c("[M-H-NH3]-", "[M-H-H2O]-", "[M-H-COCH2]-", "[M-H-CO2]-", "[M-H-NH3-CO2]-", "[M-H-NH3-HCOOH]-", "[M-H-NH3-H2O]-", "[M-H-NH3-C2OH2]-", "[M-H-S]-", "[M-H-S-NH3-HCOOH]-", "[M-H-H4O2]-", "[M-H-CH2]-", "[M-H-O]-", "[M-H-C2H2]-", "[M-H-C2H4]-", "[M-H-CO]-", "[M-H-C3H6]-", "[M-H-C2H4O]-", "[M-H-C4H6]-", "[M-H-C3H4O]-", "[M-H-C4H8]-", "[M-H-C5H8]-", "[M-H-C4H6O]-", "[M-H-C5H10]-", "[M-H-C6H12]-", "[M-H-C4H8O2]-", "[M-H-H2O-HCOOH]-", "[M-H-CH4]-", "[M-H-CH2O]-", "[M-H-C2H6]-", "[M-H-CH3OH]-", "[M-H-C3H4]-", "[M-H-C3H6O]-", "[M-H-CO2-C3H6]-", "[M-H-SO3]-", "[M-H-SO3-H2O]-", "[M-H-SO3-H2O-NH3]-", "[M-H-NH3-C3H4]-", "[M-H-H2O-CO2]-", "[M-H-H2O-H2O-C2H4O]-", "[M-H-NH3-CO-CO]-", "[M-H-NH3-CO-COCH2]-", "[M-H-C8H6O]-", "[M-H-C8H6O-NH3]-", "[M-H-C8H6O-H2O]-", "[M-H-C2H2O2]-", "[M-H-C2H4O2]-", "[M-H-C5H8O]-", "[M-H-NH3-CO2-CH2O]-", "[M-H-NH3-CO2-NH3-H2O]-", "[M-H-NH3-CO2-C3H4O]-", "[M-H-NH3-CO2-C5H8]-", "[M-H-HCOOH-HCOOH]-", "[M-H-C2H4-CO2]-", "[M-H-C2H4-HCOOH]-", "[M-H-NH3-H2O-H2O]-", "[M-H-H2O-C2H2O2]-", "[M-H-C2OH2-C4H8]-", "[M-H-NH3-NH3-C3H4]-", "[M-H-C2H4O2-CH3OH]-", "[M-H-C3H6O-CH3OH]-", "[M-H-NH3-CO-COCH2-C4H6O]-", "[M-H-C4H6-H2O]-", "[M-H-C4H6-C2H4]-", "[M-H-C4H6-NH3-H2O]-", "[M-H-C4H6-COCH2]-", "[M-H-C4H6-C4H6O]-", "[M-H-C3H4O-C4H6]-", "[M-H-C3H4O-C4H8O2]-", "[M-H-C4H8-C4H6]-", "[M-H-NH3-HCOOH-CH3OH]-", "[M-H-NH3-C2H6]-", "[M-H-NH3-C8H6O-CH2]-", "[M-H-NH3-C3H4-COCH2]-", "[M-H-C3H9N]-", "[M-H-C3H9N-C2H4O2]-", "[M-H-C6H10O7]-", "[M-H-C6H10O7+H2O]-", "[M-H-C6H10O7-H4O2]-", "[M-H-C6H12O6]-", "[M-H-C6H12O6+H2O]-", "[M-H-C6H12O6-H2O]-", "[M-H-C5H10O5]-", "[M-H-C5H10O5+H2O]-", "[M-H-C5H10O5-H2O]-", "[M-H-C2H8NO4P]-", "[M-H-CH6O4]-", "[M-H-C6H13NO2]-", "[M-H-C5H11NO2]-", "[M-H-CH3S]-", "[M-H-C8H8O2]-", "[M-H-C12H22O11]-", "[M-H-C12H24O12]-", "[M-H-C12H20O10]-") } if (is.null(nls)) { stop("please define neutral losses using 'nls' or set mode to either'positive' or 'negative'") } } M.findmain <- rep(NA, max(ramclustObj$featclus)) M.ppm.findmain <- rep(NA, max(ramclustObj$featclus)) M.ann.findmain <- as.list(rep(NA, max(ramclustObj$featclus))) M.nann.findmain <- rep(NA, max(ramclustObj$featclus)) M.space.findmain <- rep(NA, max(ramclustObj$featclus)) M.ramclustr <- rep(NA, max(ramclustObj$featclus)) M.ppm.ramclustr <- rep(NA, max(ramclustObj$featclus)) M.rank.ramclustr <- rep(NA, max(ramclustObj$featclus)) M.ann.ramclustr <- as.list(rep(NA, max(ramclustObj$featclus))) M.nann.ramclustr <- rep(NA, max(ramclustObj$featclus)) M.space.ramclustr <- rep(NA, max(ramclustObj$featclus)) if (is.null(cmpd)) { cmpd <- (1:max(ramclustObj$featclus)) } for (cl in cmpd) { s <- data.frame(mz = ramclustObj[[use.mass]][which(ramclustObj$featclus == cl)], int = ramclustObj$msint[which(ramclustObj$featclus == cl)]) s <- s[order(s$mz),] out <- InterpretMSSpectrum::findMAIN( s, rules = c(ads), adducthyp = ads[grep("[M",ads, fixed = TRUE)], ionmode = mode, mzabs = mzabs.error, ppm = ppm.error ) summarytable <- summary(out) M.findmain[cl] <- summarytable[1, "neutral_mass"] M.ppm.findmain[cl] <- summarytable[1, "medppm"] M.ann.findmain[[cl]] <- out[[1]] M.nann.findmain[cl] <- summarytable[1, "adducts_explained"] M.space.findmain[cl] <- summarytable[1, "total_score"]/summarytable[2, "total_score"] out <- InterpretMSSpectrum::findMAIN( s, adductmz = NULL, ionmode = mode, rules = c(ads, nls), adducthyp = ads[grep("[M", ads, fixed = TRUE)], ms2spec = NULL, mzabs = mzabs.error, ppm = ppm.error, mainpkthr = 0.1, collapseResults = FALSE) summarytable <- summary(out) summaryscores <- sapply(1:length(out), FUN = function(x) { is.adduct <- which(out[[x]][, "adduct"] %in% ads) det.adducts <- out[[x]][is.adduct, "adduct"] is.nl <- which(out[[x]][, "adduct"] %in% nls) keep <- is.adduct int <- sum(out[[x]][is.adduct, "int"]) if (length(is.nl) > 0) { int <- sum(int, sum(0.5 * (out[[x]][is.nl, "int"]))) keep <- c(keep, is.nl) } int <- int/sum(out[[x]][, "int"]) * length(is.adduct) if (any(grepl("[M+H-NH3]+", det.adducts)) & length(det.adducts == 1)) { int <- int/2 } mzerr <- (1/summarytable[x, "medppm"])^0.5 return(int * mzerr) }) best <- which.max(summaryscores) if (length(best) == 0) { best <- 1 } M.space.ramclustr[cl] <- { sort(summaryscores, decreasing = TRUE)[1]/sort(summaryscores, decreasing = TRUE)[2] } if(is.na(M.space.ramclustr[cl])) {M.space.ramclustr[cl] <- 1} M.ramclustr[cl] <- summarytable[best, "neutral_mass"] M.ppm.ramclustr[cl] <- summarytable[best, "medppm"] M.rank.ramclustr[cl] <- best M.ann.ramclustr[[cl]] <- out[[best]] M.nann.ramclustr[cl] <- summarytable[best, "adducts_explained"] if (10 * round(cl/10, digits = 0) == cl) { cat(cl, "of", max(ramclustObj$featclus), "\n") } } ramclustObj$M.ramclustr <- M.ramclustr ramclustObj$M.ppm.ramclustr <- M.ppm.ramclustr ramclustObj$M.rank.ramclustr <- M.rank.ramclustr ramclustObj$M.ann.ramclustr <- M.ann.ramclustr ramclustObj$M.nann.ramclustr <- M.nann.ramclustr ramclustObj$M.space.ramclustr <- M.space.ramclustr ramclustObj$M.findmain <- M.findmain ramclustObj$M.ppm.findmain <- M.ppm.findmain ramclustObj$M.ppm.findmain[which(is.na(ramclustObj$M.ppm.findmain))] <- { ppm.error * 2} ramclustObj$M.ann.findmain <- M.ann.findmain ramclustObj$M.nann.findmain <- M.nann.findmain ramclustObj$use.findmain <- rep(FALSE, length(M.ppm.findmain)) ramclustObj$M.space.findmain <- M.space.findmain if (scoring == "ramclustr") { ramclustObj$use.findmain <- rep(FALSE, length(ramclustObj$use.findmain)) } if (scoring == "imss") { ramclustObj$use.findmain <- rep(TRUE, length(ramclustObj$use.findmain)) } if (scoring == "auto") { resolve <- which(abs(ramclustObj$M.ramclustr - ramclustObj$M.findmain) > (2 * mzabs.error)) for (i in resolve) { ppm.rat <- (ramclustObj$M.ppm.ramclustr[i]^0.5/ramclustObj$M.ppm.findmain[i]^0.5) if(is.na(ppm.rat)) {ppm.rat <-1} ppm.rat <- 1/ppm.rat sel.rat <- (ramclustObj$M.space.ramclustr[i]/ramclustObj$M.space.findmain[i]) nann.rat <- (ramclustObj$M.nann.ramclustr[i]/ramclustObj$M.nann.findmain[i]) master.rat <- ppm.rat * sel.rat * nann.rat if (master.rat < 1) { ramclustObj$use.findmain[i] <- TRUE } } } ramclustObj$M.ann <- ramclustObj$M.ann.ramclustr ramclustObj$precursor.mz <- rep(NA, length(ramclustObj$M.ann)) ramclustObj$precursor.type <- rep(NA, length(ramclustObj$M.ann)) ramclustObj$M <- ramclustObj$M.ramclustr change <- which(ramclustObj$use.findmain) ramclustObj$M[change] <- ramclustObj$M.findmain[change] if(length(change) > 0) { for (i in change) { ramclustObj$M.ann[[i]] <- ramclustObj$M.ann.findmain[[i]] } } for(i in 1:length(ramclustObj$precursor.mz)) { if(is.vector(ramclustObj$M.ann[[i]])) next for(j in 1:length(ads)) { m <- which(ramclustObj$M.ann[[i]]$label == ads[[j]]) if(length(m) == 0) next ramclustObj$precursor.mz[i] <- ramclustObj$M.ann[[i]][m, "mz"] ramclustObj$precursor.type[i] <- ads[j] } } if (plot.findmain) { cat("plotting findmain annotation results", "\n") if(!dir.exists('spectra')) { dir.create('spectra') } pdf("spectra/findmainPlots.pdf", width = 15, height = 7) par(mfrow = c(1, 2)) par(xpd = TRUE) for (cl in cmpd) { InterpretMSSpectrum::PlotSpec(x = ramclustObj$M.ann.ramclustr[[cl]], txt = ramclustObj$M.ann.ramclustr[[cl]][, c("mz", "adduct")], cutoff = 0, masslab = 0, ylim = c(0, 1.1 * max(ramclustObj$M.ann.ramclustr[[cl]][, 2])) ) title(main = list( paste( cl, ":", "M.ramclustr =", round(ramclustObj$M.ramclustr[cl], digits = 4), "( +/-", round(ramclustObj$M.ppm.ramclustr[cl], digits = 1), "ppm )"), font = if (ramclustObj$use.findmain[cl]) { 1 } else { 2 }, col = if (ramclustObj$use.findmain[cl]) { 1 } else { 2 }) ) InterpretMSSpectrum::PlotSpec(x = ramclustObj$M.ann.findmain[[cl]], txt = ramclustObj$M.ann.findmain[[cl]][, c("mz", "adduct")], cutoff = 0, masslab = 0, ylim = c(0, 1.1 * max(ramclustObj$M.ann.ramclustr[[cl]][, 2]))) title(main = list(paste(cl, ":", "M.findmain =", round(ramclustObj$M.findmain[cl], digits = 4), "( +/-", round(ramclustObj$M.ppm.findmain[cl], digits = 1), "ppm )"), font = if (ramclustObj$use.findmain[cl]) { 2 } else { 1 }, col = if (ramclustObj$use.findmain[cl]) { 2 } else { 1 })) } dev.off() } if (writeMat) { if (!dir.exists("spectra")) { dir.create("spectra") } dir.create("spectra/mat") for (cl in cmpd) { ms <- ramclustObj$M.ann[[cl]] prcr <- which(ms[, "adduct"] %in% ads) prcr <- prcr[which.max(ms[prcr, "int"])] prcmz <- ms[prcr, "mz"] prctype <- ms[prcr, "adduct"] out <- paste("NAME: ", ramclustObj$cmpd[cl], "\n", "RETENTIONTIME: ", round(ramclustObj$clrt[cl], digits = 2), "\n", "PRECURSORMZ: ", prcmz, "\n", "PRECURSORTYPE: ", prctype, "\n", "IONTYPE: ", mode, "\n", "SPECTRUMTYPE: Centroid", "\n", if ((!is.null(ramclustObj$msmsint))) { paste("COLLISIONENERGY: ", as.character(ramclustObj$ExpDes[[2]][which(row.names(ramclustObj$ExpDes[[2]]) == "CE2"), 1]), "\n", sep = "") }, "MSTYPE: ", "MS1", "\n", "Num Peaks: ", nrow(ms), "\n", sep = "") for (i in 1:nrow(ms)) { out <- paste(out, ms[i, 1], " ", ms[i, 2], "\n", sep = "") } if (!is.null(ramclustObj$msmsint)) { do <- which(ramclustObj$featclus == cl) if (length(do) > 0) { msms <- cbind(mz = ramclustObj[[use.mass]][do], int = ramclustObj$msmsint[do]) msms <- msms[which(msms[, "mz"] <= (prcmz + 3)), , drop = FALSE] msms <- msms[order(msms[, "int"], decreasing = TRUE), , drop = FALSE] if (nrow(msms) > 0) { out <- paste(out, "MSTYPE:", "MS2", "\n", "Num Peaks: ", nrow(msms), "\n", sep = "") for (i in 1:nrow(msms)) { out <- paste(out, msms[i, 1], " ", msms[i, 2], "\n", sep = "") } } } } else { do <- which(ramclustObj$featclus == cl) if (length(do) > 0) { msms <- cbind(mz = ramclustObj[[use.mass]][do], int = ramclustObj$msint[do]) msms <- msms[which(msms[, "mz"] <= (prcmz + 3)), , drop = FALSE] msms <- msms[order(msms[, "int"], decreasing = TRUE), , drop = FALSE] if (nrow(msms) > 0) { out <- paste(out, "MSTYPE:", "MS2", "\n", "Num Peaks: ", nrow(msms), "\n", sep = "") for (i in 1:nrow(msms)) { out <- paste(out, msms[i, 1], " ", msms[i, 2], "\n", sep = "") } } } } write(out, file = paste0("spectra/mat/", ramclustObj$cmpd[cl], ".mat")) } } if (writeMS) { if (!dir.exists("spectra")) { dir.create("spectra") } dir.create("spectra/ms") for (cl in cmpd) { ms <- ramclustObj$M.ann[[cl]] m1ads <- ads[grep("[M", ads, fixed = TRUE)] if (length(m1ads) == 0) { next } else { prcr <- which(ms[, "adduct"] %in% m1ads) } prcr <- prcr[which.max(ms[prcr, "int"])] prcmz <- ms[prcr, "mz"] prctype <- ms[prcr, "adduct"] out <- paste(">compound ", ramclustObj$cmpd[cl], "\n", ">parentmass ", prcmz, "\n", ">ionization ", prctype, "\n", "\n", sep = "") ms <- ms[which((abs(ms[, "mz"] - prcmz) < 5.5) | (abs(prcmz - ms[, "mz"]) < 0.2)), ] out <- paste(out, ">ms1peaks", "\n", sep = "") for (i in 1:nrow(ms)) { out <- paste(out, ms[i, 1], " ", ms[i, 2], "\n", sep = "") } if (!is.null(ramclustObj$msmsint)) { do <- which(ramclustObj$featclus == cl) if (length(do) > 0) { msms <- cbind(mz = ramclustObj[[use.mass]][do], int = ramclustObj$msmsint[do]) msms <- msms[which(msms[, "mz"] <= (prcmz + 3)), , drop = FALSE] msms <- msms[order(msms[, "int"], decreasing = TRUE), , drop = FALSE] if (nrow(msms) > 0) { out <- paste(out, "\n", ">collision ", ramclustObj$ExpDes$instrument[which(dimnames(ramclustObj$ExpDes$instrument)[[1]] == "CE2"), 1], "\n", sep = "") for (i in 1:nrow(msms)) { out <- paste(out, msms[i, 1], " ", msms[i, 2], "\n", sep = "") } } } } else { do <- which(ramclustObj$featclus == cl) if (length(do) > 0) { msms <- cbind(mz = ramclustObj[[use.mass]][do], int = ramclustObj$msint[do]) msms <- msms[which(msms[, "mz"] <= (prcmz + 3)), , drop = FALSE] msms <- msms[order(msms[, "int"], decreasing = TRUE), , drop = FALSE] if (nrow(msms) > 0) { out <- paste(out, "\n", ">collision ", ramclustObj$ExpDes$instrument[which(dimnames(ramclustObj$ExpDes$instrument)[[1]] == "CE1"), 1], "\n", sep = "") for (i in 1:nrow(msms)) { out <- paste(out, msms[i, 1], " ", msms[i, 2], "\n", sep = "") } } } } write(out, file = paste0("spectra/ms/", ramclustObj$cmpd[cl], ".ms")) } } ramclustObj$history$do.findmain <- paste( " Molecular weight was inferred from in-source spectra (Broeckling 2016) using the do.findmain function, which calls the ", "interpretMSSpectrum package (Jaeger 2016). ", "Parameters for do.findmain were set to: ", "mode = ", mode, ", mzabs.error = ", mzabs.error, ", ppm.error = ", ppm.error, ", ads = ", paste(ads, collapse = " "), ", nls = ", paste(nls, collapse = " "), ", scoring = ", scoring, ", and use.z = ", use.z, ".", sep = "") cat("finished", "\n") return(ramclustObj) }
PrintToPDF <- function(Path, OutputName, ObjectList = NULL, Tables = FALSE, MaxPages = 500, Title = "Model Output", Width = 12, Height = 7, Paper = "USr", BackgroundColor = "transparent", ForegroundColor = "black") { if("grDevices" %chin% installed.packages()) { if(is.null(ObjectList) || is.null(Path) || !is.list(ObjectList)) { print("Nothing to print") stop() } if(!Tables) { grDevices::pdf(file = file.path(Path, paste0(OutputName,".pdf")), onefile = TRUE, title = Title, width = Width, height = Height, fonts = NULL, paper = Paper, bg = BackgroundColor, fg = ForegroundColor, compress = TRUE) for(i in seq_along(ObjectList)) for(j in seq_along(ObjectList[[i]])) multiplot(plotlist = list(ObjectList[[i]][[j]])) while(grDevices::dev.cur() > 1) grDevices::dev.off() } else { for(i in seq_along(ObjectList)) { grDevices::pdf(file = file.path(normalizePath(Path), paste0(OutputName,"_",i,".pdf")), onefile = TRUE, title = Title, width = Width, height = Height, fonts = NULL, paper = Paper, bg = BackgroundColor, fg = ForegroundColor, compress = TRUE) tryCatch({ for(j in seq_along(ObjectList[[i]])) { counter <- 1L repeat{ temp <- ObjectList[[i]][[j]][counter:(counter + 14L)] temp <- temp[!is.na(temp[[eval(names(temp)[1])]])] counter <- counter + 15L if(temp[,.N] < 15 || counter == MaxPages+1L) break print(gridExtra::grid.table(temp, rows = NULL)) grid::grid.newpage() } }}, error = function(x) NULL) while(grDevices::dev.cur() > 1) grDevices::dev.off() } } } else { warning("Need to install the package grDevices in order to run this function") } } DeleteFile <- function(File = NULL) { if(is.null(File)) { shell(paste0("del ", file.choose())) } else { shell(paste0("del ", File)) } } Logger <- function(x) { function(...) { warn <- err <- NULL res <- withCallingHandlers( tryCatch(x(...), error = function(e) { err <<- conditionMessage(e) NULL }), warning = function(w) { warn <<- append(warn, conditionMessage(w)) invokeRestart("muffleWarning") }) list(res, warn = warn, err = err) } } LB <- function(TimeAgg) { if(tolower(TimeAgg) %chin% c("hour","hours", "1min","1mins","1minute","1minutes", "5min","5mins","5minute","5minutes", "10min","10mins","10minute","10minutes", "15min","15mins","15minute","15minutes", "30min","30mins","30minute","30minutes", "day","days")) { return(1) } else if(tolower(TimeAgg) %chin% c("week","weeks")) { return(7) } else if(tolower(TimeAgg) %chin% c("month","months")) { return(30) } else if(tolower(TimeAgg) %chin% c("quarter","quarters")) { return(120) } else if(tolower(TimeAgg) %chin% c("years","year")) { return(365) } } BuildBinary <- function(Root = NULL) { x <- getwd() if(!is.null(Root)) { setwd(Root) devtools::install(pkg = "RemixAutoML", dependencies = FALSE) } else { setwd("C:/Users/Bizon/Documents/GitHub") devtools::build(pkg = "RemixAutoML") } setwd(x) } Install <- function(Root = NULL) { x <- getwd() if(!is.null(Root)) { setwd(Root) devtools::install(pkg = "RemixAutoML", dependencies = FALSE) } else { setwd("C:/Users/Bizon/Documents/GitHub") devtools::install(pkg = "RemixAutoML", dependencies = FALSE) } setwd(x) } UpdateDocs <- function(BuildVignette = FALSE, Root = NULL) { x <- getwd() if(!is.null(Root)) { setwd(Root) devtools::document() if(BuildVignette) devtools::build_manual() } else { setwd("C:/Users/Bizon/Documents/GitHub/RemixAutoML") devtools::document() if(BuildVignette) devtools::build_manual() } setwd(x) } ParseOptParse <- function(x) { if(!is.null(x)) { return(as.character(if(is.list(strsplit(x, ","))) unlist(strsplit(x, ",")) else x)) } else { return(x) } } ColTypes <- function(data) { CT <- c() for(Col in names(data)) CT <- c(CT, class(data[[Col]])[[1L]]) CT } LoadAssign <- function(FilePath) { load(FilePath, envir = .GlobalEnv) get(ls()[ls() != "FilePath"]) } ColNameFilter <- function(data, Types = 'all') { if(Types == 'all') return(names(data)) nam <- c() for(t in Types) { if(tolower(t) == 'numeric') { nam <- c(nam, NumericColNames(data)) } else if(tolower(t) == 'character') { nam <- c(nam, CharacterColNames(data)) } else if(tolower(t) == 'factor') { nam <- c(nam, FactorColNames(data)) } else if(tolower(t) == 'logical') { nam <- c(nam, LogicalColNames(data)) } } return(unique(nam)) } NumericColNames <- function(data) { x <- names(data)[which(sapply(data, is.numeric))] if(!identical(x, character(0))) return(x) else return(NULL) } CharacterColNames <- function(data) { x <- names(data)[which(sapply(data, is.character))] if(!identical(x, character(0))) return(x) else return(NULL) } FactorColNames <- function(data) { x <- names(data)[which(sapply(data, is.factor))] if(!identical(x, character(0))) return(x) else return(NULL) } LogicalColNames <- function(data) { x <- names(data)[which(sapply(data, is.logical))] if(!identical(x, character(0))) return(x) else return(NULL) }
library(testthat) test_that("it returns correct data", { r <- ptd_spc_options( value_field = "value_field", date_field = "date_field", facet_field = "facet_field", rebase = as.Date("2020-01-01"), fix_after_n_points = NULL, improvement_direction = "increase", target = 1, trajectory = "trajectory", screen_outliers = TRUE ) expect_equal(r$value_field, "value_field") expect_equal(r$date_field, "date_field") expect_equal(r$facet_field, "facet_field") expect_equal(r$rebase, as.Date("2020-01-01")) expect_equal(r$fix_after_n_points, NULL) expect_equal(r$improvement_direction, "increase") expect_equal(r$target, 1) expect_equal(r$trajectory, "trajectory") expect_equal(r$screen_outliers, TRUE) expect_s3_class(r, "ptd_spc_options") }) test_that("value_field can only be a scalar character", { expect_error(ptd_spc_options(1), "value_field argument must be a 'character' of length 1.") expect_error(ptd_spc_options(c("a", "b")), "value_field argument must be a 'character' of length 1.") }) test_that("date_field can only be a scalar character", { expect_error(ptd_spc_options("a", 1), "date_field argument must be a 'character' of length 1.") expect_error(ptd_spc_options("a", c("a", "b")), "date_field argument must be a 'character' of length 1.") }) test_that("facet_field is either null, or a scalar character", { ptd_spc_options("a", "b", facet_field = NULL) expect_error( ptd_spc_options("a", "b", facet_field = 1 ), "facet_field argument must be a 'character' of length 1." ) expect_error( ptd_spc_options("a", "b", facet_field = c("a", "b") ), "facet_field argument must be a 'character' of length 1." ) }) test_that("rebase is either null, a date, or a named list of dates", { ptd_spc_options("a", "b", rebase = NULL) ptd_spc_options("a", "b", rebase = as.Date("2020-01-01")) ptd_spc_options("a", "b", rebase = list("a" = as.Date("2020-01-01")), facet_field = "a") em <- "rebase argument must be a date vector, or a named list of date vectors." expect_error(ptd_spc_options("a", "b", rebase = 1), em) expect_error(ptd_spc_options("a", "b", rebase = c("a", "b")), em) expect_error(ptd_spc_options("a", "b", rebase = list("a" = as.Date("2020-01-01"), b = "a")), em) expect_error(ptd_spc_options("a", "b", rebase = list(as.Date("2020-01-01"))), em) }) test_that("rebase must be a date vector if facet_field is not set", { em <- "rebase must be a date vector if facet_field is not set" expect_error(ptd_spc_options("a", "b", rebase = list("a" = Sys.Date())), em) }) test_that("fix_after_n_points must be a single numeric that is greater than or equal to 12.", { expect_error( ptd_spc_options("a", "b", fix_after_n_points = "a"), "fix_after_n_points must be a single numeric that is greater than or equal to 12." ) expect_error( ptd_spc_options("a", "b", fix_after_n_points = c(15, 20)), "fix_after_n_points must be a single numeric that is greater than or equal to 12." ) expect_error( ptd_spc_options("a", "b", fix_after_n_points = 11), "fix_after_n_points must be a single numeric that is greater than or equal to 12." ) ptd_spc_options("a", "b", fix_after_n_points = 12) }) test_that("improvement_direction defaults to increase", { o <- ptd_spc_options("a", "b") expect_equal(o$improvement_direction, "increase") }) test_that("improvement_direction must be one of increase, neutral, or decrease", { ptd_spc_options("a", "b", improvement_direction = "increase") ptd_spc_options("a", "b", improvement_direction = "neutral") ptd_spc_options("a", "b", improvement_direction = "decrease") expect_error( ptd_spc_options("a", "b", improvement_direction = "a"), "'arg' should be one of \"increase\", \"neutral\", \"decrease\"" ) }) test_that("target is either null, a scalar numeric, or a named list of numerics", { ptd_spc_options("a", "b", target = NULL) ptd_spc_options("a", "b", target = 1) ptd_spc_options("a", "b", target = list("a" = 1)) em <- "target argument must be a single numeric, or a named list of numerics." expect_error(ptd_spc_options("a", "b", target = "a"), em, fixed = TRUE) expect_error(ptd_spc_options("a", "b", target = c(0, 1)), em, fixed = TRUE) }) test_that("trajectory is either null, or a scalar character", { ptd_spc_options("a", "b", trajectory = NULL) expect_error( ptd_spc_options("a", "b", trajectory = 1), "trajectory argument must be a 'character' of length 1." ) expect_error( ptd_spc_options("a", "b", trajectory = c("a", "b")), "trajectory argument must be a 'character' of length 1." ) }) test_that("screen_outliers must be a scalar logical", { ptd_spc_options("a", "b", screen_outliers = TRUE) ptd_spc_options("a", "b", screen_outliers = FALSE) em <- "screen_outliers must either `TRUE` or `FALSE`." expect_error( ptd_spc_options("a", "b", screen_outliers = c(TRUE, FALSE)), em ) expect_error( ptd_spc_options("a", "b", screen_outliers = "TRUE"), em ) }) test_that("printing output", { r <- ptd_spc_options("hello", "world") expect_output(print(r), "Plot the Dots SPC options:") expect_output(print(r), "================================") expect_output(print(r), "value_field:.*'hello'") expect_output(print(r), "date_field:.*'world'") expect_output(print(r), "facet_field:.*not set") expect_output(print(r), "rebase:.*not set") expect_output(print(r), "fix_after_n_points:.*not set") expect_output(print(r), "improvement_direction:.*not set") expect_output(print(r), "target:.*not set") expect_output(print(r), "trajectory:.*not set") expect_output(print(r), "screen_outliers:.*'TRUE'") expect_output(print(r), "--------------------------------") })
ergm.estimate<-function(init, model, statsmatrices, statsmatrices.obs=NULL, epsilon=1e-10, nr.maxit=1000, nr.reltol=sqrt(.Machine$double.eps), metric="lognormal", method="Nelder-Mead", calc.mcmc.se=TRUE, hessianflag=TRUE, verbose=FALSE, trace=6*verbose, dampening=FALSE, dampening.min.ess=100, dampening.level=0.1, steplen=1, steplen.point.exp=1, cov.type="normal", estimateonly=FALSE, ...) { estimateonly <- estimateonly & !calc.mcmc.se obsprocess <- !is.null(statsmatrices.obs) etamap <- model$etamap etamap.no <- deoffset.etamap(etamap, init) eta0 <- ergm.eta(init[!etamap$offsettheta], etamap.no) statsmatrices.orig <- statsmatrices statsmatrices.orig.obs <- statsmatrices.obs statsmean <- colMeans.mcmc.list(statsmatrices.orig) if(!is.null(statsmatrices.orig.obs)){ statsmatrices.obs <- lapply.mcmc.list(statsmatrices.orig.obs, .shift_scale_points, statsmean, steplen, steplen^steplen.point.exp) }else{ statsmatrices <- lapply.mcmc.list(statsmatrices.orig,sweep,2,(1-steplen)*statsmean,"-") } statsmatrix <- as.matrix(statsmatrices) if(obsprocess) statsmatrix.obs <- as.matrix(statsmatrices.obs) statsmatrix.orig <- as.matrix(statsmatrices.orig) if(obsprocess) statsmatrix.orig.obs <- as.matrix(statsmatrices.orig.obs) xsim <- compress_rows(as.logwmatrix(statsmatrix[,!etamap$offsetmap, drop=FALSE])) lrowweights(xsim) <- -log_sum_exp(lrowweights(xsim)) xsim.orig <- compress_rows(as.logwmatrix(statsmatrix.orig[,!etamap$offsetmap, drop=FALSE])) lrowweights(xsim.orig) <- -log_sum_exp(lrowweights(xsim.orig)) if(obsprocess){ xsim.obs <- compress_rows(as.logwmatrix(statsmatrix.obs[,!etamap$offsetmap, drop=FALSE])) lrowweights(xsim.obs) <- -log_sum_exp(lrowweights(xsim.obs)) xsim.orig.obs <- compress_rows(as.logwmatrix(statsmatrix.orig.obs[,!etamap$offsetmap, drop=FALSE])) lrowweights(xsim.orig.obs) <- -log_sum_exp(lrowweights(xsim.orig.obs)) }else{ xsim.obs <- NULL xsim.orig.obs <- NULL } if(cov.type=="robust"){ tmp <- covMcd(decompress_rows(xsim, target.nrows=nrow(statsmatrix))) av <- tmp$center V <- tmp$cov }else{ av <- lweighted.mean(xsim, lrowweights(xsim)) V <- lweighted.var(xsim, lrowweights(xsim)) } xobs <- -av if(obsprocess) { if(cov.type=="robust"){ tmp <- covMcd(decompress_rows(xsim.obs, target.nrows=nrow(statsmatrix.obs))) av.obs <- tmp$center V.obs <- tmp$cov }else{ av.obs <- lweighted.mean(xsim.obs, lrowweights(xsim.obs)) V.obs <- lweighted.var(xsim.obs, lrowweights(xsim.obs)) } xobs <- av.obs - av } varweight <- 0.5 if (verbose) { message("Using ", metric, " metric (see control.ergm function).") } if (obsprocess) { loglikelihoodfn <- switch(metric, Likelihood=llik.fun.obs.lognormal, lognormal=llik.fun.obs.lognormal, logtaylor=llik.fun.obs.lognormal, Median.Likelihood=llik.fun.obs.robust, EF.Likelihood=llik.fun.obs.lognormal, llik.fun.obs.IS) gradientfn <- switch(metric, Likelihood=llik.grad.obs.IS, lognormal=llik.grad.obs.IS, logtaylor=llik.grad.obs.IS, Median.Likelihood=llik.grad.obs.IS, EF.Likelihood=llik.grad.obs.IS, llik.grad.obs.IS) Hessianfn <- switch(metric, Likelihood=llik.hessian.obs.IS, lognormal=llik.hessian.obs.IS, logtaylor=llik.hessian.obs.IS, Median.Likelihood=llik.hessian.obs.IS, EF.Likelihood=llik.hessian.obs.IS, llik.hessian.obs.IS) } else { loglikelihoodfn <- switch(metric, Likelihood=llik.fun.lognormal, lognormal=llik.fun.lognormal, logtaylor=llik.fun.logtaylor, Median.Likelihood=llik.fun.median, EF.Likelihood=llik.fun.EF, llik.fun.IS) gradientfn <- switch(metric, Likelihood=llik.grad.IS, lognormal=llik.grad.IS, logtaylor=llik.grad.IS, Median.Likelihood=llik.grad.IS, EF.Likelihood=llik.grad.IS, llik.grad.IS) Hessianfn <- switch(metric, Likelihood=llik.hessian.IS, lognormal=llik.hessian.IS, logtaylor=llik.hessian.IS, Median.Likelihood=llik.hessian.IS, EF.Likelihood=llik.hessian.IS, llik.hessian.IS) } if (!is.curved(model) && (metric=="lognormal" || metric=="Likelihood") && all(model$etamap$mintheta==-Inf) && all(model$etamap$maxtheta==+Inf)) { if (obsprocess) { if (verbose) { message("Using log-normal approx with missing (no optim)") } Lout <- list(hessian = -as.matrix(nearPD(V-V.obs,posd.tol=0)$mat)) } else { if (verbose) { message("Using log-normal approx (no optim)") } Lout <- list(hessian = -V) } Lout$par <- try(eta0 - solve(Lout$hessian, xobs), silent=TRUE) if(inherits(Lout$par,"try-error")){ Lout$par <- try(eta0 - ginv(Lout$hessian) %*% xobs, silent=TRUE) } if(inherits(Lout$par,"try-error")){ if (obsprocess) { Lout <- list(hessian = -(as.matrix(nearPD(V-V.obs)$mat))) }else{ Lout <- list(hessian = -(as.matrix(nearPD(V)$mat))) } Lout$par <- eta0 - solve(Lout$hessian, xobs) } Lout$convergence <- 0 Lout$value <- 0.5*crossprod(xobs, Lout$par - eta0) hessianflag <- TRUE } else { guess <- init[!model$etamap$offsettheta] loglikelihoodfn.trust<-function(theta, ...){ if(anyNA(theta) || any(theta < model$etamap$mintheta[!model$etamap$offsettheta]) || any(theta > model$etamap$maxtheta[!model$etamap$offsettheta])) return(list(value=-Inf)) value<-loglikelihoodfn(theta, ...) grad<-gradientfn(theta, ...) hess<-Hessianfn(theta, ...) hess[upper.tri(hess)]<-t(hess)[upper.tri(hess)] list(value=value,gradient=as.vector(grad),hessian=hess) } if (verbose) { message("Optimizing loglikelihood") } Lout <- try(trust(objfun=loglikelihoodfn.trust, parinit=guess, rinit=1, rmax=100, parscale=rep(1,length(guess)), minimize=FALSE, xsim=xsim, xsim.obs=xsim.obs, varweight=varweight, dampening=dampening, dampening.min.ess=dampening.min.ess, dampening.level=dampening.level, eta0=eta0, etamap=etamap.no), silent=FALSE) Lout$par<-Lout$argument if(inherits(Lout,"try-error")) { message("MLE could not be found. Trying Nelder-Mead...") Lout <- try(optim(par=guess, fn=llik.fun.median, hessian=hessianflag, method="Nelder-Mead", control=list(trace=trace,fnscale=-1,maxit=100*nr.maxit, reltol=nr.reltol), xsim=xsim, xsim.obs=xsim.obs, varweight=varweight, dampening=dampening, dampening.min.ess=dampening.min.ess, dampening.level=dampening.level, eta0=eta0, etamap=etamap.no), silent=FALSE) if(inherits(Lout,"try-error")){ message(paste("No direct MLE exists!")) } if(Lout$convergence != 0 ){ message("Non-convergence after ", nr.maxit, " iterations.") } message("Nelder-Mead Log-likelihood ratio is ", Lout$value," ") } } theta <- init theta[!model$etamap$offsettheta] <- Lout$par names(theta) <- names(init) if (estimateonly) { return(structure(list(coefficients = setNames(theta, names(init)), MCMCtheta = init, samplesize = nrow(statsmatrix), loglikelihood = Lout$value, failure = FALSE), class = "ergm")) } else { gradienttheta <- llik.grad.IS(theta=Lout$par, xsim=xsim, xsim.obs=xsim.obs, varweight=varweight, eta0=eta0, etamap=etamap.no) gradient <- rep(NA, length=length(init)) gradient[!model$etamap$offsettheta] <- gradienttheta mc.se <- rep(NA, length=length(theta)) mc.cov <- matrix(NA, length(theta), length(theta)) covar <- NA if(!hessianflag || steplen!=1){ Lout$hessian <- Hessianfn(theta=Lout$par, xsim=xsim.orig, xsim.obs=xsim.orig.obs, varweight=varweight, eta0=eta0, etamap=etamap.no ) } covar <- matrix(NA, ncol=length(theta), nrow=length(theta)) covar[!model$etamap$offsettheta,!model$etamap$offsettheta ] <- ginv(-Lout$hessian) dimnames(covar) <- list(names(theta),names(theta)) He <- matrix(NA, ncol=length(theta), nrow=length(theta)) He[!model$etamap$offsettheta,!model$etamap$offsettheta ] <- Lout$hessian dimnames(He) <- list(names(theta),names(theta)) Lout$hessian <- He if(calc.mcmc.se){ if (verbose) message("Starting MCMC s.e. computation.") mcse.metric <- if ((metric == "lognormal" || metric == "Likelihood") && length(model$etamap$curved) == 0) "lognormal" else "IS" mc.cov <- ergm.MCMCse(model = model, theta = theta, init = init, statsmatrices = statsmatrices, statsmatrices.obs = statsmatrices.obs, H = V, H.obs = V.obs, metric = mcse.metric) } return(structure(list( coefficients = setNames(theta, names(init)), sample = statsmatrices, sample.obs = statsmatrices.obs, iterations = Lout$counts[1], MCMCtheta = init, loglikelihood = Lout$value, gradient = gradient, hessian = Lout$hessian, covar = covar, failure = FALSE, mc.cov = mc.cov ), class = "ergm")) } }
1:4 %*% 1:4 sum(1:4 * 1:4)
wod_4hist_k_p <- function( obs_index, surv.object, covariate.data , nruns , m ) { actual_data <- cbind( covariate.data , surv.object[,1], surv.object[,2] ) time_index <- ncol(actual_data) - 1 status_index <- ncol(actual_data) data_length <- nrow(actual_data) obs_influences <- rep(x = 0 , data_length) obs_max_influences <- rep(x= 0 , data_length) concs_vector <- rep(x = 0, nruns ) cox_object <- coxph( survival::Surv(actual_data[,time_index], as.integer(actual_data[,status_index]) ) ~ . , data = data.frame(actual_data[,-c(time_index,status_index)]) ) baseline_concordance <- cox_object$concordance[1]/(cox_object$concordance[1] + cox_object$concordance[2] ) cat("baseline:" , baseline_concordance ) concordance_sum <- 0 for ( i in 1:nruns ){ boot_datax <- getBootstrap_K(data = actual_data[-obs_index,], ) actual_conc <- coxph.call(boot_data = boot_datax , time_index = time_index, status_index = status_index ) if (is.null(actual_conc)) { for( ix in 1:10) { cat('Error: ', ix) boot_datax <- getBootstrap_K(data = actual_data[-obs_index,], ) actual_conc <- coxph.call(boot_data = boot_datax , time_index = time_index, status_index = status_index ) if (!is.null(actual_conc)) { break } } } if (is.null(actual_conc)) { next } concordance_run <- actual_conc - baseline_concordance concs_vector[i] <- concordance_run } return(concs_vector) }
summarizeadd2 <- function(abo,probe.effects){ inds.all = indexProbes(abo,which="pm") if(dim(Biobase::exprs(abo))[2] != 1) stop("\n error: to many chips \n\n") if(sum( names(inds.all) == names(probe.effects)) != length(probe.effects)) { reorderind <- as.numeric(factor(names(probe.effects), levels=names(inds.all))) inds.all <- inds.all[reorderind] if(sum( names(inds.all) == names(probe.effects)) != length(probe.effects)) stop("\n error: names of the probe effects do not match the abo \n\n") } inds = unlist(inds.all) Biobase::exprs(abo) = log2(Biobase::exprs(abo)) Biobase::exprs(abo)[inds,] = Biobase::exprs(abo)[inds,]-unlist(probe.effects) chipfu = function(x) median(Biobase::exprs(abo)[x,]) resfu = function(x) Biobase::exprs(abo)[x,] - median(Biobase::exprs(abo)[x,]) chip.effects = unlist(lapply(inds.all,chipfu)) residuals = unlist(lapply(inds.all,resfu )) eset = abo Biobase::exprs(eset) = as.matrix(chip.effects) eset@experimentData@preprocessing$val= list(residuals=residuals) return(eset) }
library(buildmer) library(testthat) test_that('build.formula',{ form1 <- Reaction ~ Days + (Days|Subject) terms <- tabulate.formula(form1) form2 <- build.formula(dep='Reaction',terms) library(lme4) check <- function (f) resid(lmer(f,sleepstudy)) expect_equal(check(form1),check(form2)) })
make_true_parameter_MRMC <- function(StanS4class) { f <- StanS4class z <- extract_EAP_CI(f,"z",f@dataList$C )$z.EAP dz <- extract_EAP_CI(f,"dz",f@dataList$C-1 )$dz.EAP mu <- extract_EAP_by_array(f,mu) v <- extract_EAP_by_array(f,v) }
predictLKrigFixedFunction <- function(object, xnew=NULL, Znew = NULL, drop.Z = FALSE, collapseFixedEffect = FALSE){ if( is.null(xnew)){ xnew<- object$x } nt<- object$nt nZ<- object$nZ ind.drift<- c( rep( TRUE, (nt-nZ) ), rep( FALSE, nZ)) distance.type<- object$LKinfo$distance.type T.matrix<- do.call(object$LKinfo$fixedFunction, c( list(x = xnew, Z = Znew, distance.type = distance.type), object$LKinfo$fixedFunctionArgs)) if( !drop.Z){ temp1<- T.matrix%*%object$d.coef} else{ temp1<- T.matrix%*%object$d.coef[ind.drift, ] } return( temp1) }
setMethodS3("segmentByHaarSeg", "RawGenomicSignals", function(this, ..., cache=FALSE, force=FALSE, verbose=FALSE) { verbose <- Arguments$getVerbose(verbose) if (verbose) { pushState(verbose) on.exit(popState(verbose)) } verbose && enter(verbose, "Segmenting") verbose && cat(verbose, "Chromosomes: ", hpaste(getChromosomes(this))) assertOneChromosome(this) verbose && enter(verbose, "Retrieving the fit function") pkgName <- "HaarSeg" isPackageInstalled(pkgName) || throw("Package is not installed: ", pkgName) pkg <- packageDescription(pkgName) pkgVer <- pkg$Version pkgDetails <- sprintf("%s v%s", pkgName, pkgVer) methodName <- "haarSeg" verbose && cat(verbose, "Method: ", methodName) verbose && cat(verbose, "Package: ", pkgDetails) require(pkgName, character.only=TRUE) || throw("Package not loaded: ", pkgName) envir <- as.environment(sprintf("package:%s", pkgName)) fitFcn <- get(methodName, mode="function", envir=envir) verbose && str(verbose, "Function: ", fitFcn) formals <- formals(fitFcn) verbose && cat(verbose, "Formals:") verbose && str(verbose, formals) verbose && exit(verbose) signatures <- list() signatures$fitFcn <- list( pkgName=pkgName, methodName=methodName, formals=formals, pkgDetails=pkgDetails ) verbose && enter(verbose, "Extracting data of interest") data <- extractDataForSegmentation(this, ..., verbose=less(verbose, 5)) verbose && str(verbose, data) verbose && exit(verbose) sampleName <- attr(data, "sampleName") chromosome <- data$chromosome[1] nbrOfLoci <- nrow(data) hasWeights <- !is.null(data$w) verbose && cat(verbose, "Sample name: ", sampleName) verbose && cat(verbose, "Chromosome: ", chromosome) verbose && cat(verbose, "Number of loci: ", nbrOfLoci) if (hasWeights) { if (!is.element("W", names(formals))) { hasWeights <- FALSE msg <- paste("Weights detected but ignored, because the available segmentation function ('", methodName, "()') does not support weights. Check with a more recent version of the package: ", pkgDetails, sep="") verbose && cat(verbose, "WARNING: ", msg) warning(msg) } } verbose && enter(verbose, "Setting up method arguments") verbose && enter(verbose, "Setting up ", pkgName, " data structure") cnData <- data$y verbose && str(verbose, cnData) verbose && exit(verbose) params <- list() if (hasWeights) { params$W <- data$w verbose && cat(verbose, "Additional segmentation arguments:") verbose && str(verbose, params) } userArgs <- list(...) if (length(userArgs) > 0) { verbose && cat(verbose, "User and segmentation arguments:") verbose && str(verbose, userArgs) for (ff in names(userArgs)) { params[[ff]] <- userArgs[[ff]] } } if (!any(names(formals) == "...")) { keep <- (names(params) %in% names(formals)) params <- params[keep] } signatures$data <- cnData signatures$params <- params args <- c(list(I=cnData), params) verbose && cat(verbose, "Final arguments:") verbose && str(verbose, args) verbose && exit(verbose) verbose && enter(verbose, "Looking for cached results") key <- list(method="segmentByHaarSeg", class=class(this)[1], signatures=signatures) dirs <- c("aroma.cn", class(this)[1]) if (!force) { res <- loadCache(key, dirs=dirs) if (!is.null(res)) { verbose && cat(verbose, "Found cached results.") verbose && exit(verbose) return(res) } } verbose && exit(verbose) verbose && enter(verbose, sprintf("Calling %s() of %s", methodName, pkgName)) stdout <- capture.output({ t <- system.time({ fit <- do.call(methodName, args) }, gcFirst = FALSE) attr(fit, "processingTime") <- t attr(fit, "pkgDetails") <- pkgDetails }) verbose && cat(verbose, "Captured output that was sent to stdout:") stdout <- paste(stdout, collapse="\n") verbose && cat(verbose, stdout) verbose && cat(verbose, "Fitting time (in seconds):") verbose && print(verbose, t) verbose && cat(verbose, "Fitting time per 1000 loci (in seconds):") verbose && print(verbose, 1000*t/nbrOfLoci) verbose && cat(verbose, "Results object:") verbose && str(verbose, fit) verbose && enter(verbose, "Setting up return HaarSeg object") fit <- list( output = fit, data = list(M=data$y, x=data$x, chromosome=chromosome) ) class(fit) <- "HaarSeg" attr(fit, "processingTime") <- t attr(fit, "pkgDetails") <- pkgDetails verbose && exit(verbose) verbose && enter(verbose, "Estimating aroma parameters") sigma <- estimateStandardDeviation(this) cnr <- extractCopyNumberRegions(fit) cnrData <- as.data.frame(cnr) regions <- as.matrix(cnrData[,c("start", "stop")]) nbrOfRegions <- nrow(regions) cnr <- cnrData <- NULL x <- data$x y <- data$y naValue <- NA_real_ sigmas <- rep(naValue, times=nbrOfRegions) for (kk in seq_len(nbrOfRegions)) { keep <- which(regions[kk,1] < x & x <= regions[kk,2]) t <- y[keep] t <- diff(t) t <- median(t, na.rm=TRUE)/sqrt(2) sigmas[kk] <- t } x <- y <- t <- keep <- NULL aromaEstimates <- list( stddevAll = sigma, stddevRegions = sigmas ) attr(fit, "aromaEstimates") <- aromaEstimates verbose && exit(verbose) verbose && cat(verbose, "Results object:") verbose && str(verbose, fit) verbose && exit(verbose) if (cache) { saveCache(fit, key=key, dirs=dirs) } verbose && exit(verbose) fit })
options(shiny.trace = F) require(shiny) require(shinysky) shinyServer(function(input, output, session) { observe({ if (input$id_blank == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "blank")) }) observe({ if (input$id_primary == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "primary"), styleclass = "primary") }) observe({ if (input$id_info == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "info"), styleclass = "info") }) observe({ if (input$id_success == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "success"), styleclass = "success") }) observe({ if (input$id_warning == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "warning"), styleclass = "warning") }) observe({ if (input$id_danger == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "danger", "<button type='button' class='btn btn-danger'>Danger</button>"), styleclass = "danger") }) observe({ if (input$id_inverse == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "inverse"), styleclass = "inverse") }) observe({ if (input$id_link == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "link"), styleclass = "link") }) observe({ if (input$id_inverse2 == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "inverse2"), styleclass = "inverse") }) observe({ if (input$id_warning2 == 0) return() showshinyalert(session, "shinyalert1", paste("You have clicked", "warning2"), styleclass = "warning") }) observe({ if (is.null(input$id_double_click_event)) { return() } print(input$id_double_click_event) if (input$id_double_click_event$event == "dblclick") { showshinyalert(session, "shinyalert2", "You have double clicked! Event button can handle doubleclicks") } else if (input$id_double_click_event$event == "mouseenter") { showshinyalert(session, "shinyalert2", "You came in! Single click won't change me", styleclass = "info") } }) observe({ input$thti showshinyalert(session, "shinyalert3", sprintf("Typeahead Text Input Value: '%s'", input$thti), "error") }) observe({ if (input$updateselect2 == 0) return() updateSelect2Input(session, "select2Input1", choices = c("d", "e", "f"), selected = c("f", "d"), label = "hello") updateSelectInput(session, "select2Input2", choices = c("d", "e", "f"), selected = c("f", "d"), label = "hello") updateSelectInput(session, "select2Input3", choices = c("d", "e", "f"), selected = "f", label = "hello") }) observe({ showshinyalert(session, "shinyalert4", paste(input$select2Input1, collapse = ","), "info") }) observe({ showshinyalert(session, "shinyalert5", paste(input$select2Input2, collapse = ","), "info") }) observe({ showshinyalert(session, "shinyalert6", paste(input$select2Input3, collapse = ","), "info") }) output$plot1 <- renderPlot({ if (input$busyBtn == 0) return() Sys.sleep(3) hist(rnorm(10^3)) }) observe({ if (input$update_typeahead_btn == 0) { return() } dataset <- data.frame(firstname = c("ZJ", "Mitchell"), lastname = c("Dai", "Joblin")) valueKey <- "lastname" tokens <- c("zd", "mj", dataset$firstname) template <- HTML("First Name: <em>{{firstname}}</em> Last Name: <em>{{lastname}}</em>") updateTextInput.typeahead(session, "thti", dataset, valueKey, tokens, template, placeholder = "type 'm' or 'z' to see the updated table") }) output$hotable1 <- renderHotable({ head(iris) }, readOnly = FALSE) observe({ df <- hot.to.df(input$hotable1) print(head(df)) }) })
hidden_paths <- function(model){ if(!inherits(model, c("hmm", "mhmm"))) stop("Argument model must be an object of class 'hmm' or 'mhmm.") if(inherits(model,"mhmm")){ model <- combine_models(model) mix <- TRUE } else mix <- FALSE if(model$n_channels == 1){ model$observations <- list(model$observations) model$emission_probs <- list(model$emission_probs) } model$initial_probs <- log(model$initial_probs) model$transition_probs <- log(model$transition_probs) obsArray <- array(0,c(model$n_sequences,model$length_of_sequences,model$n_channels)) for(i in 1:model$n_channels){ obsArray[,,i] <- sapply(model$observations[[i]], as.integer) - 1L obsArray[,,i][obsArray[,,i]>model$n_symbols[i]] <- model$n_symbols[i] } obsArray <- aperm(obsArray) emissionArray <- array(0,c(model$n_states,max(model$n_symbols)+1,model$n_channels)) for(i in 1:model$n_channels) emissionArray[,1:model$n_symbols[i],i] <- log(model$emission_probs[[i]]) if(mix){ out <- viterbix(model$transition_probs, emissionArray, model$initial_probs, obsArray, model$coefficients, model$X, model$n_states_in_clusters) } else{ out <- viterbi(model$transition_probs, emissionArray, model$initial_probs, obsArray) } if(model$n_sequences==1){ mpp <- t(model$state_names[out$q+1]) }else{ mpp <- apply(out$q+1,2,function(x) model$state_names[x]) } mpp <- suppressWarnings( suppressMessages( seqdef( mpp,alphabet=model$state_names, id=rownames(model$obs[[1]]), start=attr(model$obs[[1]],"start"), xtstep=attr(model$obs[[1]],"xtstep") ) ) ) if (sum(model$n_states) <= 200) { attr(mpp, "cpal") <- seqHMM::colorpalette[[sum(model$n_states)]] } else { cp <- NULL k <- 200 p <- 0 while(sum(model$n_states) - p > 0){ cp <- c(cp, seqHMM::colorpalette[[k]]) p <- p + k k <- k - 1 } attr(mpp, "cpal") <- cp[1:sum(model$n_states)] } attr(mpp, "log_prob") <- c(out$logp) mpp }
fitSpliced=function(cell, body, tail, method,thresh=NULL){ if(method == "BestFit"){ return(fitSplicedBestFit(cell, body, tail, thresh0 = 0.7, thresh.max = 0.98)) } else { if(method != "Fixed"){ thresh<-fitThreshold(cell, body, tail, method) } pars=fitSplicedPar(cell, thresh, body, tail) weight=fitWeights(cell,thresh) sevdist=buildSplicedSevdist(body, pars[[1]], tail, pars[[2]], thresh, weight) return(sevdist) } }