Simple 2-Way Anova issue in R

4 messages Options
Embed this post
Permalink
znd

Simple 2-Way Anova issue in R

Reply Threaded More More options
Print post
Permalink
Hello, I'm new to R and have been following many guides including the two-way anova (http://www.personality-project.org/r/r.anova.html).  Using that walkthrough including the supplied data I do str(data.ex2) and receive the appropriate types of data as follows:
> str(data.ex2)
'data.frame':   16 obs. of  4 variables:
 $ Observation: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Gender     : Factor w/ 2 levels "f","m": 2 2 2 2 2 2 2 2 1 1 ...
 $ Dosage     : Factor w/ 2 levels "a","b": 1 1 1 1 2 2 2 2 1 1 ...
 $ Alertness  : int  8 12 13 12 6 7 23 14 15 12 ...

aov.ex2 = aov(Alertness~Gender*Dosage,data=data.ex2)  

summary(aov.ex2)

Outputs:
              Df  Sum Sq Mean Sq F value Pr(>F)
Gender         1  76.562  76.562  2.9518 0.1115
Dosage         1   5.062   5.062  0.1952 0.6665
Gender:Dosage  1   0.063   0.063  0.0024 0.9617
Residuals     12 311.250  25.938        

However, when I got to use my data that I made in csv format I have to tell R to interpret my factors which are year and depth as factors...
datafilename="C:/Rclass/hmwk1pt2.csv"                            
data.ex2=read.csv(datafilename,header=T)                              
data.ex2$Year<-as.factor(data.ex2$Year)
data.ex2$Depth<-as.factor(data.ex2$Depth)
data.ex2
str(data.ex2)

This outputs what I would expect:

> str(data.ex2)
'data.frame':   12 obs. of  4 variables:
 $ Year      : Factor w/ 3 levels "1999","2000",..: 1 1 1 1 2 2 2 2 3 3 ...
 $ Depth     : Factor w/ 4 levels "10","15","20",..: 1 2 3 4 1 2 3 4 1 2 ...
 $ Replicate1: num  14.3 15.1 16.7 17.3 16.3 17.4 18.6 20.9 22.9 23.9 ...
 $ Replicate2: num  14.7 15.6 16.9 17.9 16.4 17.2 19.6 21.3 22.7 23.3 ...

But something is not causing my anova to carry through...this is what I have.

ANOVA = aov(Replicate1~Year*Depth,data=data.ex2)
summary(ANOVA)

which outputs:

> summary(ANOVA)                                          
            Df  Sum Sq Mean Sq
Year         2 143.607  71.803
Depth        3  17.323   5.774
Year:Depth   6   2.587   0.431

There is no F-value or Pr(>F) columns.

I also can't boxplot this correctly, again following the example at that website above they have:

boxplot(Alertness~Dosage*Gender,data=data.ex2)

which outputs:



My code is:

boxplot(Replicate1~Year*Depth,data=data.ex2)

which outputs:



This is incorrect, it's multiplying my factors but I thought that when I did the str() on my data it recognized the Year and Depth as factors, not numbers or integers.

My csv file is:
hmwk1pt2.csv

Any help on what is going one would be greatly appreciated because I need to perform one-way, two-way, nested, and factorial anovas but I first need to solve this problem before I can continue.

Jeremy Miles-2

Re: Simple 2-Way Anova issue in R

Reply Threaded More More options
Print post
Permalink
If I've understood correctly, you have cell sizes of 1.  This is not enough.

ANOVA compares within group variance to between group variance, and
your within group variances are zero.

You need more data, or to collapse some cells.

Jeremy




2009/11/8 znd <[hidden email]>:

>
> Hello, I'm new to R and have been following many guides including the two-way
> anova (http://www.personality-project.org/r/r.anova.html).  Using that
> walkthrough including the supplied data I do str(data.ex2) and receive the
> appropriate types of data as follows:
>> str(data.ex2)
> 'data.frame':   16 obs. of  4 variables:
>  $ Observation: int  1 2 3 4 5 6 7 8 9 10 ...
>  $ Gender     : Factor w/ 2 levels "f","m": 2 2 2 2 2 2 2 2 1 1 ...
>  $ Dosage     : Factor w/ 2 levels "a","b": 1 1 1 1 2 2 2 2 1 1 ...
>  $ Alertness  : int  8 12 13 12 6 7 23 14 15 12 ...
>
> aov.ex2 = aov(Alertness~Gender*Dosage,data=data.ex2)
>
> summary(aov.ex2)
>
> Outputs:
>              Df  Sum Sq Mean Sq F value Pr(>F)
> Gender         1  76.562  76.562  2.9518 0.1115
> Dosage         1   5.062   5.062  0.1952 0.6665
> Gender:Dosage  1   0.063   0.063  0.0024 0.9617
> Residuals     12 311.250  25.938
>
> However, when I got to use my data that I made in csv format I have to tell
> R to interpret my factors which are year and depth as factors...
> datafilename="C:/Rclass/hmwk1pt2.csv"
> data.ex2=read.csv(datafilename,header=T)
> data.ex2$Year<-as.factor(data.ex2$Year)
> data.ex2$Depth<-as.factor(data.ex2$Depth)
> data.ex2
> str(data.ex2)
>
> This outputs what I would expect:
>
>> str(data.ex2)
> 'data.frame':   12 obs. of  4 variables:
>  $ Year      : Factor w/ 3 levels "1999","2000",..: 1 1 1 1 2 2 2 2 3 3 ...
>  $ Depth     : Factor w/ 4 levels "10","15","20",..: 1 2 3 4 1 2 3 4 1 2 ...
>  $ Replicate1: num  14.3 15.1 16.7 17.3 16.3 17.4 18.6 20.9 22.9 23.9 ...
>  $ Replicate2: num  14.7 15.6 16.9 17.9 16.4 17.2 19.6 21.3 22.7 23.3 ...
>
> But something is not causing my anova to carry through...this is what I
> have.
>
> ANOVA = aov(Replicate1~Year*Depth,data=data.ex2)
> summary(ANOVA)
>
> which outputs:
>
>> summary(ANOVA)
>            Df  Sum Sq Mean Sq
> Year         2 143.607  71.803
> Depth        3  17.323   5.774
> Year:Depth   6   2.587   0.431
>
> There is no F-value or Pr(>F) columns.
>
> I also can't boxplot this correctly, again following the example at that
> website above they have:
>
> boxplot(Alertness~Dosage*Gender,data=data.ex2)
>
> which outputs:
>
> http://old.nabble.com/file/p26258684/87o3uicpf6dt4kkdyvfv.jpeg
>
> My code is:
>
> boxplot(Replicate1~Year*Depth,data=data.ex2)
>
> which outputs:
>
> http://old.nabble.com/file/p26258684/gik02vyhvvbmcvw3ia2h.jpeg
>
> This is incorrect, it's multiplying my factors but I thought that when I did
> the str() on my data it recognized the Year and Depth as factors, not
> numbers or integers.
>
> My csv file is:
> http://old.nabble.com/file/p26258684/hmwk1pt2.csv hmwk1pt2.csv
>
> Any help on what is going one would be greatly appreciated because I need to
> perform one-way, two-way, nested, and factorial anovas but I first need to
> solve this problem before I can continue.
>
>
> --
> View this message in context: http://old.nabble.com/Simple-2-Way-Anova-issue-in-R-tp26258684p26258684.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jeremy Miles
Psychology Research Methods Wiki: www.researchmethodsinpsychology.com

______________________________________________
[hidden email] 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.
znd

Re: Simple 2-Way Anova issue in R

Reply Threaded More More options
Print post
Permalink
Ah, I believe I constructed my *.csv wrong in that I only had 1 observation within groups whereas I needed at least 2.

Originally I had:

Year     Depth     Biomass1     Biomass2
1999     10         14.3           14.7
1999     15         14.7           15.6
etc.

but I switched this to:

Year     Depth     Biomass
1999     10         14.3
1999     10         14.7
1999     15         15.1
1999     15         15.6

I believe this may be the appropriate way to collapse my biomass data in order to perform a 2-way anova.

If not feel free comment and thanks for the help.
jholtman

Re: Simple 2-Way Anova issue in R

Reply Threaded More More options
Print post
Permalink
> x
  Year Depth Biomass1 Biomass2
1 1999    10     14.3     14.7
2 1999    15     14.7     15.6
> require(reshape)
> melt(x, id=c('Year','Depth'))
  Year Depth variable value
1 1999    10 Biomass1  14.3
2 1999    15 Biomass1  14.7
3 1999    10 Biomass2  14.7
4 1999    15 Biomass2  15.6


On Sun, Nov 8, 2009 at 7:38 PM, znd <[hidden email]> wrote:

>
> Ah, I believe I constructed my *.csv wrong in that I only had 1 observation
> within groups whereas I needed at least 2.
>
> Originally I had:
>
> Year     Depth     Biomass1     Biomass2
> 1999     10         14.3           14.7
> 1999     15         14.7           15.6
> etc.
>
> but I switched this to:
>
> Year     Depth     Biomass
> 1999     10         14.3
> 1999     10         14.7
> 1999     15         15.1
> 1999     15         15.6
>
> I believe this may be the appropriate way to collapse my biomass data in
> order to perform a 2-way anova.
>
> If not feel free comment and thanks for the help.
> --
> View this message in context: http://old.nabble.com/Simple-2-Way-Anova-issue-in-R-tp26258684p26259649.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.