I am running a lot of ANOVA tables and would ultimately like to run them through the 'xtable' function in the xtable package for export into LaTeX. However I need to supply xtable with a table object, it will not accept an ANOVA object. I basically want to make the ANOVA table as a table object. Here is some reproducable code:
utils::data(npk, package="MASS")
npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)
summary(npk.aovE) ## THIS IS THE TABLE I WANT AS A TABLE OBJECT
I have tried all the usual suspects (as.table, print and xtable(summary(npk.aoE))) with no success. Any help would be greatly appreciated
Generally what people want is the matrix obtained with:
coef( summary(npk.aovE) ) # which returns NULL
As the help page says: "Function coef will extract the matrix of coefficients with standard errors, t-statistics and p-values." Unfortunately theory and practice do not always agree. That summary object is actually two dataframes and its behavior is described on ?summary.aovlist :
> summary(npk.aovE)[[2]]
Df Sum Sq Mean Sq F value Pr(>F)
N 1 189.28 189.28 12.259 0.00437 **
P 1 8.40 8.40 0.544 0.47490
K 1 95.20 95.20 6.166 0.02880 *
N:P 1 21.28 21.28 1.378 0.26317
N:K 1 33.14 33.14 2.146 0.16865
P:K 1 0.48 0.48 0.031 0.86275
Residuals 12 185.29 15.44
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> summary(npk.aovE)[[1]]
Df Sum Sq Mean Sq F value Pr(>F)
N:P:K 1 37.0 37.00 0.483 0.525
Residuals 4 306.3 76.57
Related
I am trying to run 2x2 mixed ANOVA with unequal sample size using R. The data was collected from 30 individuals with two different conditions (i.e., 2 levels of within factor), and they were allocated by k-means clustering analysis (i.e., # of group 1 = 11, # of group 2 = 19). Here is a sample of my code and the output:
summary(aov(JH ~ Box+Group+Box:Group+Error(P/Box), data = d3))
Error: P
Df Sum Sq Mean Sq F value Pr(>F)
Group 1 0.027 0.02715 1.56 0.22
Box:Group 1 0.001 0.00078 0.04 0.83
Residuals 27 0.470 0.01741
Error: P:Box
Df Sum Sq Mean Sq F value Pr(>F)
Box 1 0.000000 1.00e-07 0.00 0.97
Group 1 0.000022 2.17e-05 0.24 0.63
Box:Group 1 0.000032 3.24e-05 0.35 0.56
Residuals 27 0.002488 9.21e-05
Unlike the output when I ran 2x2 mixed ANOVA with equal sample sizes using another dataset (the output attached below), I had another interaction effect under Error: P.
summary(aov(HipLoadingK ~ Box*Sex+Error(P/Box), data = TW))
Error: P
Df Sum Sq Mean Sq F value Pr(>F)
Sex 1 0.02578 0.025779 8.038 0.00841 **
Residuals 28 0.08980 0.003207
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Error: P:Box
Df Sum Sq Mean Sq F value Pr(>F)
Box 1 0.003454 0.003454 4.271 0.0481 *
Box:Sex 1 0.000066 0.000066 0.082 0.7765
Residuals 28 0.022645 0.000809
Is this result fine as long as I interpret appropriate results for each between/within main effects and interaction effect? Or should I edit my code to get the correct outputs? If so, please let me know what should be added or edited in my code.
When I change the dataset from a short format to a long format, I accidentally put the different order of group name for each participant. For instance, P01 was group A in X condition, but his or her group was B in Y condition. After I fixed the code, it worked well.
I'm building a repeated measures ANCOVA using a multi-level framework through the AOV package. I have one continuous response variable, two factor predictors, and 3 continuous covariates. My script for the model is below:
ModelDV <- aov(DV ~ IV1 + IV2 + IV1*IV2 + CV1 + CV2 + CV3 + Error(PartID/(IV1 + IV2 + IV1:IV2)), data)
A snippet of my data set shows how it is formatted:
PartID DV IV1 IV2 CV1 CV2 CV3
1 56 CondA1 CondB1 Contunous values
2 45 CondA2 CondB2 -
3 32 CondA3 CondB1 -
4 21 CondA4 CondB2 -
1 10 CondA1 CondB1 -
2 19 CondA2 CondB2 -
3 35 CondA3 CondB1 -
4 45 CondA4 CondB2 -
My condiitons are embedded in the error term of the participant ID since this is a fully within repeated measures model.
I am attempting to conduct a pairwise analysis on these values. My output provides omnibus F-tests:
Error: PartID
Df Sum Sq Mean Sq F value Pr(>F)
CV1 1 348 348 0.442 0.5308
CV2 1 9 9 0.011 0.9193
CV3 1 3989 3989 5.063 0.0654 .
Residuals 6 4727 788
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Error: PartID:IV1
Df Sum Sq Mean Sq F value Pr(>F)
IV1 1 6222 6222 17.41 0.0024 **
Residuals 9 3217 357
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Error: PartID:IV2
Df Sum Sq Mean Sq F value Pr(>F)
IV2 2 6215 3107.7 16.18 9.51e-05 ***
Residuals 18 3457 192.1
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Error: PartID:IV1:IV2
Df Sum Sq Mean Sq F value Pr(>F)
IV1:IV2 2 575.2 287.6 1.764 0.2
Residuals 18 2934.4 163.0
When calculating emmeans via:
emm<-emmeans(Model, ~ IV1)
pairs(emm)
I get a sensible output.
However, when using this for the covariates:
emm<-emmeans(Model, ~ CV1)
pairs(emm)
I get the following output:
contrast estimate SE df z.ratio p.value
(nothing) nonEst NA NA NA NA
Results are averaged over the levels of: IV1, IV2
What am I doing wrong here that a pairwise comparison is not working for the covariate?
Short answer is because you have made them covariates to control for them and not to consider them as part of the explanation for your model. You of course could do pairwise comparisons for the covariates outside the model but not inside the model framework. Longer blogpost using these tools I wrote here...
I ran a one way ANOVA with repeated measures:
aov <- aov(score ~ group*time + Error(subject/time), data=valueAest)
However, the result is hard to interpret as to whether the group difference comes from before treatment, after treatment, or both?
Error: subject
Df Sum Sq Mean Sq F value Pr(>F)
group 1 12.01 12.013 4.424 0.0421 *
Residuals 38 103.17 2.715
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Error: subject:time
Df Sum Sq Mean Sq F value Pr(>F)
time 1 0.012 0.0125 0.029 0.866
group:time 1 0.013 0.0125 0.029 0.866
Residuals 38 16.475 0.4336
so I decided to run some post-hoc analysis. Looking at some answers from other people, I used the multcomp library:
lme_score = lme(score ~ group, data=valueAest, random = ~1|subject)
anova(lme_score)
require(multcomp)
summary(glht(lme_score, linfct=mcp(group = "Tukey")), test = adjusted(type = "bonferroni"))
but this gave me error of ‘ncol(linfct)’ is not equal to ‘length(coef(model))’.
I also tried:
Mixed_Fitted_Interaction<-emmeans(aov, ~group|time)
Mixed_Fitted_Interaction
# pairwise comparison
pairs(Mixed_Fitted_Interaction)
This gave me the following result:
time = recreation1:
contrast estimate SE df t.ratio p.value
map - VR -0.75 0.397 49.8 -1.890 0.0646
time = recreation2:
contrast estimate SE df t.ratio p.value
map - VR -0.80 0.397 49.8 -2.016 0.0492
This looks promising in my naive mind, can I draw the conclusion that the significance actually doesn't come from treatment, but from the population difference?
In the example data set found below I want to calculate the proportion of variance in science explained by each independent variable using linear regression model. How could I achieve that in R?
hsb2 <- read.table('http://www.ats.ucla.edu/stat/r/modules/hsb2.csv', header=T, sep=",")
m1<-lm(science ~ math+female+ socst+ read, data =hsb2)
One of the ways is to use anova() function from stats package.
It gives you the residual sum of squares explained by each variable and total sum of squares (i.e. variance)
anova(m1)
Analysis of Variance Table
Response: science
Df Sum Sq Mean Sq F value Pr(>F)
math 1 7760.6 7760.6 151.8810 < 2.2e-16 ***
female 1 233.0 233.0 4.5599 0.033977 *
socst 1 465.6 465.6 9.1128 0.002878 **
read 1 1084.5 1084.5 21.2254 7.363e-06 ***
Residuals 195 9963.8 51.1
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
I'm quite new to R but I've tried recently to make a two way repeated measures ANOVA, to replicate the results that my supervisor did on SPSS.
I've struggled for days and read dozens of articles to understand what was going on in R, but I still don't get the same results.
> mod <- lm(Y~A*B)
> Anova(mod, type="III")
Anova Table (Type III tests)
Response: Y
Sum Sq Df F value Pr(>F)
(Intercept) 0.000 1 0.0000 1.00000
A 2.403 5 8.6516 4.991e-08 ***
B 0.403 2 3.6251 0.02702 *
A:B 1.220 10 2.1962 0.01615 *
Residuals 51.987 936
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
My data are from a balanced design, I used the type III SS since it's the one used in SPSS as well. The Sum squares the Df, and the linear model are the same in SPSS, the only things that differ being the F and p value. Thus, it should not be a Sum square mistake.
Results in SPSS are:
F Sig.
A 7.831 .000
B 2.681 .073
A:B 2.247 .014
I'm a little bit lost. Would it be a problem related to the contrasts?
Lucas