-
Notifications
You must be signed in to change notification settings - Fork 1
/
BeNice_demo.Rmd
445 lines (252 loc) · 8.85 KB
/
BeNice_demo.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
---
title: "BeNice demo"
author:
- "Pedro Cardoso-Leite"
- "Albert Buchard"
date: "2 Feb 2016"
tags: [nothing, nothingness]
abstract:
This is the abstract.
It has 2 paragraphs.
output:
html_document:
theme: spacelab
highlight: tango
css: BeNice.css
toc: true
toc_float: true
fig_captions: yes
code_folding: hide
fontsize: 12pt
geometry: margin=4in
---
```{r global_options, include=FALSE}
# change knitr's default settings
knitr::opts_chunk$set(fig.width=5, fig.height=5, fig.path='figs/',
echo=FALSE, warning=FALSE, message=FALSE)
# themes: http://bootswatch.com/
# not all themes are valid.
# “default”, “cerulean”, “journal”, “flatly”, “readable”, “spacelab”, “united”, “cosmo”
# spacelab
# readable
# cosmo
# theme specifies the Bootstrap theme to use for the page (themes are drawn from the Bootswatch theme library). Valid themes include "default", "cerulean", "journal", "flatly", "readable", "spacelab", "united", "cosmo", "lumen", "paper", "sandstone", "simplex", and "yeti". Pass null for no theme (in this case you can use the css parameter to add your own styles).
#
# highlight specifies the syntax highlighting style. Supported styles include "default", "tango", "pygments", "kate", "monochrome", "espresso", "zenburn", "haddock", and "textmate". Pass null to prevent syntax highlighting.
# great resource:
# http://rmarkdown.rstudio.com/html_document_format.html
```
```{r functions, include=FALSE}
# A function for captioning and referencing figures and tables
# from http://stackoverflow.com/questions/13848137/figure-captions-references-using-knitr-and-markdown-to-html
figM <- local({
i <- 0
ref <- list()
list(
cap=function(refName, text) {
i <<- i + 1
ref[[refName]] <<- i
paste("**Figure ", i, ":** ", text, sep="")
},
ref=function(refName) {
ref[[refName]]
})
})
tabM <- local({
i <- 0
ref <- list()
list(
cap=function(refName, text) {
i <<- i + 1
ref[[refName]] <<- i
paste("**Table ", i, ":** ", text, sep="")
},
ref=function(refName) {
ref[[refName]]
})
})
# examples are shown below
```
# print function
```{r echo = F}
source('printTest.R', verbose = F)
```
```{r loadStuff, echo = F, include=F}
library(ggplot2)
library(texreg)
# library(xtable)
# http://yihui.name/printr/
# https://github.com/yihui/printr
# install.packages(
# 'printr',
# type = 'source',
# repos = c('http://yihui.name/xran', 'http://cran.rstudio.com')
# )
library(printr)
summary(cars)
```
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r, echo=T}
x <- seq(1, 10, 1)
```
```{r fig_cars, echo=FALSE, fig.cap=figM$cap("fig_cars", "Here you see some interesting stuff about cars and such."), fig.align='center'}
plot(cars)
```
```{r tab1, echo=FALSE, fig.cap=tabM$cap("tab1", "Here you see some interesting stuff about cars and such."), fig.align='center'}
# plot(cars)
# library(texreg)
# htmlreg(table(rbinom(10, 1, .5)))
table(rbinom(10, 1, .5))
```
```{r tab2, echo=FALSE, fig.cap=tabM$cap("tab2", "Here you see some interesting stuff about cars and such."), fig.align='center'}
t1 <- table(rbinom(10, 1, .5))
library(xtable)
# fm3.table <- xtable(glm.fit)
fm3.table <- xtable(t1)
# Coefficients
print(fm3.table, type = "html")
```
```{r tab3, echo=FALSE, fig.cap=tabM$cap("tab3", "Here you see some interesting stuff about cars and such."), fig.align='center'}
plot(cars)
```
```{r fig_cars2, echo=FALSE, fig.cap=figM$cap("fig_cars2", "Here you see some interesting stuff about cars and such."), fig.align='center'}
plot(cars)
```
```{r}
help.search('contourplot')
```
## Referencing to figure or table {#identifier .class1 .class2 key=value key=value}
What you always wanted to know about cars is shown in table `r tabM$ref("tab1")` or figure `r figM$ref("fig_cars2")`
## Tables
### GLM output
```{r echo=T}
cuse <- read.table("http://data.princeton.edu/wws509/datasets/cuse.dat", header=TRUE)
lrfit <- glm( cbind(using, notUsing) ~ age + education + wantsMore , family = binomial, data=cuse)
```
#### no formatting
```{r glm_fit, echo=FALSE, fig.cap=tabM$cap("glm_fit", "Output of glm."), fig.align='center'}
lrfit
```
```{r glm_fit_summary, echo=FALSE, fig.cap=tabM$cap("glm_fit_summary", "Summary of glm output."), fig.align='center'}
summary(lrfit)
```
#### texreg
```{r glm_fit_texreg, echo=FALSE, results='asis'}
htmlreg(lrfit,single.row=TRUE, caption = tabM$cap("glm_fit_texreg", "texreg table output."))
```
Looks good but not enough information.
#### xtable
```{r LvsN_glm, echo=FALSE, results="asis"}
library(xtable)
fm3.table <- xtable(lrfit, caption = tabM$cap("LvsN_glm","xtable output."))
# Coefficients
print(fm3.table, type = "html")
# Analysis of variance.
# print(xtable(anova(lrfit)), type = "html")
```
Requires some css code to make it look nice.
#### ktable
```{r LvsN_glm2, echo=FALSE}
library(xtable)
lrfit.table <- xtable(lrfit)
knitr::kable(lrfit.table,
digits = 2,
caption = tabM$cap("LvsN_glm2","ktable output."))
##
```
Probably the best off the shelf solution.
## Tests
When considering all the coefficients from our glm we observe that none of them are significant (all p > `r paste(pBiggerThan(summary(lrfit)$coefficients[-1,4]))`).
```{r}
x <- rnorm(100, 110, 10)
y <- x+rnorm(100, 10, 10)
cxy <- cor.test(x,y)
txy <- t.test(x,y, paired = T)
```
We observe that the average value of x was `r paste(print_sem(x))` and the average of y was `r paste(print_sem(x))`. This difference was not significant `r paste( print.t.test(txy, units="ms"))`.
We observed however a significant correlation between x and y `r paste(print.cor.test(cxy))`.
## PCA
```{r pca_tab1, echo=FALSE, results='asisi'}
# create some data for the pca
nSubj <- 200
nVar <- 21
varNames <- paste0("Variable.", 1:nVar) # can't contain spaces
X <- as.data.frame(matrix(rnorm(nSubj*nVar), nSubj, nVar))
names(X) <- varNames
# run the pca with varimax:
library(psych)
pc <- principal(X, nfactors = 7, rotate = "varimax")
# work on the output:
pc.table <- principal.print(pc)
# pc.table
# tt <- pc.table
# typeof(pc.table)
# tt[2] <- gsub("@b", "\textbf{", tt[2])
knitr::kable(pc.table,
digits = 2,
caption = tabM$cap("pca_table","Loadings from the PCA (7 factors + varimax rotation). Explanations on the variables: Ufov.BiasForCenter = performance on center – performance on periphery; the higher the score on BiasForCenter the better your performance on the central task relative to the peripheral task.")
)
# remove variables below a certain value.
# ?strsplit
# paste(pc.out[5], pc.out[6], sep = '\\')
# # function to remove multiple spaces
# trim <- function(x) return(gsub("^ *|(?<= ) | *$", "", x, perl=T))
#
# # replace space by " & "
# tt <- trim(pc.out[5])
# gsub(" ", " & ", tt)
#
#
# t0 <- paste0('\begin{table}[ht]
# \centering
# \begin{tabular}{', paste0(rep('r', 8), collapse = ''),'}\hline')
#
# # remove more than 1 spaces
#
#
# aa <- gregexpr(" ", pc.out[5])
# aa[[1]]
#
#
# ttt <- c('\begin{table}[ht]
# \centering
# \begin{tabular}{rrrrr}
# \hline
# & Estimate & Std. Error & z value & Pr($>$$|$z$|$) \\
# \hline
# (Intercept) & -0.3120 & 0.0888 & -3.52 & 0.0004 \\
# motivation & 0.0287 & 0.0887 & 0.32 & 0.7465 \\
# intrusion & -0.1147 & 0.0886 & -1.30 & 0.1952 \\
# VSability & 0.1419 & 0.0886 & 1.60 & 0.1092 \\
# genderCov & 0.0053 & 0.0876 & 0.06 & 0.9519 \\
# brainState & 0.2393 & 0.0919 & 2.60 & 0.0092 \\
# eduCoCo & 0.2225 & 0.0910 & 2.44 & 0.0145 \\
# singleTasking & -0.0004 & 0.0874 & -0.00 & 0.9962 \\
# \hline
# \end{tabular}
# \end{table}')
```
## Beta regression:
```{r}
# generate data
# x <- seq(from=0.01, to = 0.99, len = 100)
# y <- dbeta(x, 10, 10)
# plot(x,y, type = 'l')
# y <- dbeta(x, 20, 10)
# plot(x,y, type = 'l')
X1 <- rnorm(100)
X2 <- rnorm(100)
S <- 2*X1 + 3 * X2
S <- S - min(S)
y <- rbeta(length(S), shape1 = S, shape2 = 10)
if (any(y==0) | any(y==1)) y <- prior.beta(y)
tmp <- data.frame(y, X1, X2)
library(betareg)
beta.fit <- betareg(y~., link="probit", data=tmp)
beta.table <- beta.print(beta.fit)
knitr::kable(beta.table,
digits = 2,
caption = tabM$cap("beta_table","Coeffs from beta regression.")
)
```