R中有没有办法在R中创建最近邻矩阵和变量值矩阵?
Is there a way in R to create a matrix of nearest neighbours and variable values inR?
我有这样的数据:
identity growth x-pos y-pos
1: Z 0.1 0.5 0.7
2: B 0.1 0.1 0.0
3: C 0.2 4.6 2.5
4: D 0.3 5.6 5.0
5: A 0.4 0.2 1.0
6: P 0.1 0.4 2.0
我想比较具有唯一身份的每个对象的 n 个最近邻居之间的增长值是否相关。
因此,基本上创建一个矩阵,根据 x-pos
和 y-pos
表示的位置,为每个唯一的 identity
行识别 5 个最近的邻居,并在对象的 growth
值之间执行关联(例如Z
)和Z
的第1、2、3、4、5最近邻的增长值。
我尝试制作一个欧几里德矩阵,然后使用 ADE 包使用自相关度量,但想知道是否有更简单的方法来构建这样的矩阵。
perform corelations between the growth
value of object (e.g. Z
) and the growth value of the 1st, 2nd, 3rd, 4th and 5th nearest neighbour of Z
您无法计算两点之间的相关性。
我能想到的最相似的事情是计算你的点和它们的平均邻居之间的相关性,或者做成对测试来比较它们。但这将适用于所有“对象”,而不是每个对象的相关性(因为每个对象只有 1 点)。
create a matrix which identifies the 5 nearest neighbours for each unique identity row based on the locations denoted by x-pos and y-pos
# read in data
df <- tribble(
~identity, ~growth, ~`x-pos`, ~`y-pos`,
"Z", 0.1, 0.5, 0.7,
"B", 0.1, 0.1, 0.0,
"C", 0.2, 4.6, 2.5,
"D", 0.3, 5.6, 5.0,
"A", 0.4, 0.2, 1.0,
"P", 0.1, 0.4, 2.0)
# here with 3 neighbors since we have only 6 points
n_neighbors <- 3
# make matrix of coordinates
mat <- as.matrix(df[,3:4])
rownames(mat) <- df$identity
# compute [euclidian] distances
dmat <- as.matrix(dist(mat))
# find neighbors (by name)
nei_mat <- apply(dmat, 1,
function(crow) {names(sort(crow))[seq_len(n_neighbors+1)]})[-1,]
# match names to initial data frame to make matrix of growth
ref_growth_mat <- matrix(df$growth, dimnames=list(df$identity))
growth_mat <- matrix(ref_growth_mat[nei_mat,], nrow = n_neighbors)
colnames(growth_mat) <- df$identity
# done
growth_mat
#> Z B C D A P
#> [1,] 0.4 0.1 0.3 0.2 0.1 0.4
#> [2,] 0.1 0.4 0.1 0.1 0.1 0.1
#> [3,] 0.1 0.1 0.1 0.1 0.1 0.1
我有这样的数据:
identity growth x-pos y-pos
1: Z 0.1 0.5 0.7
2: B 0.1 0.1 0.0
3: C 0.2 4.6 2.5
4: D 0.3 5.6 5.0
5: A 0.4 0.2 1.0
6: P 0.1 0.4 2.0
我想比较具有唯一身份的每个对象的 n 个最近邻居之间的增长值是否相关。
因此,基本上创建一个矩阵,根据 x-pos
和 y-pos
表示的位置,为每个唯一的 identity
行识别 5 个最近的邻居,并在对象的 growth
值之间执行关联(例如Z
)和Z
的第1、2、3、4、5最近邻的增长值。
我尝试制作一个欧几里德矩阵,然后使用 ADE 包使用自相关度量,但想知道是否有更简单的方法来构建这样的矩阵。
perform corelations between the
growth
value of object (e.g.Z
) and the growth value of the 1st, 2nd, 3rd, 4th and 5th nearest neighbour ofZ
您无法计算两点之间的相关性。
我能想到的最相似的事情是计算你的点和它们的平均邻居之间的相关性,或者做成对测试来比较它们。但这将适用于所有“对象”,而不是每个对象的相关性(因为每个对象只有 1 点)。
create a matrix which identifies the 5 nearest neighbours for each unique identity row based on the locations denoted by x-pos and y-pos
# read in data
df <- tribble(
~identity, ~growth, ~`x-pos`, ~`y-pos`,
"Z", 0.1, 0.5, 0.7,
"B", 0.1, 0.1, 0.0,
"C", 0.2, 4.6, 2.5,
"D", 0.3, 5.6, 5.0,
"A", 0.4, 0.2, 1.0,
"P", 0.1, 0.4, 2.0)
# here with 3 neighbors since we have only 6 points
n_neighbors <- 3
# make matrix of coordinates
mat <- as.matrix(df[,3:4])
rownames(mat) <- df$identity
# compute [euclidian] distances
dmat <- as.matrix(dist(mat))
# find neighbors (by name)
nei_mat <- apply(dmat, 1,
function(crow) {names(sort(crow))[seq_len(n_neighbors+1)]})[-1,]
# match names to initial data frame to make matrix of growth
ref_growth_mat <- matrix(df$growth, dimnames=list(df$identity))
growth_mat <- matrix(ref_growth_mat[nei_mat,], nrow = n_neighbors)
colnames(growth_mat) <- df$identity
# done
growth_mat
#> Z B C D A P
#> [1,] 0.4 0.1 0.3 0.2 0.1 0.4
#> [2,] 0.1 0.4 0.1 0.1 0.1 0.1
#> [3,] 0.1 0.1 0.1 0.1 0.1 0.1