羅
羅左欣 8 years ago
關於"閉包(closuer)" : http://openhome.cc/Gossip/JavaScript/Closure.html
- 20151023 [Coursera] R Programming (9)
- 整理自 R Programming (Week 2) -- Scoping Rules
- [ Week 2 課程內容 ]
- [ 筆記內容 ] 作用域規則
- (一)Scoping Rules - Symbol Binding
- [ 重點整理 ]
- 1. 符號的順序
- 2. 作用域規則
- 3. 詞法作用域 (lexical scoping)
- (二)Scoping Rules - R Scoping Rules
- [ 重點整理 ]
- 1. 詞法作用域 (lexical scoping)
- 2. 探索"函數閉包 (Function Closure)"
- 3. "詞法作用域 (Lexical Scoping)" VS "動態作用域 (Dynamic Scoping)"
- 4. "詞法作用域 (lexical scoping)"的使用結論
- (三)Scoping Rules - Optimization Example (OPTIONAL)
- [ 重點整理 ] 作用域規則的應用
- 1. 優化 (Optimization)
- 2. 結論 >> 關於"詞法作用域 (Lexical Scoping)"
整理自 R Programming (Week 2) -- Scoping Rules
Control Structures - Introduction[0:54](已完成)Control Structures - If-else[1:58](已完成)Control Structures - For loops[4:25](已完成)Control Structures - While loops[3:22](已完成)Control Structures - Repeat, Next, Break[4:57](已完成)Your First R Function[10:29](僅觀看)Functions(part1)[9:17](已完成)Functions(part2)[7:13](已完成)(一)Scoping Rules - Symbol Binding
# 原先 R 裡面的"lm()"是用來擬合線性模型。
# "lm()"詳細說明 : https://stat.ethz.ch/R-manual/R-devel/library/stats/html/lm.html
> lm <- function(x) { x * x } # 重新定義"lm()"
> lm
function(x) { x * x }
# 使用"search()"顯示 R 搜尋列表的內容(依照順序排列)。
> search()
[1] ".GlobalEnv" "tools:rstudio"
[3] "package:stats" "package:graphics"
[5] "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods"
[9] "Autoloads" "package:base"
# 這裡有一個自由變數: z (未在函數中定義)
f <- function(x, y) {
x^2 + y / z
}
(二)Scoping Rules - R Scoping Rules
# 在函數內定義另外一個函數
> make.power <- function(n) { # "make.power()"的參數"n"
+ pow <- function(x) { # "pow()"的參數是"x"
+ x^n
+ }
+ pow # 回傳"pow()"的值
+ }
>
>
> cube <- make.power(3)
> square <- make.power(2)
> cube(3)
[1] 27
> square(3)
[1] 9
# 使用"ls()"查看環境
> ls(environment(cube))
[1] "n" "pow"
> get("n", environment(cube))
[1] 3
>
>
> ls(environment(square))
[1] "n" "pow"
> get("n", environment(square))
[1] 2
# "詞法作用域 (Lexical Scoping)"
> y <- 10 # "y"
> f <- function(x) { # "f()"
+ y <- 2
+ y^2 + g(x) # 這裡"y"的值為"2"
+ }
> g <- function(x) { # "g()"
+ x*y # 這裡"y"的值為"10"
+ }
>
> f(3)
[1] 34
# "詞法作用域"VS"動態作用域"的結果看起來相同
> # 情況1:還沒有定義"y"
> g <- function(x) { # "x"是形式參數
+ a <- 3
+ x+a+y # "y"是自由變數
+ }
> g(2)
Error in g(2) : object "y" not found
>
> # 情況2:已經定義"y"了
> y <- 3
> g(2)
[1] 8
(三)Scoping Rules - Optimization Example (OPTIONAL)
# 對數概似函數
> make.NegLogLik <- function(data, fixed=c(FALSE,FALSE)) {
+ params <- fixed
+ function(p) {
+ params[!fixed] <- p
+ mu <- params[1]
+ sigma <- params[2]
+ a <- -0.5*length(data)*log(2*pi*sigma^2)
+ b <- -0.5*sum((data-mu)^2) / (sigma^2)
+ -(a + b)
+ }
+ }
>
>
> set.seed(1); normals <- rnorm(100, 1, 2)
> nLL <- make.NegLogLik(normals)
> nLL
function(p) {
params[!fixed] <- p
mu <- params[1]
sigma <- params[2]
a <- -0.5*length(data)*log(2*pi*sigma^2)
b <- -0.5*sum((data-mu)^2) / (sigma^2)
-(a + b)
}
<environment: 0x08d25c7c>
>
> ls(environment(nLL))
[1] "data" "fixed" "params"
>
>
> optim(c(mu = 0, sigma = 1), nLL)$par
mu sigma
1.218239 1.787343
>
> nLL <- make.NegLogLik(normals, c(FALSE, 2))
> optimize(nLL, c(-1, 3))$minimum
[1] 1.217775
>
> nLL <- make.NegLogLik(normals, c(1, FALSE))
> optimize(nLL, c(1e-6, 10))$minimum
[1] 1.800596
>
>
> nLL <- make.NegLogLik(normals, c(1, FALSE))
> x <- seq(1.7, 1.9, len = 100)
> y <- sapply(x, nLL)
> plot(x, exp(-(y - min(y))), type = "l")
>
>
> nLL <- make.NegLogLik(normals, c(FALSE, 2))
> x <- seq(0.5, 1.5, len = 100)
> y <- sapply(x, nLL)
> plot(x, exp(-(y - min(y))), type = "l")
>