Title: | Format R Output to Look Like SPSS |
---|---|
Description: | Create plots and LaTeX tables that look like SPSS output for use in teaching materials. Rather than copying-and-pasting SPSS output into documents, R code that mocks up SPSS output can be integrated directly into dynamic LaTeX documents with tools such as knitr. Functionality includes statistical techniques that are typically covered in introductory statistics classes: descriptive statistics, common hypothesis tests, ANOVA, and linear regression, as well as box plots, histograms, scatter plots, and line plots (including profile plots). |
Authors: | Andreas Alfons [aut, cre] |
Maintainer: | Andreas Alfons <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.3.3 |
Built: | 2025-02-01 03:27:11 UTC |
Source: | https://github.com/aalfons/r2spss |
Create plots and LaTeX tables that look like SPSS output for use in teaching materials. Rather than copying-and-pasting SPSS output into documents, R code that mocks up SPSS output can be integrated directly into dynamic LaTeX documents with tools such as knitr. Functionality includes statistical techniques that are typically covered in introductory statistics classes: descriptive statistics, common hypothesis tests, ANOVA, and linear regression, as well as box plots, histograms, scatter plots, and line plots (including profile plots).
The DESCRIPTION file:
Package: | r2spss |
Type: | Package |
Title: | Format R Output to Look Like SPSS |
Version: | 0.3.3 |
Date: | 2022-06-03 |
Description: | Create plots and LaTeX tables that look like SPSS output for use in teaching materials. Rather than copying-and-pasting SPSS output into documents, R code that mocks up SPSS output can be integrated directly into dynamic LaTeX documents with tools such as knitr. Functionality includes statistical techniques that are typically covered in introductory statistics classes: descriptive statistics, common hypothesis tests, ANOVA, and linear regression, as well as box plots, histograms, scatter plots, and line plots (including profile plots). |
License: | GPL (>= 3) |
URL: | https://github.com/aalfons/r2spss |
BugReports: | https://github.com/aalfons/r2spss/issues |
Depends: | R (>= 3.5.0), ggplot2 (>= 3.3.0) |
Imports: | graphics, scales, stats, car |
Suggests: | knitr |
LazyLoad: | yes |
VignetteBuilder: | knitr |
Authors@R: | c(person("Andreas", "Alfons", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-2513-3788"))) |
Author: | Andreas Alfons [aut, cre] (<https://orcid.org/0000-0002-2513-3788>) |
Maintainer: | Andreas Alfons <[email protected]> |
Encoding: | UTF-8 |
RoxygenNote: | 7.2.0 |
Config/pak/sysreqs: | cmake make libicu-dev |
Repository: | https://aalfons.r-universe.dev |
RemoteUrl: | https://github.com/aalfons/r2spss |
RemoteRef: | HEAD |
RemoteSha: | 84c1856d406f6f49b8cff8fb72bdeb4ddf4413b4 |
Index of help topics:
ANOVA One-way and Two-way ANOVA Eredivisie Football players of the Dutch Eredivisie season 2013-14 Exams Exam results of an applied statistics course box_plot Box Plots chisq_test Chi-squared Tests descriptives Descriptive Statistics format_SPSS Format Objects histogram Histogram kruskal_test Kruskal-Wallis Test labels_SPSS Format axis tick labels similar to SPSS line_plot Line Plots palette_SPSS SPSS Color Palette and Color Scales r2spss-deprecated Deprecated plot functions in r2spss r2spss-package Format R Output to Look Like SPSS r2spss.sty Create the LaTeX style file for 'r2spss' r2spss_options Options for package r2spss regression Linear Regression scatter_plot Scatter Plot and Scatter Plot Matrix sign_test Sign Test t_test t Tests theme_SPSS Plot theme to mimic the look of SPSS graphs to_SPSS Convert R Objects to SPSS-Style Tables to_latex Print LaTeX Tables that Mimic the Look of SPSS Output trimmed_mean Trimmed mean wilcoxon_test Wilcoxon Signed Rank and Rank Sum Tests
Further information is available in the following vignettes:
r2spss-intro |
r2spss: Format R Output to Look Like SPSS (source) |
Andreas Alfons [aut, cre] (<https://orcid.org/0000-0002-2513-3788>)
Maintainer: Andreas Alfons <[email protected]>
Perform one-way or two-way ANOVA on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output, and a profile plot of the results mimics the look of SPSS graphs.
ANOVA(data, variable, group, conf.level = 0.95) ## S3 method for class 'ANOVA_SPSS' to_SPSS( object, statistics = c("test", "variance", "descriptives"), version = r2spss_options$get("version"), digits = 3, ... ) ## S3 method for class 'ANOVA_SPSS' print( x, statistics = c("descriptives", "variance", "test"), version = r2spss_options$get("version"), ... ) ## S3 method for class 'ANOVA_SPSS' plot(x, y, which = 1, version = r2spss_options$get("version"), ...)
ANOVA(data, variable, group, conf.level = 0.95) ## S3 method for class 'ANOVA_SPSS' to_SPSS( object, statistics = c("test", "variance", "descriptives"), version = r2spss_options$get("version"), digits = 3, ... ) ## S3 method for class 'ANOVA_SPSS' print( x, statistics = c("descriptives", "variance", "test"), version = r2spss_options$get("version"), ... ) ## S3 method for class 'ANOVA_SPSS' plot(x, y, which = 1, version = r2spss_options$get("version"), ...)
data |
a data frame containing the variables. |
variable |
a character string specifying the numeric variable of interest. |
group |
a character vector specifying one or two grouping variables. |
conf.level |
a number between 0 and 1 giving the confidence level of the confidence interval. |
object , x
|
an object of class |
statistics |
a character string or vector specifying which SPSS tables
to produce. Available options are |
version |
a character string specifying whether the table or plot
should mimic the content and look of recent SPSS versions ( |
digits |
an integer giving the number of digits after the comma to be printed in the SPSS tables. |
... |
for the |
y |
ignored (only included because it is defined for the generic
function |
which |
for two-way ANOVA, an integer with possible values |
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
ANOVA
returns an object of class "ANOVA_SPSS"
with the
following components:
descriptives
a data frame containing per-group descriptive statistics.
levene
an object as returned by
leveneTest
(if version = "legacy"
); or a
list of such objects containing different variations of Levene's test
(if version = "modern"
).
test
a data frame containing the ANOVA table.
variable
a character string containing the name of the numeric variable of interest.
group
a character vector containing the name(s) of the grouping variable(s).
i
an integer giving the number of groups in the (first) grouping variable.
j
an integer giving the number of groups in the second grouping variable (only two-way ANOVA).
conf.level
numeric; the confidence level used.
type
a character string giving the type of ANOVA performed
("one-way"
or "two-way"
).
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
The plot
method returns an object of class
"ggplot"
, which produces a profile plot of the ANOVA
results when printed.
The test statistic and p-value for Levene's test based on the trimmed mean
(only returned for version = "modern"
) differ slightly from those
returned by SPSS. Function trimmed_mean
rounds the number of
observations to be trimmed in a different manner than the base R function
mean
, which brings the results closer to those of SPSS, but
they are still not identical.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # one-way ANOVA oneway <- ANOVA(Eredivisie, "logMarketValue", group = "Position") oneway # print LaTeX table plot(oneway) # create profile plot # two-way ANOVA twoway <- ANOVA(Eredivisie, "logMarketValue", group = c("Position", "Foreign")) twoway # print LaTeX table plot(twoway) # create profile plot
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # one-way ANOVA oneway <- ANOVA(Eredivisie, "logMarketValue", group = "Position") oneway # print LaTeX table plot(oneway) # create profile plot # two-way ANOVA twoway <- ANOVA(Eredivisie, "logMarketValue", group = c("Position", "Foreign")) twoway # print LaTeX table plot(twoway) # create profile plot
Draw box plots of variables in a data frame, including box plots for groups of observations and box plots for separate variables. The plots thereby mimic the look of SPSS graphs.
box_plot( data, variables, group = NULL, cut.names = NULL, style = c("T", "whiskers"), coef = c(1.5, 3), outlier.shape = c(1, 42), version = r2spss_options$get("version"), ... )
box_plot( data, variables, group = NULL, cut.names = NULL, style = c("T", "whiskers"), coef = c(1.5, 3), outlier.shape = c(1, 42), version = r2spss_options$get("version"), ... )
data |
a data frame containing the variables to be plotted. |
variables |
a character vector specifying separate variables to be
plotted. If |
group |
an character string specifying a grouping variable, or
|
cut.names |
a logical indicating whether to cut long variable names or
group labels to 8 characters. The default is |
style |
a character string specifying the box plot style. Possible
values are |
coef |
a numeric vector of length 2 giving the multipliers of the interquartile range for determining intermediate and extreme outliers, respectively. |
outlier.shape |
an integer vector of length 2 giving the plot symbol for intermediate and extreme outliers, respectively. |
version |
a character string specifying whether the plot should mimic
the look of recent SPSS versions ( |
... |
additional arguments to be passed down, in particular
aesthetics (see |
An object of class "ggplot"
, which produces
a box plot when printed.
Andreas Alfons
## paired sample # load data data("Exams") # plot grades on regular and resit exams box_plot(Exams, c("Regular", "Resit")) ## independent samples # load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # plot log market values of Dutch and Foreign players box_plot(Eredivisie, "logMarketValue", group = "Foreign")
## paired sample # load data data("Exams") # plot grades on regular and resit exams box_plot(Exams, c("Regular", "Resit")) ## independent samples # load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # plot log market values of Dutch and Foreign players box_plot(Eredivisie, "logMarketValue", group = "Foreign")
TestsPerform a goodness-of-fit test or a
test on independence on variables of
a data set. The output is printed as a LaTeX table that mimics
the look of SPSS output.
chisq_test(data, variables, p = NULL) ## S3 method for class 'chisq_test_SPSS' to_SPSS( object, statistics = c("test", "frequencies"), version = r2spss_options$get("version"), digits = c(1, 3), ... ) ## S3 method for class 'chisq_test_SPSS' print( x, statistics = c("frequencies", "test"), version = r2spss_options$get("version"), digits = c(1, 3), ... ) chisqTest(data, variables, p = NULL)
chisq_test(data, variables, p = NULL) ## S3 method for class 'chisq_test_SPSS' to_SPSS( object, statistics = c("test", "frequencies"), version = r2spss_options$get("version"), digits = c(1, 3), ... ) ## S3 method for class 'chisq_test_SPSS' print( x, statistics = c("frequencies", "test"), version = r2spss_options$get("version"), digits = c(1, 3), ... ) chisqTest(data, variables, p = NULL)
data |
a data frame containing the variables. |
variables |
a character vector specifying the categorical variable(s) of interest. If only one variable is specified, a goodness-of-fit test is performed. If two variables are specified, a test on independence is performed (with the first variable used for the rows and the second variable for the columns of the crosstabulation). |
p |
a vector of probabilities for the categories in the goodness-of-fit test. |
object , x
|
an object of class |
statistics |
a character string or vector specifying which SPSS tables
to produce. Available options are |
version |
a character string specifying whether the table should
mimic the content and look of recent SPSS versions ( |
digits |
an integer vector giving the number of digits after the comma to be printed in the SPSS tables. The first element corresponds to the number of digits for the expected frequencies, and the second element corresponds to the number of digits in the table for the test. |
... |
additional arguments to be passed down to
|
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
An object of class "chisq_test_SPSS"
with the following components:
chisq
a list containing the results of the
test.
lr
a list containing the results of a likelihood ratio test (only test on independence).
MH
a list containing the results of a Mantel-Haenszel test of linear association (only test on independence).
observed
a table containing the observed frequencies.
expected
a vector or matrix containing the expected frequencies.
n
an integer giving the number of observations.
k
an integer giving the number of groups (only goodness-of-fit test).
r
an integer giving the number of groups in the first variable corresponding to the rows (only test on independence).
c
an integer giving the number of groups in the second variable corresponding to the columns (only test on independence).
variables
a character vector containing the name(s) of the categorical variable(s) of interest.
type
a character string giving the type of
test performed (
"goodness-of-fit"
or "independence"
).
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
The test on independence also reports the results of a likelihood ratio test.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
# load data data("Eredivisie") # test whether the traditional Dutch 4-3-3 (total football) # is still reflected in player composition chisq_test(Eredivisie, "Position", p = c(1, 4, 3, 3)/11) # test whether playing position and dummy variable for # foreign players are independent chisq_test(Eredivisie, c("Position", "Foreign"))
# load data data("Eredivisie") # test whether the traditional Dutch 4-3-3 (total football) # is still reflected in player composition chisq_test(Eredivisie, "Position", p = c(1, 4, 3, 3)/11) # test whether playing position and dummy variable for # foreign players are independent chisq_test(Eredivisie, c("Position", "Foreign"))
Compute descriptive statistics of numeric variables of a data set (number of observations, minimum, maximum, mean, standard deviaiton). The output is printed as a LaTeX table that mimics the look of SPSS output.
descriptives(data, variables) ## S3 method for class 'descriptives_SPSS' to_SPSS(object, digits = 2, ...) ## S3 method for class 'descriptives_SPSS' print(x, version = r2spss_options$get("version"), ...)
descriptives(data, variables) ## S3 method for class 'descriptives_SPSS' to_SPSS(object, digits = 2, ...) ## S3 method for class 'descriptives_SPSS' print(x, version = r2spss_options$get("version"), ...)
data |
a data frame containing the variables. |
variables |
a character vector specifying numeric variables for which to compute descriptive statistics. |
object , x
|
an object of class |
digits |
an integer giving the number of digits after the comma to be printed in the SPSS table. |
... |
additional arguments to be passed down to
|
version |
a character string specifying whether the table should
mimic the look of recent SPSS versions ( |
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
An object of class "descriptives_SPSS"
with the following components:
classes
a character vector giving the (first) class of the variables of interest.
descriptives
a data frame containing the descriptive statistics.
n
an integer giving the number of observations.
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
# load data data("Eredivisie") # compute descriptive statistics for market value and age descriptives(Eredivisie, c("MarketValue", "Age"))
# load data data("Eredivisie") # compute descriptive statistics for market value and age descriptives(Eredivisie, c("MarketValue", "Age"))
Data on all football players in the Dutch Eredivisie, the highest men's football league in the Netherlands, who played at least one match in the 2013-14 season.
data("Eredivisie")
data("Eredivisie")
A data frame with 417 observations on the following 20 variables.
Player
the player's name.
Team
the team with which the player was under contract at the end of the 2013-14 season.
MarketValue
the player's market value after the 2013-14 season.
Age
the player's age in years.
Height
the player's height in centimeters.
Foreign
a dummy variable with value 0 for Dutch players and value 1 for players without a Dutch nationality.
Position
the primary postion of the player
("Goalkeeper"
, "Defender"
, "Midfielder"
,
or "Forward"
).
BothFeet
a dummy variable with value 0 if the player has one stronger foot and value 1 if the player is equally strong with both feet.
AtClub
the number of years the player is with the current team.
Contract
the number of years remaining on the player's current contract.
Matches
the number of matches played in the 2013-14 season.
Goals
the number of goals scored in the 2013-14 season.
OwnGoals
the number of own goals scored in the 2013-14 season.
Assists
the number of assists given in the 2013-14 season.
Yellow
the number of yellow cards received in the 2013-14 season.
YellowRed
the number of yellow-red cards received in the 2013-14 season.
Red
the number of red cards received in the 2013-14 season.
SubOn
the number of times the player was substituted on the field in the 2013-14 season.
SubOff
the number of times the player was substituted off the field in the 2013-14 season.
Minutes
the number of minutes played in the 2013-14 season.
data("Eredivisie") summary(Eredivisie)
data("Eredivisie") summary(Eredivisie)
Data on grades for an applied statistics course at Erasmus University Rotterdam for students who took both the regular exam and the resit. Grades in the Netherlands are on a scale from 1 to 10, with a higher grade being better, and a minimum of 5.5 is required to pass.
data("Exams")
data("Exams")
A data frame with 45 observations on the following 2 variables.
Regular
the student's grade based on the regular exam at the end of the course.
Resit
the student's grade based on the resit exam at the end of the academic year.
data("Exams") summary(Exams)
data("Exams") summary(Exams)
Format an object for printing, mostly used to print numeric data in the same
way as SPSS. This is mainly for internal use in to_SPSS
and
print
methods.
format_SPSS(object, ...) ## Default S3 method: format_SPSS(object, ...) ## S3 method for class 'integer' format_SPSS(object, ...) ## S3 method for class 'numeric' format_SPSS(object, digits = 3, p_value = FALSE, check_int = FALSE, ...) ## S3 method for class 'matrix' format_SPSS(object, digits = 3, p_value = FALSE, check_int = FALSE, ...) ## S3 method for class 'data.frame' format_SPSS(object, digits = 3, p_value = FALSE, check_int = FALSE, ...) formatSPSS(object, ...)
format_SPSS(object, ...) ## Default S3 method: format_SPSS(object, ...) ## S3 method for class 'integer' format_SPSS(object, ...) ## S3 method for class 'numeric' format_SPSS(object, digits = 3, p_value = FALSE, check_int = FALSE, ...) ## S3 method for class 'matrix' format_SPSS(object, digits = 3, p_value = FALSE, check_int = FALSE, ...) ## S3 method for class 'data.frame' format_SPSS(object, digits = 3, p_value = FALSE, check_int = FALSE, ...) formatSPSS(object, ...)
object |
an R object. Currently methods are implemented
for vectors, matrices, and data frames. The default method calls
|
... |
additional arguments passed down to methods. |
digits |
an integer giving the number of digits after the comma to display. |
p_value |
a logical indicating whether small positive values should be
indicated as below the threshold defined by |
check_int |
a logical indicating whether to check for integer values
and format them as such, e.g., to format the integer |
A character vector, matrix, or data frame containing the formatted object.
Andreas Alfons
# note how numbers in the interval (-1, 1) are printed # without the zero in front of the comma format_SPSS(c(-1.5, -2/3, 2/3, 1.5))
# note how numbers in the interval (-1, 1) are printed # without the zero in front of the comma format_SPSS(c(-1.5, -2/3, 2/3, 1.5))
Draw a histogram of a variable in a data frame. The plot thereby mimics the look of SPSS graphs.
histogram( data, variable, bins = NULL, normal = FALSE, normal.colour = NULL, normal.color = NULL, normal.linetype = NULL, normal.size = NULL, normal.alpha = NULL, digits = 3, limits = NULL, expand = 0.05, version = r2spss_options$get("version"), ... )
histogram( data, variable, bins = NULL, normal = FALSE, normal.colour = NULL, normal.color = NULL, normal.linetype = NULL, normal.size = NULL, normal.alpha = NULL, digits = 3, limits = NULL, expand = 0.05, version = r2spss_options$get("version"), ... )
data |
a data frame containing the variable to be plotted. |
variable |
a character string specifying the variable to be plotted. |
bins |
an integer giving the number of bins for the histogram. |
normal |
a logical indicating whether to add a normal density with the
estimated mean and standard deviation (the default is |
normal.colour , normal.color , normal.linetype , normal.size , normal.alpha
|
aesthetics for the normal density. In the unlikely event that both US and UK spellings of color are supplied, the US spelling will take precedence. |
digits |
an integer giving the number of digits after the comma to be printed in the summary statistics in the right plot margin. |
limits |
a list of arguments to be passed to
|
expand |
a numeric value specifying the percentage of the range to be
used for padding the axes. The default is 0.05 to expand the |
version |
a character string specifying whether the plot should mimic
the look of recent SPSS versions ( |
... |
additional arguments to be passed down, in particular
aesthetics (see |
An object of class "ggplot"
, which produces
a histogram when printed.
Due to the inner workings of this function to mimic the look
of histograms in SPSS, it is not expected that the user adds
scale_x_continuous
or
scale_y_continuous
to the plot. Instead, axis
limits and padding should be modified via the limits
and
expand
arguments.
Andreas Alfons
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # plot histogram of log market values histogram(Eredivisie, "logMarketValue", normal = TRUE, limits = list(x = c(9.5, 17.5)))
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # plot histogram of log market values histogram(Eredivisie, "logMarketValue", normal = TRUE, limits = list(x = c(9.5, 17.5)))
Perform a Kruskal-Wallis test on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output.
kruskal_test(data, variable, group) ## S3 method for class 'kruskal_test_SPSS' to_SPSS( object, statistics = c("test", "ranks"), version = r2spss_options$get("version"), digits = NULL, ... ) ## S3 method for class 'kruskal_test_SPSS' print( x, statistics = c("ranks", "test"), version = r2spss_options$get("version"), digits = 2:3, ... ) kruskalTest(data, variable, group)
kruskal_test(data, variable, group) ## S3 method for class 'kruskal_test_SPSS' to_SPSS( object, statistics = c("test", "ranks"), version = r2spss_options$get("version"), digits = NULL, ... ) ## S3 method for class 'kruskal_test_SPSS' print( x, statistics = c("ranks", "test"), version = r2spss_options$get("version"), digits = 2:3, ... ) kruskalTest(data, variable, group)
data |
a data frame containing the variables. |
variable |
a character string specifying the numeric variable of interest. |
group |
a character string specifying a grouping variable. |
object , x
|
an object of class |
statistics |
a character string or vector specifying which SPSS tables
to produce. Available options are |
version |
a character string specifying whether the table should
mimic the content and look of recent SPSS versions ( |
digits |
for the |
... |
additional arguments to be passed down to
|
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
An object of class "kruskal_test_SPSS"
with the following components:
statistics
a data frame containing information on the per-group mean ranks.
test
a list containing the results of the Kruskal-Wallis test.
variable
a character string containing the name of the numeric variable of interest.
group
a character string containing the name of the grouping variable.
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
# load data data("Eredivisie") # test whether market values differ by playing position kruskal_test(Eredivisie, "MarketValue", group = "Position")
# load data data("Eredivisie") # test whether market values differ by playing position kruskal_test(Eredivisie, "MarketValue", group = "Position")
Format axis tick labels in a similar manner to SPSS to mimic the look of SPSS graphs.
number_SPSS(x, big.mark = "", ...) numberSPSS(x, big.mark = "", ...) substr_SPSS(x, start = 1, stop = 8) substrSPSS(x, start = 1, stop = 8)
number_SPSS(x, big.mark = "", ...) numberSPSS(x, big.mark = "", ...) substr_SPSS(x, start = 1, stop = 8) substrSPSS(x, start = 1, stop = 8)
x |
for |
big.mark |
a character string to be inserted every 3 digits to separate thousands. The default is an empty string for no separation. |
... |
additional arguments to be passed to
|
start , stop
|
integers giving the first and last character to remain in the cut string, respectively. The default is to cut strings to the first 8 characters. |
number_SPSS
is a wrapper for number
that by
default does not put a separator every 3 digits so separate thousands. It
mainly exists to prevent scientific notation in axis tick labels, hence it
is typically supplied as the labels
argument of
scale_x_continuous
or
scale_y_continuous
.
substr_SPSS
is a wrapper for substr
to cut character
strings by default to the first 8 characters, which is SPSS behavior for
the tick labels of a discrete axis in some (but not all) plots. It is
typically supplied as the labels
argument of
scale_x_discrete
or
scale_y_discrete
.
A character vector of the same length as x
Draw connected lines for variables in a data frame. The plot thereby mimics the look of SPSS graphs.
line_plot( data, variables, index = NULL, version = r2spss_options$get("version"), ... )
line_plot( data, variables, index = NULL, version = r2spss_options$get("version"), ... )
data |
a data frame containing the variables to be plotted. |
variables |
a character vector specifying at least one variable to be
plotted on the |
index |
a character string specifying a variable to be plotted on the
|
version |
a character string specifying whether the plot should mimic
the look of recent SPSS versions ( |
... |
additional arguments to be passed down to
|
An object of class "ggplot"
, which produces
a line plot when printed.
Andreas Alfons
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # aggregate log market values by position means <- aggregate(Eredivisie[, "logMarketValue", drop = FALSE], Eredivisie[, "Position", drop = FALSE], FUN = mean) # create profile plot line_plot(means, "logMarketValue", "Position") # easier and fancier as the plot method of ANOVA results oneway <- ANOVA(Eredivisie, "logMarketValue", group = "Position") plot(oneway)
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # aggregate log market values by position means <- aggregate(Eredivisie[, "logMarketValue", drop = FALSE], Eredivisie[, "Position", drop = FALSE], FUN = mean) # create profile plot line_plot(means, "logMarketValue", "Position") # easier and fancier as the plot method of ANOVA results oneway <- ANOVA(Eredivisie, "logMarketValue", group = "Position") plot(oneway)
Color palette used by SPSS, and discrete color scales to be used in plots (e.g., for multiple lines in a plot) to mimic the look of SPSS graphs.
palette_SPSS(n = NULL, version = r2spss_options$get("version")) paletteSPSS(n = NULL, version = r2spss_options$get("version")) SPSS_pal(version = r2spss_options$get("version"), direction = 1) scale_color_SPSS( ..., version = r2spss_options$get("version"), direction = 1, aesthetics = "color" ) scale_colour_SPSS( ..., version = r2spss_options$get("version"), direction = 1, aesthetics = "colour" ) scale_fill_SPSS( ..., version = r2spss_options$get("version"), direction = 1, aesthetics = "fill" )
palette_SPSS(n = NULL, version = r2spss_options$get("version")) paletteSPSS(n = NULL, version = r2spss_options$get("version")) SPSS_pal(version = r2spss_options$get("version"), direction = 1) scale_color_SPSS( ..., version = r2spss_options$get("version"), direction = 1, aesthetics = "color" ) scale_colour_SPSS( ..., version = r2spss_options$get("version"), direction = 1, aesthetics = "colour" ) scale_fill_SPSS( ..., version = r2spss_options$get("version"), direction = 1, aesthetics = "fill" )
n |
an integer giving the number of colors to select from the palette.
If |
version |
a character string specifying whether to use the color
palette of recent SPSS versions ( |
direction |
an integer giving the direction to travel through the palette. Possible values are 1 for forward (the default) and -1 for backward. |
... |
additional arguments to be passed to
|
aesthetics |
a character string or vector listing the names of the
aesthetics with which the scale works. For example, color settings can be
applied to the |
palette_SPSS
returns a character vector specifying up to 30
colors as used by SPSS.
SPSS_pal
returns a function that generates colors from the
specified SPSS color palette, in the specified direction. It is mainly
used internally by the discrete color scales.
scale_color_SPSS
, scale_colour_SPSS
, and
scale_fill_SPSS
return a discrete color scale to be added to plots.
Andreas Alfons
# data to be plotted df <- data.frame(x = 1:30, y = 0) # initialize plot p <- ggplot(aes(x = x, y = y, fill = factor(x)), data = df) + geom_point(shape = 21, size = 3, show.legend = FALSE) + theme_SPSS() # colors of modern SPSS versions p + theme_SPSS() + scale_fill_SPSS() # colors of legacy SPSS versions p + theme_SPSS(version = "legacy") + scale_fill_SPSS(version = "legacy")
# data to be plotted df <- data.frame(x = 1:30, y = 0) # initialize plot p <- ggplot(aes(x = x, y = y, fill = factor(x)), data = df) + geom_point(shape = 21, size = 3, show.legend = FALSE) + theme_SPSS() # colors of modern SPSS versions p + theme_SPSS() + scale_fill_SPSS() # colors of legacy SPSS versions p + theme_SPSS(version = "legacy") + scale_fill_SPSS(version = "legacy")
Retrieve or set global options for package r2spss (within the current R session) via accessor functions.
r2spss_options
r2spss_options
A list with the following two components:
get(which, drop = TRUE)
an accessor function to retrieve
current options, which are usually returned as a named list. Argument
which
allows to select which options to retrieve. If a single
option is selected, argument drop
indicates whether only its value
should be returned (TRUE
) or a list of length one (FALSE
).
set(...)
an accessor function to set certain options using
name = value
pairs.
The following options are available:
version
a character string that controls the default for
whether tables and plots should mimic the content and look of recent SPSS
versions ("modern"
) or older versions (<24; "legacy"
).
minor
a logical that overrides whether function
to_latex
should include any supplied minor grid lines
in SPSS tables. In particular for tables that mimic older SPSS versions,
minor grid lines can be somewhat distracting from the content, so setting
this option to FALSE
provides a quick way to suppress them. The
look of the resulting tables still closely mimics SPSS while being
somewhat cleaner.
Andreas Alfons
# retrieve the list of options r2spss_options$get() # retrieve a single option r2spss_options$get("version") ## Not run: # set options r2spss_options$set(version = "legacy", minor = FALSE) ## End(Not run)
# retrieve the list of options r2spss_options$get() # retrieve a single option r2spss_options$get("version") ## Not run: # set options r2spss_options$set(version = "legacy", minor = FALSE) ## End(Not run)
These plot functions are deprecated and may be removed as soon as the
next release of r2spss. The functions plotSPSS
,
linesSPSS
, boxSPSS
, and histSPSS
are built around
base R graphics and have been superseded by functions built on
ggplot2
.
plotSPSS(data, variables, xlab = NULL, ylab = NULL, ...) linesSPSS(data, variables, index = NULL, xlab = NULL, ylab = NULL, ...) boxplotSPSS( data, variables, group = NULL, xlab = NULL, ylab = NULL, cut.names = NULL, ... ) histSPSS(data, variable, normal = FALSE, xlab = NULL, ylab = NULL, ...)
plotSPSS(data, variables, xlab = NULL, ylab = NULL, ...) linesSPSS(data, variables, index = NULL, xlab = NULL, ylab = NULL, ...) boxplotSPSS( data, variables, group = NULL, xlab = NULL, ylab = NULL, cut.names = NULL, ... ) histSPSS(data, variable, normal = FALSE, xlab = NULL, ylab = NULL, ...)
data |
a data frame containing the variables to be plotted. |
variables |
For For For |
xlab , ylab
|
the axis labels. |
... |
additional arguments to be passed down, in particular
graphical parameters (see |
index |
a character string specifying a variable to be plotted on the
|
group |
an character string specifying a grouping variable, or
|
cut.names |
a logical indicating whether to cut long variable names or
group labels to 8 characters. The default is |
variable |
a character string specifying the variable to be plotted. |
normal |
a logical indicating whether to add a normal density with the
estimated mean and standard deviation (the default is |
plotSPSS
draws a scatter plot or a scatter plot matrix of variables
in a data frame.
linesSPSS
draws connected lines for variables in a data frame.
boxplotSPSS
draw box plots of variables in a data frame, including
box plots for groups of observations and box plots for separate variables.
histSPSS
draws a histogram of a variable in a data frame.
The plots thereby mimic the look of graphs in older versions of SPSS (<24).
plotSPSS
and linesSPSS
do not return anything but produce a
plot.
boxplotSPSS
returns a list containing summary statistics invisibly
(see boxplot
) and produces a plot.
histSPSS
returns an object of class "histogram"
invisibly (see
hist
) and produces a plot.
Andreas Alfons
Create the LaTeX style file required to compile LaTeX documents that include tables created by package r2spss. You can put the resulting file r2spss.sty in the folder containing your LaTeX document, and you should include
\usepackage{r2spss}
in the preamble of your LaTeX document.
r2spss.sty(path = NULL)
r2spss.sty(path = NULL)
path |
a character string specifying the path to the folder in which
to put the style file, or |
Nothing is returned, the function is called for its side effects.
Andreas Alfons
# print contents of style file r2spss.sty() ## Not run: # put file 'r2spss.sty' in the current working directory r2spss.sty(".") ## End(Not run)
# print contents of style file r2spss.sty() ## Not run: # put file 'r2spss.sty' in the current working directory r2spss.sty(".") ## End(Not run)
Perform linear regression on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output, and plots of the results mimic the look of SPSS graphs.
regression(..., data, labels = NULL) ## S3 method for class 'regression_SPSS' to_SPSS( object, statistics = c("estimates", "anova", "summary"), change = FALSE, version = r2spss_options$get("version"), ... ) ## S3 method for class 'regression_SPSS' print( x, statistics = c("summary", "anova", "estimates"), change = FALSE, version = r2spss_options$get("version"), ... ) ## S3 method for class 'regression_SPSS' coef(object, ...) ## S3 method for class 'regression_SPSS' df.residual(object, ...) ## S3 method for class 'regression_SPSS' fitted(object, standardized = FALSE, ...) ## S3 method for class 'regression_SPSS' residuals(object, standardized = FALSE, ...) ## S3 method for class 'regression_SPSS' plot( x, y, which = c("histogram", "scatter"), version = r2spss_options$get("version"), ... )
regression(..., data, labels = NULL) ## S3 method for class 'regression_SPSS' to_SPSS( object, statistics = c("estimates", "anova", "summary"), change = FALSE, version = r2spss_options$get("version"), ... ) ## S3 method for class 'regression_SPSS' print( x, statistics = c("summary", "anova", "estimates"), change = FALSE, version = r2spss_options$get("version"), ... ) ## S3 method for class 'regression_SPSS' coef(object, ...) ## S3 method for class 'regression_SPSS' df.residual(object, ...) ## S3 method for class 'regression_SPSS' fitted(object, standardized = FALSE, ...) ## S3 method for class 'regression_SPSS' residuals(object, standardized = FALSE, ...) ## S3 method for class 'regression_SPSS' plot( x, y, which = c("histogram", "scatter"), version = r2spss_options$get("version"), ... )
... |
for |
data |
a data frame containing the variables. |
labels |
a character or numeric vector giving labels for the regression models in the output tables. |
object , x
|
an object of class |
statistics |
a character string or vector specifying which SPSS tables
to produce. Available options are |
change |
a logical indicating whether tests on the
|
version |
a character string specifying whether the table or plot
should mimic the content and look of recent SPSS versions ( |
standardized |
a logical indicating whether to return standardized
residuals and fitted values ( |
y |
ignored (only included because it is defined for the generic
function |
which |
a character string specifying which plot to produce. Possible
values are |
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
An object of class "regression_SPSS"
with the following
components:
models
a list in which each component is an ojbect of class
"lm"
as returned by function lm
.
summaries
a list in which each component is an ojbect of
class "summary.lm"
as returned by the
summary
method for objects of class
"lm"
.
response
a character string containing the name of the response variable.
method
a character string specifying whether the nested
models are increasing in dimension by entering additional variables
("enter"
) or decreasing in dimension by removing variables
("remove"
).
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
The coef
, df.residual
, fitted
and residuals
methods return the coefficients, residual degrees of freedom, fitted
values and residuals, respectively, of the last model (to mimic
SPSS functionality).
Similarly, the plot
method returns the specified plot for the
last model as an object of class "ggplot"
,
which produces the plot when printed.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # squared values of age Eredivisie$AgeSq <- Eredivisie$Age^2 # simple regression model of log market value on age fit1 <- regression(logMarketValue ~ Age, data = Eredivisie) fit1 # print LaTeX table plot(fit1, which = "scatter") # diagnostic plot # add a squared effect for age fit2 <- regression(logMarketValue ~ Age + AgeSq, data = Eredivisie, labels = 2) fit2 # print LaTeX table plot(fit2, which = "scatter") # diagnostic plot # more complex models with model comparison fit3 <- regression(logMarketValue ~ Age + AgeSq, logMarketValue ~ Age + AgeSq + Contract + Foreign, logMarketValue ~ Age + AgeSq + Contract + Foreign + Position, data = Eredivisie, labels = 2:4) print(fit3, change = TRUE) # print LaTeX table plot(fit3, which = "histogram") # diagnostic plot
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # squared values of age Eredivisie$AgeSq <- Eredivisie$Age^2 # simple regression model of log market value on age fit1 <- regression(logMarketValue ~ Age, data = Eredivisie) fit1 # print LaTeX table plot(fit1, which = "scatter") # diagnostic plot # add a squared effect for age fit2 <- regression(logMarketValue ~ Age + AgeSq, data = Eredivisie, labels = 2) fit2 # print LaTeX table plot(fit2, which = "scatter") # diagnostic plot # more complex models with model comparison fit3 <- regression(logMarketValue ~ Age + AgeSq, logMarketValue ~ Age + AgeSq + Contract + Foreign, logMarketValue ~ Age + AgeSq + Contract + Foreign + Position, data = Eredivisie, labels = 2:4) print(fit3, change = TRUE) # print LaTeX table plot(fit3, which = "histogram") # diagnostic plot
Draw a scatter plot or a scatter plot matrix of variables in a data frame. The plots thereby mimic the look of SPSS graphs.
scatter_plot(data, variables, version = r2spss_options$get("version"), ...)
scatter_plot(data, variables, version = r2spss_options$get("version"), ...)
data |
a data frame containing the variables to be plotted. |
variables |
a character vector specifying at least two variables to be
plotted. In case of two variables, a simple scatter plot is produced with
the first variable on the |
version |
a character string specifying whether the plot should mimic
the look of recent SPSS versions ( |
... |
for a simple scatter plot, additional arguments are passed
down to |
In case of a simple scatter plot, an object of class
"ggplot"
, which produces the plot when printed.
In case of a scatter plot matrix, nothing is returned but a plot is produced.
Wile all other plots in r2spss are based on ggplot2 (including the simple scatter plot), the scatter plot matrix is built around base R graphics. This is because ggplot2 does not provide an implementation of a scatter plot matrix, and an implementation based on separate scatter plots on a matrix layout would be slow.
Andreas Alfons
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # plot log market values against age scatter_plot(Eredivisie, c("Age", "logMarketValue")) # scatterplot matrix of age, number of minutes played, and # log market values scatter_plot(Eredivisie, c("Age", "Minutes", "logMarketValue"))
# load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # plot log market values against age scatter_plot(Eredivisie, c("Age", "logMarketValue")) # scatterplot matrix of age, number of minutes played, and # log market values scatter_plot(Eredivisie, c("Age", "Minutes", "logMarketValue"))
Perform a sign test for a paired sample on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output.
sign_test(data, variables, exact = FALSE) ## S3 method for class 'sign_test_SPSS' to_SPSS( object, statistics = c("test", "frequencies"), version = r2spss_options$get("version"), ... ) ## S3 method for class 'sign_test_SPSS' print( x, statistics = c("frequencies", "test"), version = r2spss_options$get("version"), ... ) signTest(data, variables, exact = FALSE)
sign_test(data, variables, exact = FALSE) ## S3 method for class 'sign_test_SPSS' to_SPSS( object, statistics = c("test", "frequencies"), version = r2spss_options$get("version"), ... ) ## S3 method for class 'sign_test_SPSS' print( x, statistics = c("frequencies", "test"), version = r2spss_options$get("version"), ... ) signTest(data, variables, exact = FALSE)
data |
a data frame containing the variables. |
variables |
a character vector specifying two numeric variables containing the paired observations. |
exact |
a logical indicating whether or not to include the exact p-value using the binomial distribution. Note that the p-value using the normal approximation is always reported. |
object , x
|
an object of class |
statistics |
a character string or vector specifying which SPSS tables
to produce. Available options are |
version |
a character string specifying whether the table should
mimic the content and look of recent SPSS versions ( |
... |
additional arguments to be passed down to
|
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
An object of class "sign_test_SPSS"
with the following
components:
statistics
a data frame containing information on the number of observations with negative and positive differences.
asymptotic
a list containing the results of the test using the normal approximation.
exact
if requested, a numeric vector containing the exact two-sided p-value, one-sided p-value, and point probability using the binomial distribution.
variables
a character vector containing the names of the two numeric variables with the paired observations.
n
an integer giving the number of observations.
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
# load data data("Exams") # test whether grades differ between the # regular exam and the resit sign_test(Exams, c("Regular", "Resit"))
# load data data("Exams") # test whether grades differ between the # regular exam and the resit sign_test(Exams, c("Regular", "Resit"))
Perform a one-sample t test, a paired-sample t test or an independent-samples t test on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output.
t_test(data, variables, group = NULL, mu = 0, conf.level = 0.95) ## S3 method for class 't_test_SPSS' to_SPSS( object, statistics = c("test", "statistics"), version = r2spss_options$get("version"), digits = 3, ... ) ## S3 method for class 't_test_SPSS' print( x, statistics = c("statistics", "test"), version = r2spss_options$get("version"), digits = 3, ... ) tTest(data, variables, group = NULL, mu = 0, conf.level = 0.95)
t_test(data, variables, group = NULL, mu = 0, conf.level = 0.95) ## S3 method for class 't_test_SPSS' to_SPSS( object, statistics = c("test", "statistics"), version = r2spss_options$get("version"), digits = 3, ... ) ## S3 method for class 't_test_SPSS' print( x, statistics = c("statistics", "test"), version = r2spss_options$get("version"), digits = 3, ... ) tTest(data, variables, group = NULL, mu = 0, conf.level = 0.95)
data |
a data frame containing the variables. |
variables |
a character vector specifying numeric variable(s) to be
used for testing the mean(s). If |
group |
a character string specifying a grouping variable for an
independent-samples t-test, or |
mu |
a number indicating the true value of the mean for a one-sample t test. |
conf.level |
a number between 0 and 1 giving the confidence level of the confidence interval. |
object , x
|
an object of class |
statistics |
a character string or vector specifying which SPSS tables
to produce. Available options are |
version |
a character string specifying whether the table should
mimic the content and look of recent SPSS versions ( |
digits |
an integer giving the number of digits after the comma to be printed in the SPSS tables. |
... |
additional arguments to be passed down to
|
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
An object of class "t_test_SPSS"
with the following
components:
statistics
a data frame containing the relevant descriptive statistics.
test
an object of class "htest"
as returned by
t.test
(only one-sample and paired-sample tests).
variables
a character vector containing the name(s) of the relevant numeric variable(s).
n
an integer giving the number of observations (only paired-sample test).
levene
an object as returned by
leveneTest
(only independent-samples test).
pooled
an object of class "htest"
as returned
by t.test
assuming equal variances (only
independent-samples test).
satterthwaite
an object of class "htest"
as returned
by t.test
not assuming equal variance (only
independent-samples test).
group
a character string containing the name of the grouping variable (only independent-samples test).
type
a character string giving the type of t test performed
("one-sample"
, "paired"
, or "independent"
).
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
## one-sample and paired-sample t test # load data data("Exams") # test whether the average grade on the resit # differs from 5.5 (minimum passing grade) t_test(Exams, "Resit", mu = 5.5) # test whether average grades differ between the # regular exam and the resit t_test(Exams, c("Resit", "Regular")) ## independent-samples t test # load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # test whether average log market values differ between # Dutch and foreign players t_test(Eredivisie, "logMarketValue", group = "Foreign")
## one-sample and paired-sample t test # load data data("Exams") # test whether the average grade on the resit # differs from 5.5 (minimum passing grade) t_test(Exams, "Resit", mu = 5.5) # test whether average grades differ between the # regular exam and the resit t_test(Exams, c("Resit", "Regular")) ## independent-samples t test # load data data("Eredivisie") # log-transform market values Eredivisie$logMarketValue <- log(Eredivisie$MarketValue) # test whether average log market values differ between # Dutch and foreign players t_test(Eredivisie, "logMarketValue", group = "Foreign")
Complete theme that controls all non-data display of a plot to mimic the
look of SPSS graphs. Use theme
after
theme_SPSS
to further tweak the display.
theme_SPSS( base_size = 12, base_family = "", base_line_size = 0.5, base_rect_size = 0.5, version = r2spss_options$get("version"), scales = NULL, scale.x = scales, scale.y = scales )
theme_SPSS( base_size = 12, base_family = "", base_line_size = 0.5, base_rect_size = 0.5, version = r2spss_options$get("version"), scales = NULL, scale.x = scales, scale.y = scales )
base_size |
an integer giving the base font size in pts. |
base_family |
a character string giving the base font family. |
base_line_size |
base size for line elements. |
base_rect_size |
base size for borders of rectangle elements. |
version |
a character string specifying whether to mimic the look
of recent SPSS versions ( |
scales , scale.x , scale.y
|
a character string specifying whether both
or each of the axes are expected to be continuous ( |
# data to be plotted df <- data.frame(x = 1:30, y = 0) # initialize plot p <- ggplot(aes(x = x, y = y, fill = factor(x)), data = df) + geom_point(shape = 21, size = 3, show.legend = FALSE) + theme_SPSS() # colors of modern SPSS versions p + theme_SPSS() + scale_fill_SPSS() # colors of legacy SPSS versions p + theme_SPSS(version = "legacy") + scale_fill_SPSS(version = "legacy")
# data to be plotted df <- data.frame(x = 1:30, y = 0) # initialize plot p <- ggplot(aes(x = x, y = y, fill = factor(x)), data = df) + geom_point(shape = 21, size = 3, show.legend = FALSE) + theme_SPSS() # colors of modern SPSS versions p + theme_SPSS() + scale_fill_SPSS() # colors of legacy SPSS versions p + theme_SPSS(version = "legacy") + scale_fill_SPSS(version = "legacy")
Use information from an R object to print a LaTeX table that mimics the
look of SPSS output. Typically, one would first call to_SPSS
with an object returned by a function in r2spss, and then call
to_latex
with the resulting object of class "SPSS_table"
to
print the LaTeX table. Note that the print
methods in r2spss
perform these two steps at once, but calling to_SPSS
and
to_latex
separately can be useful for customization of the LaTeX
table.
to_latex(object, ...) ## S3 method for class 'SPSS_table' to_latex(object, version = r2spss_options$get("version"), ...) ## S3 method for class 'data.frame' to_latex( object, main = NULL, sub = NULL, header = TRUE, label = NULL, row_names = TRUE, info = NULL, alignment = NULL, border = NULL, footnotes = NULL, major = NULL, minor = NULL, version = r2spss_options$get("version"), ... )
to_latex(object, ...) ## S3 method for class 'SPSS_table' to_latex(object, version = r2spss_options$get("version"), ...) ## S3 method for class 'data.frame' to_latex( object, main = NULL, sub = NULL, header = TRUE, label = NULL, row_names = TRUE, info = NULL, alignment = NULL, border = NULL, footnotes = NULL, major = NULL, minor = NULL, version = r2spss_options$get("version"), ... )
object |
an object of class |
... |
for the |
version |
a character string specifying whether the table should
mimic the look of recent SPSS versions ( |
main |
a single character string defining the main title of the SPSS
table, or |
sub |
a single character string defining the sub-title of the SPSS
table, or |
header |
a logical indicating whether to include a header in the SPSS
table based on the column names of
Line breaks (character |
label |
a character string giving a label to be added as the first
column of the table, or |
row_names |
a logical indicating whether to add the row names of
|
info |
an integer giving the number of columns in the SPSS table
that contain auxiliary information on the results. This has an effect
of the default formatting, alignment, and borders. The default is 0 if
|
alignment |
a list with components |
border |
a logical vector indicating which (outer and inner) vertical
borders should be drawn. The default is that tables that mimic recent
versions of SPSS ( |
footnotes |
a character vector giving footnotes to be added below the
SPSS table, or
|
major , minor
|
an integer vector specifying the rows of the SPSS
table after which to draw major or minor grid lines that stretch across
all columns of the table, or
The only difference between the two type of grid lines is that minor grid
lines can also be suppressed globally within the current R session by
setting |
The "SPSS_table"
method takes component table
of the object
and supplies it to the data.frame
method, with additional components
in the object being passed as additional arguments.
The "data.frame"
method allows to extend the functionality of
r2spss with additional LaTeX tables that mimic the look of SPSS
output.
Nothing is returned, the function is called for its side effects.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
## Kruskal-Wallis test example # load data data("Eredivisie") # compute a Kruskual-Wallis test to investigate whether # market values differ by playing position kw <- kruskal_test(Eredivisie, "MarketValue", group = "Position") # convert to an object of class "SPSS_table" that # contains the table with the test results kw_spss <- to_SPSS(kw, statistics = "test") kw_spss # blank out the number of degrees of freedom to ask # an assignment question about it kw_spss$table[2, 1] <- "???" # print the LaTeX table to be included in the assignment to_latex(kw_spss) ## t test example # load data data("Exams") # test whether the average grade on the resit # differs from 5.5 (minimum passing grade) t <- t_test(Exams, "Resit", mu = 5.5) # convert to an object of class "SPSS_table" that # contains the table with the test results t_spss <- to_SPSS(t, statistics = "test") # this is an example of a complex header layout t_spss$header # add additional line breaks in bottom-level header t_spss$header[[3]] <- gsub("-", "-\n", t_spss$header[[3]], fixed = TRUE) # print the LaTeX table to_latex(t_spss)
## Kruskal-Wallis test example # load data data("Eredivisie") # compute a Kruskual-Wallis test to investigate whether # market values differ by playing position kw <- kruskal_test(Eredivisie, "MarketValue", group = "Position") # convert to an object of class "SPSS_table" that # contains the table with the test results kw_spss <- to_SPSS(kw, statistics = "test") kw_spss # blank out the number of degrees of freedom to ask # an assignment question about it kw_spss$table[2, 1] <- "???" # print the LaTeX table to be included in the assignment to_latex(kw_spss) ## t test example # load data data("Exams") # test whether the average grade on the resit # differs from 5.5 (minimum passing grade) t <- t_test(Exams, "Resit", mu = 5.5) # convert to an object of class "SPSS_table" that # contains the table with the test results t_spss <- to_SPSS(t, statistics = "test") # this is an example of a complex header layout t_spss$header # add additional line breaks in bottom-level header t_spss$header[[3]] <- gsub("-", "-\n", t_spss$header[[3]], fixed = TRUE) # print the LaTeX table to_latex(t_spss)
Generic function to convert an R object into an object that contains all necessary information for printing a LaTeX table that mimics the look of SPSS output.
to_SPSS(object, ...) to_spss(object, ...)
to_SPSS(object, ...) to_spss(object, ...)
object |
an R object for which a |
... |
additional arguments passed down to methods. |
In order to work as expected, methods of to_SPSS
should return an
object of class "SPSS_table"
. It should include a component
table
that contains a data frame, which can be supplied as the
first argument to to_latex
to print a LaTeX table that mimics
the look of SPSS output. Additional components of the returned object
define additional arguments to be passed to the "data.frame"
method
of to_latex
.
to_spss
is a simple wrapper for to_SPSS
, which exists
for convenience.
Andreas Alfons
# load data data("Eredivisie") # compute a Kruskual-Wallis test to investigate whether # market values differ by playing position kw <- kruskal_test(Eredivisie, "MarketValue", group = "Position") # convert to an object of class "SPSS_table" that # contains the table with the test results kw_spss <- to_SPSS(kw, statistics = "test") kw_spss # blank out the number of degrees of freedom to ask # an assignment question about it kw_spss$table[2, 1] <- "???" # print the LaTeX table to be included in the assignment to_latex(kw_spss)
# load data data("Eredivisie") # compute a Kruskual-Wallis test to investigate whether # market values differ by playing position kw <- kruskal_test(Eredivisie, "MarketValue", group = "Position") # convert to an object of class "SPSS_table" that # contains the table with the test results kw_spss <- to_SPSS(kw, statistics = "test") kw_spss # blank out the number of degrees of freedom to ask # an assignment question about it kw_spss$table[2, 1] <- "???" # print the LaTeX table to be included in the assignment to_latex(kw_spss)
Compute the trimmed mean. This function differs from the implementation of
the trimmed mean in the base R function mean
in the following
ways. While mean
always rounds down the number of observations to
be trimmed, this function rounds to the nearest integer. In addition,
mean
implements proper NA
handling, whereas this function
assumes that there are no missing values and may fail in their presence.
trimmed_mean(x, trim = 0.05)
trimmed_mean(x, trim = 0.05)
x |
a numeric vector. |
trim |
numeric; the fraction of observations to be trimmed from each
tail of |
The main purpose of this function is to reproduce SPSS results for Levene's
test on homogeneity of the variances based on the trimmed mean (see
ANOVA
), which are slightly too far off when using the base
R function mean
. Rounding the number of observations to be
trimmed to the nearest integer brings the results closer to those of SPSS,
but they are still not identical.
The trimmed mean of the values in x
as a single numeric value.
Andreas Alfons
x <- c(0:10, 50) # trimmed_mean() rounds number of observations # to be trimmed to the nearest integer trimmed_mean(x, trim = 0.05) # base R function mean() rounds down number of # observations to be trimmed mean(x, trim = 0.05) mean(x)
x <- c(0:10, 50) # trimmed_mean() rounds number of observations # to be trimmed to the nearest integer trimmed_mean(x, trim = 0.05) # base R function mean() rounds down number of # observations to be trimmed mean(x, trim = 0.05) mean(x)
Perform a Wilcoxon signed rank test for a paired sample or a Wilcoxon rank sum test for independent samples on variables of a data set. The output is printed as a LaTeX table that mimics the look of SPSS output.
wilcoxon_test(data, variables, group = NULL, exact = FALSE) ## S3 method for class 'wilcoxon_test_SPSS' to_SPSS( object, statistics = c("test", "ranks"), version = r2spss_options$get("version"), digits = NULL, ... ) ## S3 method for class 'wilcoxon_test_SPSS' print( x, statistics = c("ranks", "test"), version = r2spss_options$get("version"), digits = 2:3, ... ) wilcoxonTest(data, variables, group = NULL, exact = FALSE)
wilcoxon_test(data, variables, group = NULL, exact = FALSE) ## S3 method for class 'wilcoxon_test_SPSS' to_SPSS( object, statistics = c("test", "ranks"), version = r2spss_options$get("version"), digits = NULL, ... ) ## S3 method for class 'wilcoxon_test_SPSS' print( x, statistics = c("ranks", "test"), version = r2spss_options$get("version"), digits = 2:3, ... ) wilcoxonTest(data, variables, group = NULL, exact = FALSE)
data |
a data frame containing the variables. |
variables |
a character vector specifying numeric variable(s) to be
used. If |
group |
a character string specifying a grouping variable for the
Wilcoxon rank sum test, or |
exact |
a logical indicating whether the Wilcoxon rank sum test
should also return the p-value of the exact test. The default is
|
object , x
|
an object of class |
statistics |
a character string or vector specifying which SPSS tables
to produce. Available options are |
version |
a character string specifying whether the table should
mimic the content and look of recent SPSS versions ( |
digits |
for the |
... |
additional arguments to be passed down to
|
The print
method first calls the to_SPSS
method followed
by to_latex
. Further customization can be done by calling
those two functions separately, and modifying the object returned by
to_SPSS
.
An object of class "wilcoxon_test_SPSS"
with the following
components:
statistics
a data frame containing the relevant information on the ranks.
test
a list containing the results of the Wilcoxon signed rank test (only paired-sample test).
variables
a character vector containing the name(s) of the relevant numeric variable(s).
n
an integer giving the number of observations (only paired-sample test).
u
numeric; the Mann-Whitney U test statistic (only independent-samples test).
w
numeric; the Wilcoxon rank sum test statistic (only independent-samples test).
asymptotic
a list containing the results of the Wilcoxon rank sum test using the normal approximation (only independent-samples test).
exact
if requested, the corresponding p-value of the exact Wilcoxon rank sum test test (only independent-samples test).
group
a character string containing the name of the grouping variable (only independent-samples test).
type
a character string giving the type of Wilcoxon test
performed "paired"
or "independent"
).
The to_SPSS
method returns an object of class "SPSS_table"
which contains all relevant information in the required format to produce
the LaTeX table. See to_latex
for possible components and
how to further customize the LaTeX table based on the returned object.
The print
method produces a LaTeX table that mimics the look of SPSS
output.
The Wilcoxon rank sum test also reports the value of the equivalent Mann-Whitney U test statistic.
LaTeX tables that mimic recent versions of SPSS (version = "modern"
)
may require several LaTeX compilations to be displayed correctly.
Andreas Alfons
## paired sample # load data data("Exams") # test whether grades differ between the # regular exam and the resit wilcoxon_test(Exams, c("Regular", "Resit")) ## independent samples # load data data("Eredivisie") # test whether market values differ between Dutch and foreign # players wilcoxon_test(Eredivisie, "MarketValue", group = "Foreign")
## paired sample # load data data("Exams") # test whether grades differ between the # regular exam and the resit wilcoxon_test(Exams, c("Regular", "Resit")) ## independent samples # load data data("Eredivisie") # test whether market values differ between Dutch and foreign # players wilcoxon_test(Eredivisie, "MarketValue", group = "Foreign")