kernMod <- function(x){
#x is the data for a single animal
rs <- kerneloverlap(x[,c("X","Y")],
x$year,lev = 90,
grid = 30,
meth = "VI",
conditional = TRUE)
#Assumes we're only comparing two years
out <- data.frame(year = paste(colnames(rs),collapse="-"), val = rs[2,1])
out
}
现在我们可以将其单独应用于每只动物:
kernMod(subset(loc,anid == 'c_002'))
year val
1 2003-2004 0
> kernMod(subset(loc,anid == 'c_104'))
year val
1 2003-2004 0.06033966
或者我们可以使用 ddply 来自 plyr 包,依次将其应用到每种动物:
ddply(loc,.(anid),.fun = kernMod)
anid year val
1 c_002 2003-2004 0.00000000
2 c_104 2003-2004 0.06033966
如果您的完整数据有多个年,kernMod 将需要更多修改,因为 kerneloverlap 返回一个 nxn 矩阵,其中 n 是数据中的年数。也许像这样(未经测试)
kernMod <- function(x){
#x is the data for a single animal
rs <- kerneloverlap(x[,c("X","Y")],
x$year,lev = 90,
grid = 30,
meth = "VI",
conditional = TRUE)
rs[lower.tri(rs,diag = TRUE)] <- NA
rs <- melt(rs)
rs <- subset(rs, !is.na(value))
out <- data.frame(year = paste(rs$X1,rs$X2,collapse="-"), val = rs$value)
out
}
这种方法应该通过仅计算您有数据的值来处理“丢失”的动物。
好的。我很想为此获得第三至第四作者,但我会满足于承认。 ;)
Ok, I'll bite.
Your code had some typos (I hope) that make it un-runnable. Let's throw it out and start over. The function kerneloverlap returns a matrix of overlap values for each pair of items specified in its second parameter. In your first example you're comparing years.
Let's start by imagining what we'd do with the data on just one animal, and write a function that outputs the values we want just for that simple case:
kernMod <- function(x){
#x is the data for a single animal
rs <- kerneloverlap(x[,c("X","Y")],
x$year,lev = 90,
grid = 30,
meth = "VI",
conditional = TRUE)
#Assumes we're only comparing two years
out <- data.frame(year = paste(colnames(rs),collapse="-"), val = rs[2,1])
out
}
Now we can apply this to each animal separately:
kernMod(subset(loc,anid == 'c_002'))
year val
1 2003-2004 0
> kernMod(subset(loc,anid == 'c_104'))
year val
1 2003-2004 0.06033966
or we can use ddply from the plyr package to apply it to each animal in turn:
ddply(loc,.(anid),.fun = kernMod)
anid year val
1 c_002 2003-2004 0.00000000
2 c_104 2003-2004 0.06033966
To include multiple seasons, you would simply add that to the list of variables to split over in ddply (untested):
ddply(loc,.(anid,season),.fun = kernMod)
To compare between seasons within year, you'll need to modify kernMod to pass x$season as the second argument and then call something like (untested):
ddply(loc,.(anid,year),.fun = kernMod)
If your full data has multiple years in it, kernMod will require some more modification, since kerneloverlap returns an n x n matrix, where n is the number of years in your data. Perhaps something like this (untested)
kernMod <- function(x){
#x is the data for a single animal
rs <- kerneloverlap(x[,c("X","Y")],
x$year,lev = 90,
grid = 30,
meth = "VI",
conditional = TRUE)
rs[lower.tri(rs,diag = TRUE)] <- NA
rs <- melt(rs)
rs <- subset(rs, !is.na(value))
out <- data.frame(year = paste(rs$X1,rs$X2,collapse="-"), val = rs$value)
out
}
This approach should handle "missing" animals by only calculating values for which you have data.
Ok. I'd love to get 3rd-4th author for this, but I'd settle for an acknowledgement. ;)
发布评论
评论(1)
好吧,我会咬的。
您的代码有一些拼写错误(我希望)导致它无法运行。让我们把它扔掉并重新开始。函数
kerneloverlap
返回第二个参数中指定的每对项目的重叠值矩阵。在您的第一个示例中,您正在比较年份。让我们首先想象一下我们将如何处理一只动物的数据,并编写一个函数来输出我们想要的简单情况的值:
现在我们可以将其单独应用于每只动物:
或者我们可以使用
ddply
来自plyr
包,依次将其应用到每种动物:要包含多个季节,您只需将其添加到变量列表中即可在
ddply
中进行拆分code>(未经测试):比较一年内的季节之间,您需要修改
kernMod
以传递x$season
作为第二个参数,然后调用类似的内容(未经测试):如果您的完整数据有多个年,
kernMod
将需要更多修改,因为kerneloverlap
返回一个 nxn 矩阵,其中 n 是数据中的年数。也许像这样(未经测试)这种方法应该通过仅计算您有数据的值来处理“丢失”的动物。
好的。我很想为此获得第三至第四作者,但我会满足于承认。 ;)
Ok, I'll bite.
Your code had some typos (I hope) that make it un-runnable. Let's throw it out and start over. The function
kerneloverlap
returns a matrix of overlap values for each pair of items specified in its second parameter. In your first example you're comparing years.Let's start by imagining what we'd do with the data on just one animal, and write a function that outputs the values we want just for that simple case:
Now we can apply this to each animal separately:
or we can use
ddply
from theplyr
package to apply it to each animal in turn:To include multiple seasons, you would simply add that to the list of variables to split over in
ddply
(untested):To compare between seasons within year, you'll need to modify
kernMod
to passx$season
as the second argument and then call something like (untested):If your full data has multiple years in it,
kernMod
will require some more modification, sincekerneloverlap
returns an n x n matrix, where n is the number of years in your data. Perhaps something like this (untested)This approach should handle "missing" animals by only calculating values for which you have data.
Ok. I'd love to get 3rd-4th author for this, but I'd settle for an acknowledgement. ;)