From 3bdf46474e0df0d974771afc654a27534bcace32 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Wed, 20 Nov 2024 14:48:39 +0100 Subject: [PATCH] tidy up syntax --- R/MXLX.R | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/R/MXLX.R b/R/MXLX.R index aaa1aad63..8459d5b6f 100644 --- a/R/MXLX.R +++ b/R/MXLX.R @@ -12,38 +12,36 @@ MXLX = function(Y = NULL, X = 5, epochSize = 1, tseg = c(0, 24), resolutionMin = do.MXLX = FALSE } - nsh = 60 / resolutionMin # number of steps per hour - nes = (resolutionMin * 60) / epochSize # number of epochs per step + nStepsPerHour = 60 / resolutionMin # number of steps per hour + nEpochsPerStep = (resolutionMin * 60) / epochSize # number of epochs per step if (do.MXLX == TRUE) { # only do the analysis if Y has values other than zero Y = Y[((((tseg[1] - tseg[1]) * 3600)/epochSize) + 1):(((tseg[2] - tseg[1]) * 3600)/epochSize)] - Nwindows = Nwindows * nsh + Nwindows = Nwindows * nStepsPerHour rollingMean = matrix(NA, Nwindows, 1) for (hri in 1:Nwindows) { #e.g.9am-9pm # start and end in terms of steps - e1 = hri - 1 #e.g. 9am - e2 = e1 + (X * nsh) #e.g. 9am + 5 hrs + indexStart = hri - 1 #e.g. 9am + indexEnd = indexStart + (X * nStepsPerHour) #e.g. 9am + 5 hrs # start and end in terms of Y index - e1 = (e1 * nes) + 1 - e2 = e2 * nes - einclude = e1:e2 - if (e2 <= length(Y)) { + indexStart = (indexStart * nEpochsPerStep) + 1 + indexEnd = indexEnd * nEpochsPerStep + einclude = indexStart:indexEnd + if (indexEnd <= length(Y)) { rollingMean[hri,1] = mean(Y[einclude]) } } valid = which(is.na(rollingMean) == F) - minV = min(rollingMean[valid], na.rm = T) - maxV = max(rollingMean[valid], na.rm = T) - LXhr = ((which(rollingMean == minV & is.na(rollingMean) == F) - 1) / nsh) + tseg[1] - MXhr = ((which(rollingMean == maxV & is.na(rollingMean) == F) - 1) / nsh) + tseg[1] - LXvalue = minV - MXvalue = maxV + LXvalue = min(rollingMean[valid], na.rm = T) + MXvalue = max(rollingMean[valid], na.rm = T) + LXhr = ((which(rollingMean == LXvalue & is.na(rollingMean) == F) - 1) / nStepsPerHour) + tseg[1] + MXhr = ((which(rollingMean == MXvalue & is.na(rollingMean) == F) - 1) / nStepsPerHour) + tseg[1] + #------------------------------------- if (length(LXvalue) > 1) { LXvalue = sort(LXvalue)[ceiling(length(LXvalue)/2)] } if (length(LXhr) > 1) { LXhr = sort(LXhr)[ceiling(length(LXhr)/2)] } if (length(MXvalue) > 1) { MXvalue = sort(MXvalue)[ceiling(length(MXvalue)/2)] } if (length(MXhr) > 1) { MXhr = sort(MXhr)[ceiling(length(MXhr)/2)] } - #Note it is + 1 because first epoch is one and not zero: LXstart = ((LXhr - tseg[1]) * (3600 / epochSize)) + 1 LXend = LXstart + (X * (3600 / epochSize)) - 1 @@ -67,7 +65,7 @@ MXLX = function(Y = NULL, X = 5, epochSize = 1, tseg = c(0, 24), resolutionMin = MXindex1 = NA, stringsAsFactors = TRUE) } MXLXnames = c(paste0("L", X, "hr"), paste0("L", X), paste0("start_L", X), paste0("end_L", X), - paste0("M", X, "hr"), paste0("M", X), paste0("start_M", X), paste0("end_M", X)) + paste0("M", X, "hr"), paste0("M", X), paste0("start_M", X), paste0("end_M", X)) names(MXLX) = MXLXnames return(MXLX) } \ No newline at end of file