[R] Getting Sphericity Tests for Within Subject Repeated Measure Anova (using "car" package)

Sergios (Sergey) Charntikov sergioschr at gmail.com
Wed Nov 11 01:54:49 CET 2009


Thank you.  I think that this is what I was looking for.  I am getting
the hang of it with my data.  Thank you all.


Sincerely,

Sergios Charntikov (Sergey), MA

Behavioral Neuropharmacology Lab
Department of Psychology
University of Nebraska-Lincoln
Lincoln, NE 68588-0308  USA




On Tue, Nov 10, 2009 at 6:14 AM, Mike Lawrence <Mike.Lawrence at dal.ca> wrote:
> Oops, I see now that despite repeated subject names, treatment is a
> between-Ss variable, so you need to use this to get the equivalent of
> Anova:
>
> library(ez)
> a = read.table( 'Sergios_wide_data.txt' , header=T )
> b = melt.data.frame( data=a , id.vars=c('subject','treatment') ,
> variable_name='day' )
> b$subject=paste(b$subject,b$treatment) #create unique sids
> ezANOVA( data=b , dv=.(value) , sid=.(subject) , within=.(day) ,
> between=.(treatment) )
>
>
>
>
> On Tue, Nov 10, 2009 at 6:47 AM, Mike Lawrence <Mike.Lawrence at dal.ca> wrote:
>> Check out the reshape package for transforming data from long to wide
>> and vice versa.
>>
>> Yet I still don't know what problem you've encountered with ezANOVA.
>> Using the data you just sent, where Day now has 3 levels, I reformat
>> back to the presumably original long format and find that ezANOVA
>> returns the same sphericity tests as John's solution (which is
>> expected because ezANOVA is a wrapper to Anova):
>>
>> library(ez)
>> a = read.table( 'Sergios_wide_data.txt' , header=T )
>> b = melt.data.frame( data=a , id.vars=c('subject','treatment') ,
>> variable_name='day' )
>> ezANOVA( data=b , dv=.(value) , sid=.(subject) , within=.(treatment,day) )
>>
>>
>>
>> On Mon, Nov 9, 2009 at 10:20 PM, Sergios (Sergey) Charntikov
>> <sergioschr at gmail.com> wrote:
>>> Thank you very much.  Finally got it to work.  However, I had to recode it from:
>>> columns: subject/treatment/DV (where all my response data was in one
>>> DV column) to columns: subject/treatment/day1/day2/day3/ (where my
>>> response data is now in three different columns).
>>>
>>> Is there a way to do that without hand recoding (cutting and pasting
>>> in spreadsheet) by hand? Thank you for your help.  Glad it works as
>>> is.
>>>
>>>
>>> Sincerely,
>>>
>>> Sergios Charntikov (Sergey), MA
>>>
>>> Behavioral Neuropharmacology Lab
>>> Department of Psychology
>>> University of Nebraska-Lincoln
>>> Lincoln, NE 68588-0308  USA
>>>
>>>
>>>
>>>
>>>
>>> On Mon, Nov 9, 2009 at 7:12 PM, John Fox <jfox at mcmaster.ca> wrote:
>>>> Dear Sergios,
>>>>
>>>> Why don't you try what I suggested originally? Adapted to this data set,
>>>>
>>>> mod <- lm(cbind(day1, day2, day3) ~ Treatment, data=Dataset)
>>>> idata <- data.frame(Day=factor(1:3))
>>>> summary(Anova(mod, idata=idata, idesign=~Day))
>>>>
>>>> Peter Dalgaard also pointed toward an article that describes how to do the
>>>> same thing with anova().
>>>>
>>>> Regards,
>>>>  John
>>>>
>>>>> -----Original Message-----
>>>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>>>> On
>>>>> Behalf Of Sergios (Sergey) Charntikov
>>>>> Sent: November-09-09 7:13 PM
>>>>> To: Mike Lawrence
>>>>> Cc: r-help at r-project.org
>>>>> Subject: Re: [R] Getting Sphericity Tests for Within Subject Repeated
>>>> Measure
>>>>> Anova (using "car" package)
>>>>>
>>>>> Hi Mike,
>>>>>
>>>>> I tried to run my data in SPSS and it works fine without any problems,
>>>> plug
>>>>> in my levels, plug in my covariate (since it is all within) and get my
>>>>> Mauchly Tests.
>>>>>
>>>>> I tried to rearrange the data so it looks like this
>>>>>
>>>>> subj/treatment/day1/day2/day3
>>>>>
>>>>> subject    treatment    day1    day2    day3
>>>>> 1    1    8    8    8
>>>>> 1    2    5    7    5
>>>>> 2    1    7    4    4
>>>>> 2    2    4    5    7
>>>>> 3    1    8    6    4
>>>>> 3    2    5    2    4
>>>>> 4    1    2    9    4
>>>>> 4    2    1    9    1
>>>>> 5    1    4    8    1
>>>>> 5    2    7    8    2
>>>>> 6    1    4    7    2
>>>>> 6    2    4    5    2
>>>>>
>>>>>
>>>>> When I try mlmfit <- lm(Dataset~1), I get "invalid type (list) for
>>>> variable
>>>>> 'Dataset"
>>>>>
>>>>> When I try
>>>>>
>>>>> mod <- lm(cbind(day1, day2, day3) ~ Treatment, data=Dataset)
>>>>>
>>>>> idata<- data.frame(factor(rep(c(Dataset$day1, Dataset$day2,
>>>> Dataset$day3))),
>>>>> ordered(Dataset$Treatment))
>>>>>
>>>>> Anova(mod, idata=idata, idesign=~Dataset$Treatment)
>>>>>
>>>>> I get: Terms in the intra-subject model matrix are not orthogonal.
>>>>>
>>>>> When I try is.matrix(Dataset) - I get no.
>>>>>
>>>>> My original mock Dataset (attached in txt) is below.  Maybe I am not
>>>> coding
>>>>> it right? I would hate to recode all my data for SPSS, since at the end I
>>>>> would need to show that Sphericity was not violated.
>>>>>
>>>>> Subj  Trtmt   Sessn   Response
>>>>>
>>>>> 1     N       1       5
>>>>>
>>>>> 1     D       1       6
>>>>>
>>>>> 1     N       2       4
>>>>>
>>>>> 1     D       2       7
>>>>>
>>>>> 2     N       1       8
>>>>>
>>>>> 2     D       1       9
>>>>>
>>>>> 2     N       2       2
>>>>>
>>>>> 2     D       2       1
>>>>>
>>>>> 3     N       1       4
>>>>>
>>>>> 3     D       1       5
>>>>>
>>>>> 3     N       2       6
>>>>>
>>>>> 3     D       2       2
>>>>>
>>>>> 4     N       1       5
>>>>>
>>>>> 4     D       1       6
>>>>>
>>>>> 4     N       2       4
>>>>>
>>>>> 4     D       2       7
>>>>>
>>>>> 5     N       1       8
>>>>>
>>>>> 5     D       1       9
>>>>>
>>>>> 5     N       2       2
>>>>>
>>>>> 5     D       2       1
>>>>>
>>>>> 6     N       1       4
>>>>>
>>>>> 6     D       1       5
>>>>>
>>>>> 6     N       2       6
>>>>>
>>>>> 6     D       2       2
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Sincerely,
>>>>>
>>>>> Sergios Charntikov (Sergey), MA
>>>>>
>>>>> Behavioral Neuropharmacology Lab
>>>>> Department of Psychology
>>>>> University of Nebraska-Lincoln
>>>>> Lincoln, NE 68588-0308  USA
>>>>>
>>>>>
>>>>>
>>>>> On Mon, Nov 9, 2009 at 5:29 PM, Mike Lawrence <Mike.Lawrence at dal.ca>
>>>> wrote:
>>>>> >
>>>>> > No luck as in...? What error did you encounter?
>>>>> >
>>>>> > In your example data set, you only have 2 levels of each within-Ss
>>>>> > factor, in which case you shouldn't expect to obtain tests of
>>>>> > sphericity; as far as I understand it, sphericity necessarily holds
>>>>> > when for repeated measures with only 2 levels and tests are really
>>>>> > only possible for repeated measures with 3 or more levels.
>>>>> >
>>>>> > I think it's analogous to how you don't need to test homogeneity of
>>>>> > variance when performing a paired t-test; the test ends up
>>>>> > representing the pairs as single distribution of difference scores
>>>>> > with a single variance.
>>>>> >
>>>>> > Mike
>>>>> >
>>>>> > On Mon, Nov 9, 2009 at 5:30 PM, Sergios (Sergey) Charntikov
>>>>> > <sergioschr at gmail.com> wrote:
>>>>> > > Tried EZanova, no luck with my particular dataset.
>>>>> > >
>>>>> > >
>>>>> > > Sincerely,
>>>>> > >
>>>>> > > Sergios Charntikov (Sergey), MA
>>>>> > >
>>>>> > > Behavioral Neuropharmacology Lab
>>>>> > > Department of Psychology
>>>>> > > University of Nebraska-Lincoln
>>>>> > > Lincoln, NE 68588-0308  USA
>>>>> > >
>>>>> > >
>>>>> > >
>>>>> > >
>>>>> > > On Mon, Nov 9, 2009 at 2:25 PM, Mike Lawrence <Mike.Lawrence at dal.ca>
>>>>> wrote:
>>>>> > >>
>>>>> > >> Have you tried ezANOVA from the ez pacakge? It attempts to provide
>>>>> > >> a simple user interface to car's ANOVA (and when that fails, aov).
>>>>> > >>
>>>>> > >> On Mon, Nov 9, 2009 at 1:44 PM, Sergios (Sergey) Charntikov
>>>>> > >> <sergioschr at gmail.com> wrote:
>>>>> > >> > Hello everyone,
>>>>> > >> >
>>>>> > >> > I am trying to do within subjects repeated measures anova
>>>>> > >> > followed by the test of sphericity (sample dataset below).
>>>>> > >> > I am able to get either mixed model or linear model anova and
>>>>> > >> > TukeyHSD, but have no luck with Repeated-Measures Assuming
>>>>> > >> > Sphericity or Separate Sphericity Tests.
>>>>> > >> > I am trying to follow example from "car" package, but it seems
>>>>> > >> > that I am not getting something right.
>>>>> > >> >
>>>>> > >> >> Dataset$Sessn <- as.factor(Dataset$Sessn)
>>>>> > >> >
>>>>> > >> >> LinearModel.1 <- lm(Response ~ Sessn*Trtmt, data=Dataset)
>>>>> > >> >
>>>>> > >> >> summary(LinearModel.1)
>>>>> > >> >
>>>>> > >> > All, good so far, but I have problem understanding "idata=" and
>>>>> > >> > "idesign="
>>>>> > >> > functions pertaining to my example.  Session is my repeated
>>>>> > >> > measure (Sessn 1 and Sessn 2 = two sessions, in reality I have
>>>>> > >> > more) and it is already stacked. Any help or guidance on this
>>>>> > >> > matter.
>>>>> > >> >
>>>>> > >> > Thank you, my mock dataset is below.  Each subject has two levels
>>>>> > >> > of treatment throughout four calendar days which are recoded to
>>>>> > >> > Session 1 and Session 2 in order to compare treatments by the
>>>>> > >> > first and subsequent days of exposure (Treatment x Session; my DV
>>>>> > >> > is Response; Session is repeated).
>>>>> > >> >
>>>>> > >> >    Subj Trtmt Sessn Response  1 N 1 5  1 D 1 6  1 N 2 4  1 D 2 7
>>>>> > >> > 2 N 1
>>>>> > >> > 8
>>>>> > >> > 2 D 1 9  2 N 2 2  2 D 2 1  3 N 1 4  3 D 1 5  3 N 2 6  3 D 2 2  4
>>>>> > >> > N 1 5
>>>>> > >> >  4 D
>>>>> > >> > 1 6  4 N 2 4  4 D 2 7  5 N 1 8  5 D 1 9  5 N 2 2  5 D 2 1  6 N 1
>>>>> > >> > 4  6 D
>>>>> > >> > 1 5
>>>>> > >> > 6 N 2 6  6 D 2 2
>>>>> > >> >
>>>>> > >> > Sincerely,
>>>>> > >> >
>>>>> > >> > Sergios Charntikov (Sergey), MA
>>>>> > >> >
>>>>> > >> > Behavioral Neuropharmacology Lab
>>>>> > >> > Department of Psychology
>>>>> > >> > University of Nebraska-Lincoln
>>>>> > >> > Lincoln, NE 68588-0308  USA
>>>>> > >> >
>>>>> > >> > sergioschr-at-gmail-dot-com
>>>>> > >> >
>>>>> > >> >        [[alternative HTML version deleted]]
>>>>> > >> >
>>>>> > >> > ______________________________________________
>>>>> > >> > R-help at r-project.org mailing list
>>>>> > >> > https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> > >> > PLEASE do read the posting guide
>>>>> > >> > http://www.R-project.org/posting-guide.html
>>>>> > >> > and provide commented, minimal, self-contained, reproducible code.
>>>>> > >> >
>>>>> > >> >
>>>>> > >>
>>>>> > >>
>>>>> > >>
>>>>> > >> --
>>>>> > >> Mike Lawrence
>>>>> > >> Graduate Student
>>>>> > >> Department of Psychology
>>>>> > >> Dalhousie University
>>>>> > >>
>>>>> > >> Looking to arrange a meeting? Check my public calendar:
>>>>> > >> http://tr.im/mikes_public_calendar
>>>>> > >>
>>>>> > >> ~ Certainty is folly... I think. ~
>>>>> > >
>>>>> > >
>>>>> >
>>>>> >
>>>>> >
>>>>> > --
>>>>> > Mike Lawrence
>>>>> > Graduate Student
>>>>> > Department of Psychology
>>>>> > Dalhousie University
>>>>> >
>>>>> > Looking to arrange a meeting? Check my public calendar:
>>>>> > http://tr.im/mikes_public_calendar
>>>>> >
>>>>> > ~ Certainty is folly... I think. ~
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>>
>> --
>> Mike Lawrence
>> Graduate Student
>> Department of Psychology
>> Dalhousie University
>>
>> Looking to arrange a meeting? Check my public calendar:
>> http://tr.im/mikes_public_calendar
>>
>> ~ Certainty is folly... I think. ~
>>
>
>
>
> --
> Mike Lawrence
> Graduate Student
> Department of Psychology
> Dalhousie University
>
> Looking to arrange a meeting? Check my public calendar:
> http://tr.im/mikes_public_calendar
>
> ~ Certainty is folly... I think. ~
>



More information about the R-help mailing list