-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions2.r
174 lines (138 loc) · 4.12 KB
/
functions2.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
#f <- function(<arguments>) {
# ## something
#}
## for arguments
# 1. exact match for named
# 2. partial match
# 3. positional match
# named arguments with possible default values
f <- function(a,b,c=1) {
## something
return(a+b+c)
}
formals(f) # lists the arguments
args(f) # show arguments
mydata <- rnorm(100) # normal distribution rnorm(n, mean=0, sd=1)
# sd: vector of standard deviations
print(mydata)
sd(mydata) # standard deviation
sd(x = mydata)
sd(x = mydata, na.rm=FALSE)
sd(na.rm=FALSE,x = mydata) # the order is not a problem
sd(na.rm=FALSE,mydata) # not recommended but works
#lm(y-x, mydata, model = F, 1:10) # fitting linear models, does not work here
## Lazy evaluation
f <- function(a,b) {
a^2
}
f(2) # b is not used in the function so it is not required
## another side effect of lazy evaluation:
f <- function(a,b) {
print(sqrt(2)) # executed
print(b) # error
}
f(2)
## variable number of arguments
myplot <- function(x,y, type = "l", ...) {
plot(x,y, type = type, ...)
}
x <- 1:20
y <- sqrt(x)
## partical matching does not work in some
args(paste) # function (..., sep = " ", collapse = NULL)
args(cat) # function (..., file = "", sep = " ", fill = FALSE,
# labels = NULL, append = FALSE)
paste("a","b",sep=":")
a <- 1:20
b <- 21:30
paste(a,b)
paste(a,b,sep=".")
## scoping: if you call search(), it will show what order a symbol is searched for
lm <- function(...) {
print("pooh")
}
lm() # linear model has been masked
rm(lm)
lm() # error for lack of arguments
rm(z)
## lexical scoping:
f <- function(x,y) {
print(z) # undpredictable, free variable (rm(z) may cause an error)
x^2+y/z
}
f(1,2)
## a environment contains of pairs of symbols and values
## functions and non-functions have separate namespaces
rm(x)
rm(n)
## enclosure returns a function
make.power <- function(n) {
pow <- function(x) {
x^n
}
pow
}
cube <- make.power(3)
square <- make.power(2)
cube(3)
square(2)
## what's inside?
ls(environment(cube))
get("n",environment(cube))
get("pow",environment(cube))
## lexical scoping
rm(x)
y <- 10
f <- function(x) {
y <- 2
y^2 + g(x)
}
g <- function(x) {
x*y
}
f(3) # 34 (2^2 + 3*10)
g <- function(x) {
a <- 3
x+a+y
}
g(2)
## sometimes, lexical scoping can cause problems (defining environment may be difficult to find)
## optimizations (optim, nlm, optimize)
## constructor function:
set.seed(1)
normals <- rnorm(100,1,2)
make.NegLogLik <- function(data, fixed=c(F,F), debug=F) { # negative log likelihood
params <- fixed
function(p) {
params[!fixed] <- p
nu <- params[1]
sigma <- params[2]
if (debug) {
cat("params: ",params,"\n")
cat("nu: ",nu,"\n")
cat("sigma: ",sigma,"\n")
}
a <- -0.5*length(data)*log(2*pi*sigma^2)
b <- -0.5*sum((data-nu)^2) / (sigma^2)
-(a+b) # maximised needs to be negated
}
}
sample <- c(1:5)
samp.res <- make.NegLogLik(sample,debug=T)
ls(environment(samp.res)) # "data", "fixed", "params"
get("data",environment(samp.res)) # 1:5
get("fixed",environment(samp.res)) # F F
get("params",environment(samp.res)) # F F
samp.res(2)
samp.res(c(2,3))
nLL <- make.NegLogLik(normals,c(FALSE,2))
optim(c(nu=0,sigma=1),nLL)$par
optimize(nLL, c(-1,3))$minimum
x <- seq(0.5, 1.5, len = 100) # sequence from 1.7 to 1.9
y <- sapply(x, nLL)
plot(x, exp(-(y-min(y))), type = 'l')
nLL <- make.NegLogLik(normals,c(1,FALSE))
optimize(nLL, c(1e-6,10))$minimum
x <- seq(1.7, 1.9, len = 100) # sequence from 1.7 to 1.9
y <- sapply(x, nLL)
plot(x, exp(-(y-min(y))), type = 'l')