小编tim*_*ffe的帖子

如何指定一个渐变函数,以便在optim()或其他优化器中使用

我有一个优化问题,该Nelder-Mead方法将解决,但我也想解决使用BFGS或Newton-Raphson,或采取渐变功能的东西,以获得更快的速度,并希望更精确的估计.我编写了这样一个渐变函数(我认为)optim/ optimxdocumentation中的示例,但是当我使用它时,BFGS我的起始值要么不移动(optim()),否则函数完全不运行(optimx(),返回Error: Gradient function might be wrong - check it!).对不起,有一些代码涉及复制这个,但是这里有:

这是我想要获得参数估计的函数(这是为了平滑老年死亡率,其中x是年龄,从80岁开始):

    KannistoMu <- function(pars, x = .5:30.5){
      a <- pars["a"]
      b <- pars["b"]
      (a * exp(b * x)) / (1 + a * exp(b * x))
    }
Run Code Online (Sandbox Code Playgroud)

这里是一个对数似然函数,用于根据观察到的速率(定义为死亡,.Dx过度暴露.Exp)来估计它:

    KannistoLik1 <- function(pars, .Dx, .Exp, .x. = .5:30.5){
      mu <- KannistoMu(exp(pars), x = .x.)
      # take negative and minimize it (default optimizer behavior) …
Run Code Online (Sandbox Code Playgroud)

optimization r

12
推荐指数
1
解决办法
8188
查看次数

如何在R中保存分层pdf(通过Sweave?)

我已经搜索了SO,谷歌搜索,阅读?pdf,并且关于将图表保存为pdf的可能性,其中图层可以在pdf查看器边距中打开和关闭.什么我谈论的例子是美国地质勘探局四地形地图,它可以下载与多层,如PDF (压缩PDF).

pdf()帮助文件中的以下句子听起来不祥,但我也想检查一下我是不是误解了它:

 The R graphics model does not distinguish graphics objects at the level 
 of the driver interface.
Run Code Online (Sandbox Code Playgroud)

我曾经能够在Illustrator中保存分层的pdf,但我不再使用这个程序了.也许有人可以想到R内部的解决方法?我用来映射的数据很大,但这里有一个玩具示例:

pdf("2objects.pdf")
plot(NULL, type = "n",xlim = c(0,1),ylim = c(0,1))
rect(0,.7,.7,0,border = "blue",lwd=2)
rect(.3,1,1,.3,border = "red",lty=2,lwd=2)
dev.off()
Run Code Online (Sandbox Code Playgroud)

它看起来像这样(这是一个png,但上面会给出一个pdf) 在此输入图像描述

我希望能够将红色和蓝色框作为具有可见性的图层,可以在pdf查看器中打开和关闭.

非常感谢!

编辑:在R-help中找到线程(re:@mnel),看起来不可能.我仍然会打开这个问题,万一有人提出了一个漂亮的R-tastic解决方法.

编辑(2012年9月5日):我尝试通过Sweave执行此操作,并使用此处发布的解决方法取得了部分成功.此方法生成带有"图层"的单个pdf,可以使用图像下方的超链接文本打开和关闭这些图层.它使用'动画'技巧来实现.虽然它仍然不是我最终的期望结果,但它的优势在于不依赖于特定的pdf观众.我仍然会等着看是否有人发布了一个方法来做一个层,也就是一个Sweave文档中的OCG,然后我可以自动化.

编辑(2012年9月13日):我使用上面提到的代码发布了我的进展答案.我能够让它在更复杂的现实世界中工作,而不会改变代码,在美国内部覆盖不同的行政和统计边界.在这种情况下,我只命名为不同的地图叠加layer-0.pdf,layer-1.pdf等等,和它的工作没有错误.我仍然希望最终能在这里出现更好的东西.

谢谢大家的评论

pdf latex r sweave

10
推荐指数
1
解决办法
678
查看次数

如何通过简化R中的两个SpatialPolygonsDataFrame对象来创建新多边形?

说我有两套形状文件覆盖同一地区,但往往并不总是共享边界,例如美国郡和PUMA.我想定义一个新的多边形比例,它使用PUMA和县作为原子构建块,即两者都不能分割,但我仍然喜欢尽可能多的单位.这是一个玩具示例:

library(sp)
# make fake data
# 1) counties:
Cty <- SpatialPolygons(list(
    Polygons(list(Polygon(cbind(x=c(0,2,2,1,0,0),y=c(0,0,2,2,1,0)), hole=FALSE)),"county1"),
    Polygons(list(Polygon(cbind(x=c(2,4,4,3,3,2,2),y=c(0,0,2,2,1,1,0)),hole=FALSE)),"county2"),
    Polygons(list(Polygon(cbind(x=c(4,5,5,4,4),y=c(0,0,3,2,0)),hole=FALSE)),"county3"),
    Polygons(list(Polygon(cbind(x=c(0,1,2,2,0,0),y=c(1,2,2,3,3,1)),hole=FALSE)),"county4"),
    Polygons(list(Polygon(cbind(x=c(2,3,3,4,4,3,3,2,2),y=c(1,1,2,2,3,3,4,4,1)),hole=FALSE)),"county5"),
    Polygons(list(Polygon(cbind(x=c(0,2,2,1,0,0),y=c(3,3,4,5,5,3)),hole=FALSE)),"county6"),
    Polygons(list(Polygon(cbind(x=c(1,2,3,4,1),y=c(5,4,4,5,5)),hole=FALSE)),"county7"),
    Polygons(list(Polygon(cbind(x=c(3,4,4,5,5,4,3,3),y=c(3,3,2,3,5,5,4,3)),hole=FALSE)),"county8")
))

counties <- SpatialPolygonsDataFrame(Cty, data = data.frame(ID=paste0("county",1:8),
            row.names=paste0("county",1:8),
            stringsAsFactors=FALSE)
)
# 2) PUMAs:
Pum <- SpatialPolygons(list(
            Polygons(list(Polygon(cbind(x=c(0,4,4,3,3,2,2,1,0,0),y=c(0,0,2,2,1,1,2,2,1,0)), hole=FALSE)),"puma1"),
            Polygons(list(Polygon(cbind(x=c(4,5,5,4,3,3,4,4),y=c(0,0,5,5,4,3,3,0)),hole=FALSE)),"puma2"),
            Polygons(list(Polygon(cbind(x=c(0,1,2,2,3,3,2,0,0),y=c(1,2,2,1,1,2,3,3,1)),hole=FALSE)),"puma3"),
            Polygons(list(Polygon(cbind(x=c(2,3,4,4,3,3,2,2),y=c(3,2,2,3,3,4,4,3)),hole=FALSE)),"puma4"),
            Polygons(list(Polygon(cbind(x=c(0,1,1,3,4,0,0),y=c(3,3,4,4,5,5,3)),hole=FALSE)),"puma5"),
            Polygons(list(Polygon(cbind(x=c(1,2,2,1,1),y=c(3,3,4,4,3)),hole=FALSE)),"puma6")
    ))
Pumas <- SpatialPolygonsDataFrame(Pum, data = data.frame(ID=paste0("puma",1:6),
            row.names=paste0("puma",1:6),
            stringsAsFactors=FALSE)
)

# desired result:
Cclust <- SpatialPolygons(list(
            Polygons(list(Polygon(cbind(x=c(0,4,4,3,3,2,2,1,0,0),y=c(0,0,2,2,1,1,2,2,1,0)), hole=FALSE)),"ctyclust1"),
            Polygons(list(Polygon(cbind(x=c(4,5,5,4,3,3,4,4),y=c(0,0,5,5,4,3,3,0)),hole=FALSE)),"ctyclust2"),
            Polygons(list(Polygon(cbind(x=c(0,1,2,2,3,3,4,4,3,3,2,2,0,0),y=c(1,2,2,1,1,2,2,3,3,4,4,3,3,1)),hole=FALSE)),"ctyclust3"),
            Polygons(list(Polygon(cbind(x=c(0,2,2,3,4,0,0),y=c(3,3,4,4,5,5,3)),hole=FALSE)),"ctyclust4")
    ))
CtyClusters <- SpatialPolygonsDataFrame(Cclust, data = data.frame(ID = paste0("ctyclust", 1:4),
            row.names = paste0("ctyclust", 1:4),
            stringsAsFactors=FALSE) …
Run Code Online (Sandbox Code Playgroud)

gis r

7
推荐指数
1
解决办法
1239
查看次数

matlab 的 csaps() 的 R 等价物是多少

csaps()在 matlab 中根据平滑参数的特定定义进行三次样条p。这是一些matlab代码及其结果:

     % x variable
    age = 75:99  

    % y variable
    diffs = [-39   -2 -167  -21  -13   32  -37 -132 -143  -91  -93  -88  -62 -112  -95  -28  -90  -40  -27  -23  -28  -11   -8   -6    1]

    % 0.0005 is the parameter p, and the later specification of 
    % age are the desired x for prediction
    csaps(age,diffs,0.0005,age)
    % result (column headers removed):
     -63.4604  -64.0474  -64.6171  -65.1397  -65.6111  -66.0165  -66.3114  
     -66.4123  -66.2229  -65.6726  -64.7244  -63.3582  -61.5676 …
Run Code Online (Sandbox Code Playgroud)

matlab r splines

5
推荐指数
1
解决办法
881
查看次数

R中的多个箱图,同时按列和行对矩阵进行分组

我无法弄清楚如何在R中制作包含多个箱图的单个图形,同时按列和行对数据框/矩阵进行分组.

我在R中有一个10行和500列的数据框.这些列分为2组(因子 - 1和2),现在我想要一个包含两个箱图的单个图,该数据框的每一行都受列组的影响.

防爆.

    M1 N2 O1 P2 Q1 R2      # [The 1's and 2's refer to my two column groups]
 A  10 11 12 13 14 15
 B  15 14 13 12 11 10 
 C  20 21 22 23 24 25
 D  25 24 23 22 21 20
Run Code Online (Sandbox Code Playgroud)

因此,对于上面的例子,我希望每行有一个带有"4个箱图对"的图,这样每个箱图对将代表对应于我的列的1和2的因子的值.

提前致谢 !!!

r boxplot

1
推荐指数
1
解决办法
3371
查看次数

如何在ecdf情节中添加图例?

我正在生成一个ecdf情节,并希望添加一个图例,但收到以下错误:

plot(xlim=c(0,100), ylim=c(0,1), main=NULL, xaxs="i", yaxs="i", 
     ecdf(PrecentageVector1),  col="red", do.p = FALSE, pch=19, 
     lwd=3,  legend("bottomleft",c("A","B","C")), panel.first = grid()) 

Error in strwidth(legend, units = "user", cex = cex, font = text.font) :  
invalid graphics state
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?

r

0
推荐指数
1
解决办法
1801
查看次数

标签 统计

r ×6

boxplot ×1

gis ×1

latex ×1

matlab ×1

optimization ×1

pdf ×1

splines ×1

sweave ×1