在 R 中的 for 循环中绘图
Plotting in a for loop in R
我需要绘制柯西分布的对数似然函数。以下是 Cauchy 分布的对数似然的代码:
CauchyLL <- function(theta,x){
#CauchyLL is the log-likelihood function for the Cauch Distribution
#x is the data vector and theta is the unknown parameter
n <- length(x)
#f0 is the log likelihood function
#f1 is the first derivative of the log likelihood
#f2 is the second derivative of the log likelihood
f0 <- -n*log(pi)-sum(log((x-theta)^2+1),na.rm=TRUE)
f1 <- sum((2*(x-theta))/((x-theta)^2+1),na.rm=TRUE)
f2 <- 2*sum(((x-theta)^2-1)/((x-theta)^2+1),na.rm=TRUE)
return(c(f0,f1,f2))
}
我的数据 x 由以下内容给出:
x <- c(1.77, -0.23, 2.76, 3.80, 3.47, 56.75, -1.34, 4.24, -2.44, 3.29, 3.71, -2.40, 4.53, -0.07, -1.05, -13.87, -2.53, -1.75, 0.27, 43.21)
我的 theta 由以下给出:
xgrid<-seq(-9,10,by=1)
我想使用 for 循环为每个 theta 值绘制柯西分布的对数似然函数。这是我的尝试:
for(j in 1:20){
print(xgrid[j])
print(CauchyLL(xgrid[j],x)[1])
plot(xgrid[j],CauchyLL(xgrid[j],x)[1])
}
这个for循环似乎只绘制theta的最后一个值,但它不绘制theta的前19个值。我怎样才能更改它以便获得所有 20 个 theta 值的绘图?
反复尝试plot
会"always"over-write/erase之前的情节。唯一的例外是特定的 plot
方法是否支持 add=
参数。不通用。
一种常用技术(使用基础图形时)通常是第一次调用 plot
,然后在每次后续添加时调用相应的函数(例如,points(...)
或 lines(...)
, 许多其他可用)。由于您可能并不总是知道如何在第一次使用初始 plot
构建 canvas(您不知道完整的维度),因此计算所有数据 首先,然后确定您的 xlim
和 ylim
,然后从 "empty canvas" 开始,例如:
plot(NA, type = "n", main = "Quux!",
xlim = my_xs, xlab = "Theta", ylim = my_ys, ylab = "Cauchy")
points(xgrid, mydat[1,]) # etc
# or perhaps
for (rn in 1:10) points(xgrid[rn], mydat[rn])
我建议你考虑一下如何在向量上做这个。虽然修改 CauchyLL
函数来处理 theta
的 vector 当然是可行的,但这里有一个权宜之计:
sapply(xgrid, CauchyLL, x)
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] -119.527861 -115.986843 -111.869287 -107.015480 -101.159610 -93.873188
# [2,] 3.288293 3.809062 4.452029 5.298709 6.484735 8.175297
# [3,] 39.002874 38.805443 38.451129 37.815442 36.552463 33.531193
# [,7] [,8] [,9] [,10] [,11] [,12]
# [1,] -84.893042 -77.253757 -73.733690 -72.9736583 -74.294976 -74.6098028
# [2,] 9.342616 5.359918 1.921416 -0.6010282 -1.108758 0.2153465
# [3,] 25.228194 17.802691 18.071895 19.4391628 24.863162 23.7244631
# [,13] [,14] [,15] [,16] [,17] [,18]
# [1,] -74.4040007 -77.690581 -86.359895 -95.35868 -102.600671 -108.487314
# [2,] -0.5162973 -6.556115 -9.660541 -8.09967 -6.478883 -5.363464
# [3,] 17.5721616 16.121772 26.837379 34.13162 36.802884 37.967104
# [,19] [,20]
# [1,] -113.436160 -117.707625
# [2,] -4.576469 -3.993343
# [3,] 38.578288 38.941769
我推断您只对第一行感兴趣(基于绘图函数中的 [1]
),所以我将从中取出第一行并在一个命令中绘制所有内容:
plot(xgrid, sapply(xgrid, CauchyLL, x)[1,])
我需要绘制柯西分布的对数似然函数。以下是 Cauchy 分布的对数似然的代码:
CauchyLL <- function(theta,x){
#CauchyLL is the log-likelihood function for the Cauch Distribution
#x is the data vector and theta is the unknown parameter
n <- length(x)
#f0 is the log likelihood function
#f1 is the first derivative of the log likelihood
#f2 is the second derivative of the log likelihood
f0 <- -n*log(pi)-sum(log((x-theta)^2+1),na.rm=TRUE)
f1 <- sum((2*(x-theta))/((x-theta)^2+1),na.rm=TRUE)
f2 <- 2*sum(((x-theta)^2-1)/((x-theta)^2+1),na.rm=TRUE)
return(c(f0,f1,f2))
}
我的数据 x 由以下内容给出:
x <- c(1.77, -0.23, 2.76, 3.80, 3.47, 56.75, -1.34, 4.24, -2.44, 3.29, 3.71, -2.40, 4.53, -0.07, -1.05, -13.87, -2.53, -1.75, 0.27, 43.21)
我的 theta 由以下给出:
xgrid<-seq(-9,10,by=1)
我想使用 for 循环为每个 theta 值绘制柯西分布的对数似然函数。这是我的尝试:
for(j in 1:20){
print(xgrid[j])
print(CauchyLL(xgrid[j],x)[1])
plot(xgrid[j],CauchyLL(xgrid[j],x)[1])
}
这个for循环似乎只绘制theta的最后一个值,但它不绘制theta的前19个值。我怎样才能更改它以便获得所有 20 个 theta 值的绘图?
反复尝试
plot
会"always"over-write/erase之前的情节。唯一的例外是特定的plot
方法是否支持add=
参数。不通用。一种常用技术(使用基础图形时)通常是第一次调用
plot
,然后在每次后续添加时调用相应的函数(例如,points(...)
或lines(...)
, 许多其他可用)。由于您可能并不总是知道如何在第一次使用初始plot
构建 canvas(您不知道完整的维度),因此计算所有数据 首先,然后确定您的xlim
和ylim
,然后从 "empty canvas" 开始,例如:plot(NA, type = "n", main = "Quux!", xlim = my_xs, xlab = "Theta", ylim = my_ys, ylab = "Cauchy") points(xgrid, mydat[1,]) # etc # or perhaps for (rn in 1:10) points(xgrid[rn], mydat[rn])
我建议你考虑一下如何在向量上做这个。虽然修改
CauchyLL
函数来处理theta
的 vector 当然是可行的,但这里有一个权宜之计:sapply(xgrid, CauchyLL, x) # [,1] [,2] [,3] [,4] [,5] [,6] # [1,] -119.527861 -115.986843 -111.869287 -107.015480 -101.159610 -93.873188 # [2,] 3.288293 3.809062 4.452029 5.298709 6.484735 8.175297 # [3,] 39.002874 38.805443 38.451129 37.815442 36.552463 33.531193 # [,7] [,8] [,9] [,10] [,11] [,12] # [1,] -84.893042 -77.253757 -73.733690 -72.9736583 -74.294976 -74.6098028 # [2,] 9.342616 5.359918 1.921416 -0.6010282 -1.108758 0.2153465 # [3,] 25.228194 17.802691 18.071895 19.4391628 24.863162 23.7244631 # [,13] [,14] [,15] [,16] [,17] [,18] # [1,] -74.4040007 -77.690581 -86.359895 -95.35868 -102.600671 -108.487314 # [2,] -0.5162973 -6.556115 -9.660541 -8.09967 -6.478883 -5.363464 # [3,] 17.5721616 16.121772 26.837379 34.13162 36.802884 37.967104 # [,19] [,20] # [1,] -113.436160 -117.707625 # [2,] -4.576469 -3.993343 # [3,] 38.578288 38.941769
我推断您只对第一行感兴趣(基于绘图函数中的
[1]
),所以我将从中取出第一行并在一个命令中绘制所有内容:plot(xgrid, sapply(xgrid, CauchyLL, x)[1,])