---
output:
  html_document:
    fig_height: 3
    fig_width: 3.5
    keep_md: yes
---

See the Rmd file here: https://raw.githubusercontent.com/ralfer/apa_format_and_misc/master/example/example.Rmd

```{r, cache=F}
library(apastats)
load.libs(c('data.table','knitr'))
opts_chunk$set(message=F)
```
```{r}
t_res<-t.test(rnorm(20, mean = 10, sd=2))

t_res
```

One sample Student's t-test demonstrated that X significantly higher than zero, `r describe.ttest(t_res)`.

```{r}
data("sleep")
describe.mean.and.t(sleep$extra, sleep$group, which.mean = 3, paired=T)

```

Student's t-test showed that increase in hours of sleep compared to control was lower in group 1 than in group 2, `r with(sleep, describe.mean.and.t( extra, group,which.mean = 3, paired=T))`. 


```{r}
require(MASS)
glm_res<-glm(Freq ~ (Age+Sex)*Survived, family = poisson, data = data.frame(Titanic))

describe.glm(glm_res)

lm_res<-lm(Postwt ~ Prewt + Treat + offset(Prewt), anorexia)
describe.glm(lm_res)
```

```{r}
describe.glm(glm_res, "SexFemale:SurvivedYes", dtype=1)
describe.glm(glm_res, "SexFemale:SurvivedYes", dtype=2)
describe.glm(glm_res, "SexFemale:SurvivedYes", dtype=3)
describe.glm(glm_res, "SexFemale:SurvivedYes", dtype=3, test.df=T)
```

Adults had lower chances of survival on Titanic than children, `r describe.glm(glm_res, "AgeAdult:SurvivedYes")`, and women had higher chances than men, `r describe.glm(glm_res, "SexFemale:SurvivedYes", dtype=3)`. 

```{r}
require(ez)
data(ANT)

rt_anova = ezANOVA(
    data = ANT[ANT$error==0,]
    , dv = rt
    , wid = subnum
    , within = .(cue,flank)
    , between = group
)

rt_anova
```

Repeated measures ANOVA demonstrated a significant influence of cue on RT, `r describe.ezanova(rt_anova, 'cue')`. 

### Plotting pointrange plots

You can plot within-subject CIs with the help of summarySEwithin from http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_%28ggplot2%29/

```{r, pointrange_plots, message=F, cache=F}
require(nlme)
require(gridExtra)

data(ergoStool)

p1<-plot.pointrange(ergoStool, aes(x=Type, y=effort))+ggtitle('Usual CIs')
p2<-plot.pointrange(ergoStool, aes(x=Type, y=effort), within_subj=T, wid='Subject')+ggtitle('Within-subject CIs')
grid.arrange(p1, p2, ncol=2)
```

### Showing log-transformed data back untransformed

Sometimes it is useful to analyze data log-transformed (e.g., in reaction time analyses), but to show it untransformed. _scale_y_exp_ comes in handy.

```{r}
library(scales)
temp <- tempfile()
download.file("http://cogjournal.org/2/1/files/ChetverikovRJCS2015SOMdata.zip",temp)
faces <- data.table(read.csv(unz(temp, "faces_data.csv"), header = T))
unlink(temp)

faces[,logAT:=log(answerTime)]

p0<-plot.pointrange(faces[correct==1, ], aes(x=user_gender, color=stim_gender, y=logAT), wid='uid')+ylab('Log RT')

p0

p1<-plot.pointrange(faces[correct==1, ], aes(x=user_gender, color=stim_gender, y=logAT), wid='uid')+scale_y_exp(digits=2)+labs(x="Participant's gender", color="Face Gender", y='Untransformed RT')

p1
```

### Sharing legend and axis title between plots

It is not very easy to share legend or axis title between plots. I modified a function from https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs to make it easier. 

```{r, cache=F}
p2<-plot.pointrange(faces, aes(x=user_gender, color=stim_gender, y=correct), wid='uid')+labs(x="Participant's gender", color="Face Gender" ,y='Accuracy')

p2
```
```{r, fig.height=7}
grid_arrange_shared_legend(p1 + theme(legend.direction="horizontal"),p2)
```
```{r, fig.width=7}
grid_arrange_shared_legend(p1, p2, stack = 'horizontal')
grid_arrange_shared_legend(p1, p2, stack = 'horizontal', one_sub = T)
```
