# (v2) [R] quadratic trends and changes in slopes (R-help digest, Vol 1 #52 - 16 msgs)

Chuck White chuck at chuckandmaggi.com
Tue Jan 21 03:50:03 CET 2003

```-----Original Message-----
Message: 6
Date: Mon, 20 Jan 2003 01:11:24 +0100
From: Martin Michlmayr <tbm at cyrius.com>
To: r-help at stat.math.ethz.ch
Subject: [R] quadratic trends and changes in slopes

I'd like to use linear and quadratic trend analysis in order to find
out a change in slope.  Basically, I need to solve a similar problem as
discussed in
http://www.gseis.ucla.edu/courses/ed230bc1/cnotes4/trend1.html

....

RESPONSE: This message updates my earlier response. I just started
programming with R on Friday and I wanted to make my response to you
more general... before starting on a more difficult problem of the same
type at work. The two improvements I've added are: (1) a more general
procedure for more quickly and accurately creating the contrast vectors
and (2) direct program interaction with results in output tables. Access
to results in output tables is so much easier in R than what I've had to
do in SAS that it's incredible. An R program to work the example you
cited is appended. I hope this meets your needs.

Chuck White

# R program for working example at:
# http://www.gseis.ucla.edu/courses/ed230bc1/cnotes4/trend2.html
#
# Copy and paste the example data from the web to a plain text editor
# and save as 'example.txt'. If this program is copied and passted into
# the R Console then the program is expected to run without further
# operator intervention.

col.names=c('y','grp','o1','o2','o3'))
example

# Make variable names available to session

attach(example)

# Convert grp from numeric format to factor format for ANOVA. If you
don't
# do this then you get the wrong test (with only 1 degree of freedom).

fgrp<-factor(grp)
fgrp

# Conduct ANOVA on grp

GRP<-lm(y~fgrp)
anova(GRP)

# Conduct ANOVA on contrasts

## The example aready contains the contrast vectors but you'll have to
## create them yourself when you use real data. A quick and accurate way
to ## set them up is to use the repeat command (rep) as follows:

Linear<-c(rep(-3,8),rep(-1,8),rep(1,8),rep(3,8))
Cubic<-c(rep(-1,8),rep(3,8),rep(-3,8),rep(1,8))

Contrasts.anova<-anova(Contrasts)
Contrasts.anova

# Calculate the F-test and P-value for Nonlinear

SS.Cubic<-Contrasts.anova\$"Sum Sq"[3]

DF.Cubic<-Contrasts.anova\$"Df"[3]

SS.Residuals<-Contrasts.anova\$"Sum Sq"[4]
DF.Residuals<-Contrasts.anova\$"Df"[4]

Nonlinear.test<-(SS.Nonlinear/DF.Nonlinear)/(SS.Residuals/DF.Residuals)

Nonlinear.test

Nonlinear.pvalue<-1-pf(Nonlinear.test,DF.Nonlinear,DF.Residuals)

Nonlinear.pvalue

```