小编gen*_*ser的帖子

如何在R中停止一个耗时太长的函数并给它一个替代方案?

我正试图以"正确的方式"做事.有时"正确的方式"需要太长时间,具体取决于输入.我真的不知道这将是什么时候.当"正确的方式"花费太长时间时,我想要采取"黑客的方式".如何让R监视特定任务的持续时间,如果阈值已经过去,还要给它做其他事情?我想这将是try家庭的一部分,但我不太确定该怎么称呼它或谷歌.

下面的虚拟例子.如果slow.func花费的时间太长,我想interuptor停下来然后打个电话fast.func.

slow.func <- function(x){
    Sys.sleep(x)    
    print('good morning')
}

fast.func <- function(x){
    Sys.sleep(x/10) 
    print('hit snooze')
}

interuptor = function(FUN,args, time.limit, ALTFUN){
#   START MONITORING TIME HERE
    do.call(FUN,args)
#   IF FUN TAKES TOO LONG, STOP IT, CALL A
    do.call(ALTFUN,args)
}

interuptor(slow.func, list(x = 2), time.limit = 1, fast.func)
Run Code Online (Sandbox Code Playgroud)

error-handling time r

18
推荐指数
2
解决办法
3205
查看次数

提取用于以mgcv形成平滑图的数据

几年前的这个主题描述了如何提取用于绘制拟合gam模型的平滑组件的数据.它有效,但只有当有一个平滑变量时才有效.我有多个平滑变量,不幸的是我只能从系列的最后一个中提取平滑.这是一个例子:

library(mgcv)
a = rnorm(100)
b = runif(100)
y = a*b/(a+b)

mod = gam(y~s(a)+s(b))
summary(mod)

plotData <- list()
trace(mgcv:::plot.gam, at=list(c(25,3,3,3)), 
        #this gets you to the location where plot.gam calls plot.mgcv.smooth (see ?trace)
        #plot.mgcv.smooth is the function that does the actual plotting and
        #we simply assign its main argument into the global workspace
        #so we can work with it later.....
        quote({
                    #browser()
                    plotData <<- c(plotData, pd[[i]])
                }))
plot(mod,pages=1)
plotData
Run Code Online (Sandbox Code Playgroud)

我试图让两个估计的平滑函数ab,但列表plotData只给我估计b.我已经研究了plot.gam …

trace r mgcv

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

R中的"圆形"表示

给定一个月的数据集,我如何计算"平均"月份,同时考虑到月份是循环的?

months = c(1,1,1,2,3,5,7,9,11,12,12,12)
mean(months)
## [1] 6.333333
Run Code Online (Sandbox Code Playgroud)

在这个虚拟的例子中,平均值应该是1月或12月.我看到有循环统计的软件包,但我不确定它们是否适合我的需求.

average r time-series mean

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

在R中匹配文本字符串时处理错误的拼写

我正在收集调查数据(使用开放数据工具包),我的现场团队,祝福他们的心,有时会对人名的拼写有点创意.所以我有一个"正确的"响应者名称,以及链接到"家庭成员名称"变量的一些记录的年龄变量.有许多不同年龄的家庭成员.我想要受访年龄.

这是一些说明我的问题的虚假数据:

#the respondent
    r = data.frame(name = c("Barack Obama", "George Bush", "Hillary Clinton"))
#a male member
    m = data.frame(name = c("Barack Obama","George", "Wulliam Clenton"), age = c(55,59,70)); m$name=as.character(m$name)
#a female member
    f = data.frame(name = c("Michelle O","Laura Busch", "Hillary Rodham Clinton"), age = c(54,58,69)); f$name=as.character(f$name)
#if the responsent is the the given member, record their age.  if not, NA
    a = cbind(
        ifelse(r$name==m$name,m$age,NA)
        ,ifelse(r$name==f$name,f$age,NA)
        )
    #make a function for plyr that gives me the age of the matched respondent …
Run Code Online (Sandbox Code Playgroud)

merge r character spelling misspelling

10
推荐指数
2
解决办法
5685
查看次数

从R中的列表元素制作矢量

我从R中写的一个函数中得到了一个bootstrap统计列表列表.主列表有1000个bootstrap迭代.列表中的每个元素本身都是三个列表的列表,包括四个变量中每个变量的拟合值("fvboot" - 501x4矩阵).

我想为x值的网格上的每个位置创建一个值的向量,从1:501开始,对于每个变量,从1:4开始.

例如,对于第j个变量的xgrid上的第i个点,我想制作如下的向量:

        vec = bootfits$fvboot[[1:1000]][i,j]
Run Code Online (Sandbox Code Playgroud)

但当我这样做时,我得到:

recursive indexing failed at level 2
Run Code Online (Sandbox Code Playgroud)

谷歌搜索,我想我明白为什么R这样做.但是我没有得到如何将每个fvboot矩阵的ijth元素转换为1000x1向量的答案.

帮助将不胜感激.

r list

9
推荐指数
2
解决办法
1万
查看次数

更新了R"maps"包的世界地图?

library(maps)
1> map.where(database="world",29.392089,53.592505)
[1] "USSR"
Run Code Online (Sandbox Code Playgroud)

有谁知道如何获得更新的世界地图数据库来在地图包中驱动这个功能?我现在只需要国名,而不是详细的地方行政信息,例如gadm.org提供的信息.

maps r geospatial

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

Predict.glm不会预测响应中的缺失值

出于某种原因,当我指定glms(并且lm也是如此)时,R不会预测数据的缺失值.这是一个例子:

y = round(runif(50))
y = c(y,rep(NA,50))
x = rnorm(100)
m = glm(y~x, family=binomial(link="logit"))
p = predict(m,na.action=na.pass)
length(p)

y = round(runif(50))
y = c(y,rep(NA,50))
x = rnorm(100)
m = lm(y~x)
p = predict(m)
length(p)
Run Code Online (Sandbox Code Playgroud)

p的长度应该是100,但是它的50.奇怪的是,我在同一个脚本中有其他预测可以预测丢失的数据.

编辑:事实证明,那些其他预测是非常错误的 - 我正在做imputed.value = rnorm(N,mean.from.predict,var.of.prediction.interval).这回收了来自lm预测或glm预测函数的均值和sd向量length(predict)<N,这与我所寻求的完全不同.

所以我的问题是我的示例代码什么阻止glm和lm预测缺失值?

谢谢!

r prediction missing-data lm glm

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

矩阵元素的逐块和

我想从这样的事情:

1> a = matrix(c(1,4,2,5,2,5,2,1,4,4,3,2,1,6,7,4),4)
1> a
     [,1] [,2] [,3] [,4]
[1,]    1    2    4    1
[2,]    4    5    4    6
[3,]    2    2    3    7
[4,]    5    1    2    4
Run Code Online (Sandbox Code Playgroud)

对于这样的事情:

     [,1] [,2]
[1,]   12   15
[2,]   10   16
Run Code Online (Sandbox Code Playgroud)

...不使用for循环,plyr或其他没有循环.可能?我正在尝试将地理纬度/经度数据集从5弧分缩小到半度,我有一个ascii网格.我指定blocksize的一个小函数会很棒.我有数百个这样的文件,所以让我能够快速完成而没有并行化/超级计算机的东西将非常感激.

r matrix linear-algebra vectorization

7
推荐指数
2
解决办法
934
查看次数

R中的数字的pdf中有难看的白线

我正在尝试在R中制作彩条和光栅贴图,输出数字在导出为pdf时会在其中显示难看的线条.

这是生成颜色条的代码.在R中运行它看起来很好:

color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
    scale = (length(lut)-1)/(max-min)

    plot(c(0,10), c(min,max), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
    axis(4, ticks, las=1)
    for (i in 1:(length(lut)-1)) {
     y = (i-1)/scale + min
     rect(0,y,10,y+1/scale, col=lut[i], border=NA)
    }
}

par(mfrow=c(2,1))
par(mar=c(3,0,3,2.5))
pal = colorRampPalette(c("red","yellow"))
neg = pal(100)
pal = colorRampPalette(c("yellow","darkgreen"))
pos = pal(50)
color.bar(c(neg,pos),min=-75,max=50,ticks=c(-75,-50,-25,0,25,50))
color.bar(colorRampPalette(c("goldenrod","blue"))(25),min=0,max=1)
par(mar=c(5.1,4.1,4.1,2.1))
    dev.copy2pdf(file = "colorbar_wood.pdf", height = 8, width = 1)
pdf("colorbar_wood.pdf",width=1,height=8)
par(mfrow=c(2,1))
par(mar=c(3,0,3,2.5))
pal = colorRampPalette(c("red","yellow"))
neg = pal(100)
pal = …
Run Code Online (Sandbox Code Playgroud)

graphics r

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

当我尝试并行安装多个模型时,为什么tensorflow/keras会窒息?

我正在尝试拟合有限混合模型,每个类的混合模型都是神经网络.能够并行化对我来说非常有用,因为keras并没有最大化我笔记本电脑上的所有可用内核,更不用说大型集群了.

但是当我尝试为平行的foreach循环内的不同模型设置不同的学习速率时,整个事情就会窒息.

到底是怎么回事?我怀疑它与范围有关 - 工作人员可能没有在tensorflow的单独实例上运行.但我真的不知道.我怎样才能做到这一点?我需要了解什么才能知道为什么这不起作用?

这是一个MWE.设置foreach循环%do%,它工作正常.将它设置为%dopar%并在装配阶段窒息.

library(foreach)
library(doParallel)
registerDoParallel(2)
library(keras)
library(tensorflow)
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y

x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))
# rescale
x_train <- x_train / 255
x_test <- x_test / 255

y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)

# make tensorflow run single-threaded
session_conf <- tf$ConfigProto(intra_op_parallelism_threads = 1L,
                               inter_op_parallelism_threads = 1L)
# Create the …
Run Code Online (Sandbox Code Playgroud)

parallel-processing scope r keras tensorflow

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