【问题标题】:Trying to generate and store multiple KDE models in R尝试在 R 中生成和存储多个 KDE 模型
【发布时间】:2016-10-17 23:05:47
【问题描述】:

我正在处理一个点数据集,其中包含来自具有唯一识别号的动物的多个 GPS 项圈的数据。

我想要做的是使用 Kernel Density Estimator 对每只动物的范围进行建模。我已经生成了每个模型的图表,但我需要保存每个模型的实际估计器以供进一步分析。我将在这里暴露我的无知,但我尝试使用 for 循环来做到这一点。

collars<-unique(gps$CollarID)

gps 是我的数据框

for (i in 1:length(collars)){
    collar<-subset(gps, CollarID == collars[i], select=c(Easting, Northing))
    cxy<-cbind(collar$Easting,collar$Northing)
    kde<-kde(cxy)
    plot(kde, xlab = "X", ylab = "Y")
    title(main = collars[i])
}

我所追求的是为每次迭代生成一个唯一命名的 kde 对象。我曾尝试在对象名称中包含计数器,但很快发现这不起作用。

任何帮助将不胜感激!

-JF

【问题讨论】:

  • 如果您能向我们展示一些您的数据,那就太好了。例如:head(gps)

标签: r gps analysis telemetry


【解决方案1】:

使用dplyr,您可以将计算包装在一个计算每个 kde 的函数中 东移、北移观察并将生成的 kde 对象保存在列表中以由单独的函数绘制

由于我们没有示例数据集,我使用了mtcars 数据集。

#load libraries, assuming you are using kde function from "ks" library
library("dplyr")
library("lazyeval") #required for interp function
library("ks")



DF = mtcars
indexColName = "cyl"
indexVar = unique(DF[,indexColName])
calcVars = c("hp","wt")


### Replacement values for your dataset ###

# DF = gps
# indexColName = "CollarID"
# indexVar = unique(DF[,indexColName])
# calcVars = c("Easting","Northing")

kde 计算函数:

fn_kdeCalc <- function(indexVarInput = indexVar,indexColInput = indexColName,calcVarsInput=calcVars) {

cat("Begin kde calc for",indexColInput,"=",indexVarInput,"\n")

#filter condition logic translates as CollarID == collars[i]
#the following is required for dynamic inputs , you can read more from trying, vignette("nse")

filter_criteria <- interp(~ filter_column == indexVarInput, filter_column = as.name(indexColInput))

kdeObj <- DF %>% 
        dplyr::filter_(filter_criteria) %>%           # subset dataset to specific filter condition
        dplyr::select(one_of(calcVarsInput)) %>%      # select specific columns for kde calc
        kde()                                         # calculate kde for selected variables

cat("End kde calc for",indexColInput,"=",indexVarInput,"\n")

return(kdeObj)

}

#calculate kde for each unique input variable using above function and save in a list

kdeObjList = lapply(indexVar,function(x) { 

fn_kdeCalc(indexVarInput = x,indexColInput = indexColName,calcVarsInput=calcVars) 

})

kde绘图功能:

fn_kdePlot = function(kdeObject = NULL,titleObj = NULL,labelVars=calcVars) {
  plot(kdeObject, xlab = labelVars[1], ylab = labelVars[2],main = paste0("Kernel Density estimation for ",indexColName,":",titleObj) )
}



### save plots ###
# you can use png() to save in different file for each plot or 
# pdf("KDE_plots1.pdf") to include them in one file

png()

lapply(1:length(kdeObjList), function(x) fn_kdePlot(kdeObject = kdeObjList[[x]],titleObj = indexVar[x]) )

dev.off()

【讨论】:

  • 这正是我所需要的。非常感谢!
  • 很高兴它有帮助,如果您没有进一步的疑问,您可以通过单击答案旁边的复选标记将答案标记为已解决。
猜你喜欢
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多