I am writing functions to output ANOVA as output
I did not understand how to output anova object from the following information:
# degrees of freedom
repdf = 1
trtdf = 22
totaldf = 23
# sum of square
ssrep = 10.3
sstrt = 14567.2
sstotal = 14577.2
Is anova object dataframe or list or there other special programming category?
Edits: based on the suggestion below from Ben
Source <- c("replication", "Treatments", "Total")
Df <- c(repdf, trtdf, totaldf)
"Sum Sq" <- c(ssrep, sstrt, sstotal)
anovadf <- data.frame(Source, Df, "Sum Sq")
class(anovadf) <- c("anova","data.frame")
Does not give me what str of the anova object should look like? Any further help
> str(anovadf)
Classes ‘anova’ and 'data.frame': 3 obs. of 3 variables:
$ Source : Factor w/ 3 levels "Error","replication",..: 2 3 1
$ Df : num 1 22 23
$ X.Sum.Sq.: Factor w/ 1 level "Sum Sq": 1 1 1
Create an
anovaobject, save it, then usestr()on the results. From thelm.D9object created byexample("lm"):So it’s a special case of a data frame. Construct your data frame
ato match the example and then try assigning the class:class(a) <- c("anova","data.frame").In particular:
You have to be a little bit careful with the column names — they have to protected by backticks, and you have to use
check.names=FALSE, because they are not legal variable names (they contain spaces). You could add the F statistic and P value to this — I didn’t because I wasn’t sure what the appropriate error term was.