Skip to content

Commit

Permalink
Move descriptive to jasp desciptives (#25)
Browse files Browse the repository at this point in the history
* Move Description analysis to jaspDescriptives module

* set right jaspDescriptives
  • Loading branch information
boutinb authored Apr 11, 2024
1 parent e00f533 commit 215f938
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 792 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Encoding: UTF-8
Imports:
jaspBase,
jaspGraphs,
jaspDescriptives,
forecast
Suggests:
testthat
Remotes:
jasp-stats/jaspBase,
jasp-stats/jaspGraphs
jasp-stats/jaspGraphs,
jasp-stats/jaspDescriptives
15 changes: 13 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import(jaspBase)
export(DescriptivesTimeSeries)
export(StationarityTimeSeries)
export(ARIMATimeSeries)
export(SpectralTimeSeries)
export(DescriptivesTimeSeries)
export(DescriptivesTimeSeriesInternal)
import(jaspBase)
importFrom(jaspDescriptives,.plotMarginal)
importFrom(jaspDescriptives,.DescriptivesTimeSeries)
importFrom(jaspDescriptives,.tsReadData)
importFrom(jaspDescriptives,.tsErrorHandler)
importFrom(jaspDescriptives,.tsGuessInterval)
importFrom(jaspDescriptives,.tsDataFilterHandler)
importFrom(jaspDescriptives,.tsDataWithMissingRowsHandler)
importFrom(jaspDescriptives,.tsFillTimeSeriesPlot)
importFrom(jaspDescriptives,.tsFillACF)

218 changes: 2 additions & 216 deletions R/DescriptivesTimeSeries.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,220 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

DescriptivesTimeSeries <- function(jaspResults, dataset, options) {
ready <- options$dependent != ""

datasetRaw <- .tsReadData(jaspResults, dataset, options, ready)

datasetFiltered <- .tsDataFilterHandler(datasetRaw, options, ready)

dataset <- .tsDataWithMissingRowsHandler(datasetFiltered, options, ready)

.tsErrorHandler(dataset, ready)

.tsDescriptivesTable(jaspResults, dataset, options, ready, position = 1, dependencies = c(.tsDescriptivesDependencies(), "descriptivesTableTransposed"))

.tsTimeSeriesPlotDescriptives(jaspResults, dataset, options, ready, position = 2, dependencies = c(.tsDescriptivesDependencies(), "timeSeriesPlot", "timeSeriesPlotType", "timeSeriesPlotDistribution"))

.tslagPlotDescriptives(jaspResults, dataset, options, ready, position = 3, dependencies = c(.tsDescriptivesDependencies(), "lagPlot", "lagPlotLag", "lagPlotRegressionType", "lagPlotRegressionLine", "lagPlotRegressionCi", "lagPlotRegressionCiLevel"))

.tsACFDescriptives(jaspResults, dataset, options, ready, position = 4, dependencies = c(.tsDescriptivesDependencies(), "acf", "acfCi", "acfCiLevel", "acfCiType", "acfZeroLag", "acfMaxLag"))

.tsPACFDescriptives(jaspResults, dataset, options, ready, position = 5, dependencies = c(.tsDescriptivesDependencies(), "pacf", "pacfCi", "pacfCiLevel", "pacfCiType", "pacfMaxLag"))
}

.tsDescriptivesDependencies <- function() {
return(c(
"dependent", "time",
"filter", "filterBy", "rowStart", "rowEnd",
"timeStart", "timeEnd", "dateStart", "dateEnd"
))
}

.tsTimeSeriesPlotDescriptives <- function(jaspResults, dataset, options, ready, position, dependencies) {
if (!options$timeSeriesPlot) {
return()
}

if (is.null(jaspResults[["timeSeriesPlot"]])) {
plot <- createJaspPlot(title = gettext("Time Series Plot"), width = 660)
plot$dependOn(dependencies)
plot$position <- position

jaspResults[["timeSeriesPlot"]] <- plot

if (!ready) {
return()
}

.tsFillTimeSeriesPlot(
plot, dataset, options,
type = options$timeSeriesPlotType,
distribution = options$timeSeriesPlotDistribution
)
}
}

.tslagPlotDescriptives <- function(jaspResults, dataset, options, ready, position, dependencies) {
if (!options$lagPlot) {
return()
}

if (is.null(jaspResults[["lagPlot"]])) {
plot <- createJaspPlot(title = gettext("Lag Plot"))
plot$dependOn(dependencies)
plot$position <- position

jaspResults[["lagPlot"]] <- plot

if (!ready) {
return()
}

.tsFillLagPlot(plot, dataset, options)
}
}

.tsFillLagPlot <- function(lagPlot, dataset, options) {
# create lag version of y
yLag <- c(rep(NA, options$lagPlotLag), dataset$y[1:(length(dataset$y) - options$lagPlotLag)])

yName <- decodeColNames(options$dependent[1])
xName <- as.expression(bquote(.(yName)[t - .(options$lagPlotLag)]))
yName <- as.expression(bquote(.(yName)[t]))

dat <- data.frame(y = dataset$y, yLag)
dat <- na.omit(dat)

breaks <- jaspGraphs::getPrettyAxisBreaks(c(dat$y, dat$yLag))

p <- jaspGraphs::JASPScatterPlot(
dat$yLag, dat$y,
xName = xName,
yName = yName,
addSmooth = options$lagPlotRegressionLine,
addSmoothCI = options$lagPlotRegressionCi,
smoothCIValue = options$lagPlotRegressionCiLevel,
forceLinearSmooth = options$lagPlotRegressionType == "linear",
plotAbove = "none", plotRight = "none"
)

# make sure the y-axis and the x-axis have the same breaks
p$subplots$mainPlot <- p$subplots$mainPlot +
ggplot2::scale_x_continuous(breaks = breaks, limits = range(breaks)) +
ggplot2::scale_y_continuous(breaks = breaks, limits = range(breaks)) +
jaspGraphs::geom_rangeframe() +
jaspGraphs::themeJaspRaw()

lagPlot$plotObject <- p

return()
}

.tsACFDescriptives <- function(jaspResults, dataset, options, ready, position, dependencies) {
if (!options$acf) {
return()
}

if (is.null(jaspResults[["acfPlot"]])) {
plot <- createJaspPlot(title = gettext("Autocorrelation Function"))
plot$dependOn(dependencies)
plot$position <- position

jaspResults[["acfPlot"]] <- plot

if (!ready) {
return()
}

.tsFillACF(plot,
type = "ACF", dataset, options,
zeroLag = options$acfZeroLag,
maxLag = options$acfMaxLag,
ci = options$acfCi,
ciValue = options$acfCiLevel,
ciType = options$acfCiType
)
}
}

.tsPACFDescriptives <- function(jaspResults, dataset, options, ready, position, dependencies) {
if (!options$pacf) {
return()
}

if (is.null(jaspResults[["pacfPlot"]])) {
plot <- createJaspPlot(title = gettext("Partial Autocorrelation Function"))
plot$dependOn(dependencies)
plot$position <- position

jaspResults[["pacfPlot"]] <- plot

if (!ready) {
return()
}

.tsFillACF(plot,
type = "PACF", dataset, options,
maxLag = options$pacfMaxLag,
ci = options$pacfCi,
ciValue = options$pacfCiLevel,
ciType = options$pacfCiType
)
}
}

.tsDescriptivesTable <- function(jaspResults, dataset, options, ready, position, dependencies) {
if (!is.null(jaspResults[["descriptivesTable"]])) {
return()
}

table <- createJaspTable(gettext("Descriptive Statistics"))
table$dependOn(dependencies)
table$position <- position
table$showSpecifiedColumnsOnly <- TRUE
table$transpose <- !options[["descriptivesTableTransposed"]] # the table is transposed by default

table$addColumnInfo(name = "variable", title = " ", type = "string")
table$addColumnInfo(name = "valid", title = gettext("Valid"), type = "integer")
table$addColumnInfo(name = "missing", title = gettext("Missing"), type = "integer")
table$addColumnInfo(name = "mean", title = gettext("Mean"), type = "number")
table$addColumnInfo(name = "sd", title = gettext("Std. Deviation"), type = "number")
table$addColumnInfo(name = "var", title = gettext("Variance"), type = "number")
table$addColumnInfo(name = "range", title = gettext("Range"), type = "number")
table$addColumnInfo(name = "min", title = gettext("Minimum"), type = "number")
table$addColumnInfo(name = "max", title = gettext("Maximum"), type = "number")
table$addColumnInfo(name = "start", title = gettext("Start"), type = "number")
table$addColumnInfo(name = "end", title = gettext("End"), type = "number")
table$addColumnInfo(name = "ar", title = gettext("Lag 1 Autocorrelation"), type = "number")

jaspResults[["descriptivesTable"]] <- table

if (ready) {
na.omitted <- na.omit(dataset$y)
yName <- options$dependent[1]

nY <- length(na.omitted)
minY <- min(na.omitted)
maxY <- max(na.omitted)

yLag <- c(NA, dataset$y[1:(length(dataset$y) - 1)])
corY <- cor(dataset$y, yLag, use = "complete.obs")

rows <- data.frame(
variable = yName,
valid = nY,
missing = nrow(dataset) - nY,
mean = mean(na.omitted),
sd = sd(na.omitted),
var = var(na.omitted),
range = maxY - minY,
min = minY,
max = maxY,
start = na.omitted[1],
end = na.omitted[nY],
ar = corY
)
table$addRows(rows)
}
DescriptivesTimeSeriesInternal <- function(jaspResults, dataset, options) {
jaspDescriptives::.DescriptivesTimeSeries(jaspResults, dataset, options)
}
70 changes: 70 additions & 0 deletions R/DescriptivesTimeSeriesWrapper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#
# Copyright (C) 2013-2022 University of Amsterdam
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# This is a generated file. Don't change it

DescriptivesTimeSeries <- function(
data = NULL,
version = "0.18.3",
acf = FALSE,
acfCi = TRUE,
acfCiLevel = 0.95,
acfCiType = "whiteNoise",
acfMaxLag = 10,
acfZeroLag = FALSE,
dateEnd = "",
dateStart = "",
dependent = "",
descriptivesTableTransposed = FALSE,
filter = FALSE,
filterBy = "row",
lagPlot = FALSE,
lagPlotLag = 1,
lagPlotRegressionCi = TRUE,
lagPlotRegressionCiLevel = 0.95,
lagPlotRegressionLine = TRUE,
lagPlotRegressionType = "smooth",
pacf = FALSE,
pacfCi = TRUE,
pacfCiLevel = 0.95,
pacfMaxLag = 10,
plotHeight = 320,
plotWidth = 480,
rowEnd = 100,
rowStart = 1,
time = "",
timeEnd = 100,
timeSeriesPlot = TRUE,
timeSeriesPlotDistribution = "none",
timeSeriesPlotType = "both",
timeStart = 1) {

defaultArgCalls <- formals(jaspTimeSeries::DescriptivesTimeSeries)
defaultArgs <- lapply(defaultArgCalls, eval)
options <- as.list(match.call())[-1L]
options <- lapply(options, eval)
defaults <- setdiff(names(defaultArgs), names(options))
options[defaults] <- defaultArgs[defaults]
options[["data"]] <- NULL
options[["version"]] <- NULL

optionsWithFormula <- c("dependent", "time")
for (name in optionsWithFormula) {
if ((name %in% optionsWithFormula) && inherits(options[[name]], "formula")) options[[name]] = jaspBase::jaspFormula(options[[name]], data) }

return(jaspBase::runWrappedAnalysis("jaspTimeSeries::DescriptivesTimeSeries", data, options, version))
}
Loading

0 comments on commit 215f938

Please sign in to comment.