-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathR-intro.R
475 lines (349 loc) · 9.17 KB
/
R-intro.R
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#### "All the examples" from ./R-intro.texi
#### -- in a way that this should be(come) an executable script.
options(digits=5, width=65)##--- for outputs !
options(stringsAsFactors=FALSE) ## for R >= 4.0.0
options(useFancyQuotes=FALSE) ## avoid problems on Windows
## 2. Simple Manipulations
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))
c(10.4, 5.6, 3.1, 6.4, 21.7) -> x
.Last.value
1/x
y <- c(x, 0, x)
v <- 2*x + y + 1
##- Warning message:
##- longer object length
##- is not a multiple of shorter object length in: 2 * x + y
sqrt(-17)
##- [1] NaN
##- Warning message:
##- NaNs produced in: sqrt(-17)
sqrt(-17+0i)
###-- 2.3 .. regular sequences
1:30
n <- 10
1:n-1
1:(n-1)
30:1
seq(2,10)
all(seq(1,30) == seq(to=30, from=1))
seq(-5, 5, by=.2) -> s3
s4 <- seq(length=51, from=-5, by=.2)
all.equal(s3,s4)
s5 <- rep(x, times=5)
s6 <- rep(x, each=5)
temp <- x > 13
z <- c(1:3,NA); ind <- is.na(z)
0/0
Inf - Inf
labs <- paste(c("X","Y"), 1:10, sep="")
labs
x <- c(z,z-2)#-- NOT in texi ; more interesting
y <- x[!is.na(x)]
(x+1)[(!is.na(x)) & x>0] -> z
z
x <- c(x, 9:12)# long enough:
x[1:10]
c("x","y")[rep(c(1,2,2,1), times=4)]
y <- x[-(1:5)]
y
fruit <- c(5, 10, 1, 20)
names(fruit) <- c("orange", "banana", "apple", "peach")
fruit
lunch <- fruit[c("apple","orange")]
lunch
x
x[is.na(x)] <- 0
x
y <- -4:9
y[y < 0] <- -y[y < 0]
all(y == abs(y))
y
###---------------
z <- 0:9
digits <- as.character(z)
digits
d <- as.integer(digits)
all.equal(z, d)
e <- numeric()
e[3] <- 17
e
alpha <- 10*(1:10)
alpha <- alpha[2 * 1:5]
alpha
winter <- data.frame(temp = c(-1,3,2,-2), cat = factor(rep(c("A","B"), 2)))
winter
unclass(winter)
###------------ Ordered and unordered factors --------
state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
"qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
"sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa",
"sa", "act", "nsw", "vic", "vic", "act")
statef <- factor(state)
statef
levels(statef)
incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
59, 46, 58, 43)
incmeans <- tapply(incomes, statef, mean)
incmeans
stdError <- function(x) sqrt(var(x)/length(x))
incster <- tapply(incomes, statef, stdError)
incster
##
z <- 1:1500
dim(z) <- c(3,5,100)
x <- array(1:20,dim=c(4,5)) # Generate a 4 by 5 array.
x
i <- array(c(1:3,3:1),dim=c(3,2))
i # @code{i} is a 3 by 2 index array.
x[i] # Extract those elements
x[i] <- 0 # Replace those elements by zeros.
x
n <- 60
b <- 5 ; blocks <- rep(1:b, length= n)
v <- 6 ; varieties <- gl(v,10)
Xb <- matrix(0, n, b)
Xv <- matrix(0, n, v)
ib <- cbind(1:n, blocks)
iv <- cbind(1:n, varieties)
Xb[ib] <- 1
Xv[iv] <- 1
X <- cbind(Xb, Xv)
N <- crossprod(Xb, Xv)
table(blocks,varieties)
all(N == table(blocks,varieties))
h <- 1:17
Z <- array(h, dim=c(3,4,2))
## If the size of 'h' is exactly 24
h <- rep(h, length = 24)
Z. <- Z ## the result is the same as
Z <- h; dim(Z) <- c(3,4,2)
stopifnot(identical(Z., Z))
Z <- array(0, c(3,4,2))
## So if @code{A}, @code{B} and @code{C} are all similar arrays
## <init>
A <- matrix(1:6, 3,2)
B <- cbind(1, 1:3)
C <- rbind(1, rbind(2, 3:4))
stopifnot(dim(A) == dim(B),
dim(B) == dim(C))
## <init/>
D <- 2*A*B + C + 1
a <- 1:9
b <- 10*(1:3)
ab <- a %o% b
stopifnot(ab == outer(a,b,"*"),
ab == outer(a,b))
x <- 1:10
y <- -2:2
f <- function(x, y) cos(y)/(1 + x^2)
z <- outer(x, y, f)
d <- outer(0:9, 0:9)
fr <- table(outer(d, d, "-"))
plot(fr, xlab="Determinant", ylab="Frequency")
##
B <- aperm(A, c(2,1))
stopifnot(identical(B, t(A)))
## for example, @code{A} and @code{B} are square matrices of the same size
## <init>
A <- matrix(1:4, 2,2)
B <- A - 1
## <init/>
A * B
A %*% B
## <init>
x <- c(-1, 2)
## <init/>
x %*% A %*% x
x %*% x
stopifnot(x %*% x == sum(x^2))
xxT <- cbind(x) %*% x
xxT
stopifnot(identical(xxT, x %*% rbind(x)))
## crossprod ... (ADD)
## diag ... (ADD)
## linear equations ... (ADD)
## solve ... (ADD)
## eigen:
## a symmetric matrix @code{Sm}
## <init>
Sm <- matrix(-2:6, 3); Sm <- (Sm + t(Sm))/4; Sm
## </init>
ev <- eigen(Sm)
evals <- eigen(Sm)$values
## SVD .....
## "if M is in fact square, then, ..."
## <init>
M <- cbind(1,1:3,c(5,2,3))
X <- cbind(1:9, .25*(-4:4)^2)
X1 <- cbind(1:7, -1)
X2 <- cbind(0,2:8)
y <- c(1:4, 2:6)
## </init>
absdetM <- prod(svd(M)$d)
stopifnot(all.equal(absdetM, abs(det(M))))# since det() nowadays exists
ans <- lsfit(X, y)
Xplus <- qr(X)
b <- qr.coef(Xplus, y)
fit <- qr.fitted(Xplus, y)
res <- qr.resid(Xplus, y)
##
X <- cbind(1, X1, X2)
vec <- as.vector(X)
vec <- c(X)
statefr <- table(statef)
statefr
statefr <- tapply(statef, statef, length)
statefr
factor(cut(incomes, breaks = 35+10*(0:7))) -> incomef
table(incomef,statef)
###--- @chapter 6. Lists and data frames
Lst <- list(name="Fred", wife="Mary", no.children=3,
child.ages=c(4,7,9))
Lst$name
Lst$wife
Lst$child.ages[1]
stopifnot(Lst$name == Lst[[1]], Lst[[1]] == "Fred",
Lst$child.ages[1] == Lst[[4]][1], Lst[[4]][1] == 4
)
x <- "name" ; Lst[[x]]
## @section 6.2 Constructing and modifying lists
##<init>
Mat <- cbind(1, 2:4)
##</init>
Lst[5] <- list(matrix=Mat)
## @section 6.3 Data frames
accountants <- data.frame(home=statef, loot=incomes, shot=incomef)
## MM: add the next lines to R-intro.texi !
accountants
str(accountants)
## ..........
###--- @chapter 8. Probability distributions
## 2-tailed p-value for t distribution
2*pt(-2.43, df = 13)
## upper 1% point for an F(2, 7) distribution
qf(0.01, 2, 7, lower.tail = FALSE)
attach(faithful)
summary(eruptions)
fivenum(eruptions)
stem(eruptions)
hist(eruptions)
## <IMG> postscript("images/hist.eps", ...)
# make the bins smaller, make a plot of density
hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE)
lines(density(eruptions, bw=0.1))
rug(eruptions) # show the actual data points
## dev.off() <IMG/>
plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)
## <IMG> postscript("images/ecdf.eps", ...)
long <- eruptions[eruptions > 3]
plot(ecdf(long), do.points=FALSE, verticals=TRUE)
x <- seq(3, 5.4, 0.01)
lines(x, pnorm(x, mean=mean(long), sd=sqrt(var(long))), lty=3)
## dev.off() <IMG/>
par(pty="s") # arrange for a square figure region
qqnorm(long); qqline(long)
x <- rt(250, df = 5)
qqnorm(x); qqline(x)
qqplot(qt(ppoints(250), df = 5), x, xlab = "Q-Q plot for t dsn")
qqline(x)
shapiro.test(long)
ks.test(long, "pnorm", mean = mean(long), sd = sqrt(var(long)))
##@section One- and two-sample tests
## scan() from stdin :
## can be cut & pasted, but not parsed and hence not source()d
##scn A <- scan()
##scn 79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97
##scn 80.05 80.03 80.02 80.00 80.02
A <- c(79.98, 80.04, 80.02, 80.04, 80.03, 80.03, 80.04, 79.97,
80.05, 80.03, 80.02, 80, 80.02)
##scn B <- scan()
##scn 80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97
B <- c(80.02, 79.94, 79.98, 79.97, 79.97, 80.03, 79.95, 79.97)
## <IMG> postscript("images/ice.eps", ...)
boxplot(A, B)
## dev.off() <IMG/>
t.test(A, B)
var.test(A, B)
t.test(A, B, var.equal=TRUE)
wilcox.test(A, B)
plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B))
plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE)
###--- @chapter Grouping, loops and conditional execution
###--- @chapter Writing your own functions
###--- @chapter Statistical models in R
###--- @chapter Graphical procedures
###--- @appendix A sample session
## "Simulate starting a new R session, by
rm(list=ls(all=TRUE))
set.seed(123) # for repeatability
if(interactive())
help.start()
x <- rnorm(50)
y <- rnorm(x)
plot(x, y)
ls()
rm(x, y)
x <- 1:20
w <- 1 + sqrt(x)/2
dummy <- data.frame(x = x, y = x + rnorm(x)*w)
dummy
fm <- lm(y ~ x, data=dummy)
summary(fm)
fm1 <- lm(y ~ x, data=dummy, weight=1/w^2)
summary(fm1)
attach(dummy)
lrf <- lowess(x, y)
plot(x, y)
lines(x, lrf$y)
abline(0, 1, lty=3)
abline(coef(fm))
abline(coef(fm1), col = "red")
detach()# dummy
plot(fitted(fm), resid(fm),
xlab="Fitted values",
ylab="Residuals",
main="Residuals vs Fitted")
qqnorm(resid(fm), main="Residuals Rankit Plot")
rm(fm, fm1, lrf, x, dummy)
filepath <- system.file("data", "morley.tab" , package="datasets")
if(interactive()) file.show(filepath)
mm <- read.table(filepath)
mm
mm$Expt <- factor(mm$Expt)
mm$Run <- factor(mm$Run)
attach(mm)
plot(Expt, Speed, main="Speed of Light Data", xlab="Experiment No.")
fm <- aov(Speed ~ Run + Expt, data=mm)
summary(fm)
fm0 <- update(fm, . ~ . - Run)
anova(fm0, fm)
detach()
rm(fm, fm0)
x <- seq(-pi, pi, len=50)
y <- x
f <- outer(x, y, function(x, y) cos(y)/(1 + x^2))
oldpar <- par(no.readonly = TRUE)
par(pty="s")
contour(x, y, f)
contour(x, y, f, nlevels=15, add=TRUE)
fa <- (f-t(f))/2
contour(x, y, fa, nlevels=15)
par(oldpar)
image(x, y, f)
image(x, y, fa)
objects(); rm(x, y, f, fa)
th <- seq(-pi, pi, len=100)
z <- exp(1i*th)
par(pty="s")
plot(z, type="l")
w <- rnorm(100) + rnorm(100)*1i
w <- ifelse(Mod(w) > 1, 1/w, w)
plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="+",xlab="x", ylab="y")
lines(z)
w <- sqrt(runif(100))*exp(2*pi*runif(100)*1i)
plot(w, xlim=c(-1,1), ylim=c(-1,1), pch="+", xlab="x", ylab="y")
lines(z)
rm(th, w, z)
## q()