我正在运行贝叶斯MCMC概率模型,我正在尝试并行实现它.在比较并行和串行时,我的机器性能令人困惑.我没有很多做并行处理的经验,所以有可能我做得不对.
我正在使用probit模型MCMCprobit的MCMCpack包,以及我parLapply在parallel包中使用的并行处理.
这是我的串行运行代码,结果来自system.time:
system.time(serial<-MCMCprobit(formula=econ_model,data=mydata,mcmc=10000,burnin=100))
user system elapsed
657.36 73.69 737.82
Run Code Online (Sandbox Code Playgroud)
这是我的并行运行代码:
#Setting up the functions for parLapply:
probit_modeling <- function(...) {
args <- list(...)
library(MCMCpack)
MCMCprobit(formula=args$model, data=args$data, burnin=args$burnin, mcmc=args$mcmc, thin=1)
}
probit_Parallel <- function(mc, model, data,burnin,mcmc) {
cl <- makeCluster(mc)
## To make this reproducible:
clusterSetRNGStream(cl, 123)
library(MCMCpack) # needed for c() method on master
probit.res <- do.call(c, parLapply(cl, seq_len(mc), probit_modeling, model=model, data=data,
mcmc=mcmc,burnin=burnin))
stopCluster(cl)
return(probit.res)
}
system.time(test<-probit_Parallel(model=econ_model,data=mydata,mcmc=10000,burnin=100,mc=2))
Run Code Online (Sandbox Code Playgroud)
结果来自system.time …
我有来自离散选择实验(DCE)的数据,查看来自不同部门的个人的招聘偏好.我已经格式化为长格式.我想用mlogit建模.我已经导出了数据,并且可以使用asclogit命令在Stata中成功运行模型,但是我无法在R中运行它.
这是前25行数据的快照:
> data[1:25,]
userid chid item sector outcome cul fit ind led prj rel
1 11275 211275 2 1 1 0 1 0 1 1 1
2 11275 211275 2 2 0 1 0 0 0 0 0
3 11275 211275 2 0 0 0 0 1 1 0 1
4 11275 311275 3 0 1 1 1 0 0 0 1
5 11275 311275 3 2 0 0 1 0 0 0 1
6 11275 311275 3 1 …Run Code Online (Sandbox Code Playgroud) 我正在创建一个邻接矩阵来在 R 中进行空间分析。数据都是美国大陆的县。我从美国人口普查老虎文件中获取了各县的空间多边形。
我能够创建邻居列表,并且它是对称的。但是当我将其转换为邻接矩阵时,它不是对称的。这是一个问题,因为我的目标是使用 运行空间自逻辑模型ngspatial::autologistic,并且出现错误,指出我必须提供对称二元邻接矩阵。
这是我创建邻接矩阵的 R 代码:
us<-readShapeSpatial("County_2010Census_DP1.shp")
#Trim out counties outside of continental US
us2<-us[!substr(us$GEOID10,1,2)%in%c('02','60','66','78','15','72'),]
us2.nb = poly2nb(us2)
is.symmetric.nb(us2.nb) #Comes out true
us2.adj = nb2mat(us2.nb, style="B",zero.policy=F)
isSymmetric(us2.adj) #comes out false
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我可以splogit毫无问题地使用这个邻接矩阵。我不是空间分析方面的专家,所以我不能说我知道这些命令中发生了什么。
在我的 Shiny 应用程序中,我的用户可以从服务器上传文件以查看结果。我让用户使用 selectInput 选择文件,然后他们单击 actionButton 加载文件。我还希望用户能够删除文件和添加新文件,并且我已经使用单独的 actionButtons 成功地进行了设置。当用户删除或添加文件时,我想更新 selectInput 以便删除的文件不再存在,而添加的文件则存在。我在我的 observeEvent 代码中使用 updateSelectInput 尝试了这个,但它不起作用。这是我的删除文件部分的observeEvent:
#Delete parameter files
observeEvent(input$delete,{
file.remove(input$input_file) #deletes the file selected in the input_file widget
choices <- list.files("C:/Users/shiny/testapp/", pattern="*.Rdata")
updateSelectInput(session,"input_file",choices) #supposed to update the input_file widget
})
Run Code Online (Sandbox Code Playgroud)
当我运行包含此代码的应用程序时,会发生两件事。在应用程序窗口中,我在 input_file 小部件的正上方看到以下文本:[object Object 在我的 R 控制台中,我得到:
asJSON(keep_vec_names=TRUE) 的输入是一个命名向量。在未来的 jsonlite 版本中,将不支持此选项,命名向量将被转换为数组而不是对象。如果您想要 JSON 对象输出,请改用命名列表。见?toJSON。
我已经session在我的 ShinyServer 调用中包含了参数:
shinyServer(function(input, output, session)
Run Code Online (Sandbox Code Playgroud)
任何意见,将不胜感激。
我构建了一个闪亮的应用程序,如果用户在 numericInput 中输入非数字值,它会显示一条弹出消息。下面的代码就是这样做的(使用shinyBS),但反应太快了。如果用户开始输入文本,但需要考虑一下要输入的数字,则会弹出该消息。我希望observeEvent在输入失去焦点时触发,以便用户有时间输入完整值,并且只有当他们转到下一个输入时observeEvent才会处理错误。有什么建议么?shinyBS如果有更好的解决方案,我不会执意使用,我在 4 年前就构建了这个应用程序,从那时起,Shiny 宇宙已经有了相当大的发展。
library(shinyBS)
library(shiny)
ui<-shinyUI(fluidPage(
fluidRow(
numericInput("value","My Value",value=0),
bsModal("number_Message", "", trigger="", size = "small","This field only accepts numeric values.")
)
))
server<-shinyServer(function(input, output, session){
output$value<-renderText({input$value})
observeEvent(input$value,{
# browser()
if(is.na(as.numeric((input$value)))==T){
toggleModal(session, "number_Message",toggle="toggle")
updateNumericInput(session,"value","My Value",value=0)
}
})
})
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)