1

一意の識別番号を持つ動物の複数の 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

4

1 に答える 1

0

を使用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()

ここに画像の説明を入力 ここに画像の説明を入力 ここに画像の説明を入力

于 2016-10-18T01:46:15.690 に答える