sas 9.2 的距离函数
Distance function for sas 9.2
在SAS 9.3中我们可以很容易的通过函数distance
计算lat,lon之间的欧式距离矩阵
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
dist=distance(mat);
run;
quit;
我在 SAS 9.2 中找不到这个函数
我怎样才能在 9.2 中做到这一点?
谢谢。
Rick Wicklin 在 blog post 中回答了这个问题,他在其中介绍了 distance
函数,但也告诉您如何使用其他两种方法。
- PROC DISTANCE(不是 IML 程序,但会生成类似矩阵的数据集,您可以轻松将其加载到 IML 中)
- 编写自己的模块。您需要创建一个
pairwisedist()
模块来定义如何计算矩阵的欧氏距离。本质上,您确定矩阵中所有行组合之间的差异。然后对差的平方求和并开平方。
PROC DISTANCE
的例子:
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
create matdata from mat [colname={'x1' 'x2' 'x3' 'x4' 'x5'}];
append from mat;
close matdata;
quit;
proc distance data=matdata out=dist method=Euclid nostd;
var interval(x1 x2);
run;
感谢@Joe,我可以在 proc iml
中使用 Rick Wicklin 的答案作为 :
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
start PairwiseDist(x, y);
if ncol(x)^=ncol(y) then return (.); /* Error */
p = nrow(x); q = nrow(y);
idx = T(repeat(1:p, q)); /* index matrix for x */
jdx = shape(repeat(1:q, p), p); /* index matrix for y */
diff = abs(X[idx,] - Y[jdx,]);
return( shape( sqrt(diff[,##]), p ) );
finish;
start EuclideanDistance(x); /* in place of 12.1 DISTANCE function */
y=x;
return( PairwiseDist(x,y) );
finish;
distance=EuclideanDistance(mat);
print distance;
run;
quit;
谢谢 Joe 并感谢 Rick Wicklin。
在SAS 9.3中我们可以很容易的通过函数distance
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
dist=distance(mat);
run;
quit;
我在 SAS 9.2 中找不到这个函数 我怎样才能在 9.2 中做到这一点?
谢谢。
Rick Wicklin 在 blog post 中回答了这个问题,他在其中介绍了 distance
函数,但也告诉您如何使用其他两种方法。
- PROC DISTANCE(不是 IML 程序,但会生成类似矩阵的数据集,您可以轻松将其加载到 IML 中)
- 编写自己的模块。您需要创建一个
pairwisedist()
模块来定义如何计算矩阵的欧氏距离。本质上,您确定矩阵中所有行组合之间的差异。然后对差的平方求和并开平方。
PROC DISTANCE
的例子:
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
create matdata from mat [colname={'x1' 'x2' 'x3' 'x4' 'x5'}];
append from mat;
close matdata;
quit;
proc distance data=matdata out=dist method=Euclid nostd;
var interval(x1 x2);
run;
感谢@Joe,我可以在 proc iml
中使用 Rick Wicklin 的答案作为 :
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
start PairwiseDist(x, y);
if ncol(x)^=ncol(y) then return (.); /* Error */
p = nrow(x); q = nrow(y);
idx = T(repeat(1:p, q)); /* index matrix for x */
jdx = shape(repeat(1:q, p), p); /* index matrix for y */
diff = abs(X[idx,] - Y[jdx,]);
return( shape( sqrt(diff[,##]), p ) );
finish;
start EuclideanDistance(x); /* in place of 12.1 DISTANCE function */
y=x;
return( PairwiseDist(x,y) );
finish;
distance=EuclideanDistance(mat);
print distance;
run;
quit;
谢谢 Joe 并感谢 Rick Wicklin。