Learn R Programming

statuser (version 0.1.9)

twolines: Two-Lines Test of U-Shapes

Description

Implements the two-lines test for U-shaped (or inverted U-shaped) relationships introduced by Simonsohn (2018).

Usage

twolines(
  f,
  graph = 1,
  link = "gaussian",
  data = NULL,
  pngfile = "",
  quiet = FALSE
)

Value

A list containing:

  • All elements from reg2(): b1, b2, p1, p2, z1, z2, u.sig, xc, glm1, glm2, rob1, rob2, msg, yhat.smooth

  • yobs: Observed y values (adjusted for covariates if present)

  • y.hat: Fitted values from GAM

  • y.ub, y.lb: Upper and lower bounds for fitted values

  • y.most: Most extreme fitted value

  • x.most: x-value associated with most extreme fitted value

  • f: Formula as character string

  • bx1, bx2: Linear and quadratic coefficients from preliminary quadratic regression

  • minx: Minimum x value

  • midflat: Median of flat region

  • midz1, midz2: Z-statistics at midpoint

Arguments

f

A formula object specifying the model (e.g., y ~ x1 + x2 + x3). The first predictor is the one tested for a u-shaped relationship.

graph

Integer. If 1 (default), produces a plot. If 0, no plot is generated.

link

Character string specifying the link function for the GAM model. Default is "gaussian".

data

An optional data frame containing the variables in the formula. If not provided, variables are evaluated from the calling environment.

pngfile

Optional character string. If provided, saves the plot to a PNG file with the specified filename.

quiet

Logical. If TRUE, suppresses the Robin Hood details messages. Default is FALSE.

Details

Reference: Simonsohn, Uri (2018) "Two lines: A valid alternative to the invalid testing of U-shaped relationships with quadratic regressions." AMPPS, 538-555. tools:::Rd_expr_doi("10.1177/2515245918805755")

The test beings fitting a GAM model, predicting y with a smooth of x, and optionally with covariates. It identifies the interior most extreme value of fitted y, and adjusts from the matching x-value to set the breakpoint relying on the Robin Hood procedure introduced also by Simonsohn (2018). It then estimates the (once) interrupted regression using that breakpoint, and reports the slope and significance of the average slopes at either side of it. A U-shape is significant if the slopes are of opposite sign and are both individually significant.

Examples

Run this code
# \donttest{
# Simple example with simulated data
set.seed(123)
x <- rnorm(100)
y <- -x^2 + rnorm(100)
data <- data.frame(x = x, y = y)
result <- twolines(y ~ x, data = data)

# With covariates
z <- rnorm(100)
y <- -x^2 + 0.5*z + rnorm(100)
data <- data.frame(x = x, y = y, z = z)
result <- twolines(y ~ x + z, data = data)

# Without data argument (variables evaluated from environment)
x <- rnorm(100)
y <- -x^2 + rnorm(100)
result <- twolines(y ~ x)
# }

Run the code above in your browser using DataLab