

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


example() apropos() help.search()
In order to use functionalities from a certain package, we need to first install and then load the package:
install.packages("")
require(); library()
Most of the functions that we need for this course uses a formula syntax:
goal(y ~ x | z, data = mydata, ...)
where goal may be a function for plotting, calculat- ing numerical summaries or making inference.
For plots:
For other things:
‘y ~^ x | z’ can usually be read ‘y is modeled by (or depends on) x differently for each z’.
These functions from the mosaic-package uses a for- mula syntax.
favstats() # min/max, median, etc. tally() # tabulate data mean() median() sd() # standard deviation cor() # correlation
gf_boxplot() gf_point() # scatter plot gf_histogram() gf_bar() # bar graph
mplot(HELPrct)# different plots splom() # matrix of scatter plots
plotDist() # plot theoretical distribution pdist() # find prob. from percentile qdist() # find percentile from prob.
t.test() # t-test binom.test() # binomial (exact) test prop.test() # approximate test fisher.test() # Fisher's exact test cor.test() # correlation test chisq.test() # chi-square test
model <- lm() # fit linear model summary(model) # model fit summary coef(model) # estimated parameters confint(model) # CI for estimates anova(model) # F-tests, etc. drop1(model) rstudent(model) # Studentized residuals fitted(model) # fitted values plotModel(model)# plot regression lines
model <- glm() # generalized linear model
read.file(); read.delim(); read.csv()
nrow(); ncol() # data dimensions head() # extract first part of data tail() # extract last part of data colnames() # column names rownames() # row names summary()
subset() # subset data by condition factor() # create grouping variable relevel() # change reference level cut() # cut numeric into intervals round() # rounding numbers c() # concatenate numerics seq() # create sequence with() aggregate() margin.table() # sum table entries
The following are examples of how some of the func- tions work (based on mosaic’s built-in data set, HELPrct). We assume mosaic is already loaded. In some chunks only the code and not the output is shown (to see the output, copy-paste the code chunk of interest into your console).
tally(sex ~ substance, data = HELPrct)
substance sex alcohol cocaine heroin female 36 41 30 male 141 111 94
mean(age ~ sex, data = HELPrct)
female male 36.25 35.
favstats(age ~ sex, data = HELPrct)
sex min Q1 median Q3 max mean sd 1 female 21 31 35 40.5 58 36.25 7. 2 male 19 30 35 40.0 60 35.47 7. n missing 1 107 0 2 346 0
gf_boxplot(age ~ substance | sex, data = HELPrct)
female male
alcohol cocaine heroin alcohol cocaine heroin
20
30
40
50
60
substance
age
gf_point(pcs~age, col = ~sex, data = HELPrct) %>% gf_lm() %>% gf_labs(x = "Age", y = "Physical score", title = "My first scatter plot")
20
40
60
20 30 40 50 60 Age
Physical score
sex female male
My first scatter plot
Note: gf point creates the scatter plot, gf lm adds regression lines and gf labs adds a title and change axis labels.
binom.test(~sex, p = 0.5, data = HELPrct)
t.test(age ~ sex, data = HELPrct)
tab <- tally(homeless ~ sex, data = HELPrct) chisq.test(tab)
prop.test(homeless ~ sex, data = HELPrct)
mod1 <- lm(pcs ~ age + substance, data = HELPrct) mod2 <- lm(pcs ~ substance, data = HELPrct) anova(mod1, mod2)
Analysis of Variance Table
Model 1: pcs ~ age + substance Model 2: pcs ~ substance Res.Df RSS Df Sum of Sq F Pr(>F) 1 449 47517 2 450 50139 -1 -2623 24.8 9.2e- Illustration of how the functions pdist and qdist works:
qdist("norm", p = 0.95, mean = 0, sd = 1)
[1] 1.
−2 0 2
density
(^) probability A: 0. B: 0.
pdist("norm", q = -1.5, mean = 0, sd = 1)
[1] 0.
−2 0 2
density
(^) probability A: 0. B: 0.