我遇到了困难:
Q.1。如何找出已加载的R模块?
SELECT * FROM r_typenames();
显示可用的类型,但是检查是否加载了Kendall( X, Y )
呢?例如,documentation显示:
CREATE TABLE plr_modules (
modseq int4,
modsrc text
);
这似乎允许插入记录来指示要加载Kendall
,但是下面的代码在语法上没有解释如何确保它被加载:
INSERT INTO plr_modules
VALUES (0, 'pg.test.module.load <-function(msg) {print(msg)}');
Q.2。如果您尝试加载Kendall
,上述行会是什么样子?
Q.3。是否适用?
使用“synaptic”软件包管理器已安装以下软件包:
r-base
r-base-core
r-base-dev
r-base-html
r-base-latex
r-cran-acepack
r-cran-boot
r-cran-car
r-cran-chron
r-cran-cluster
r-cran-codetools
r-cran-design
r-cran-foreign
r-cran-hmisc
r-cran-kernsmooth
r-cran-lattice
r-cran-matrix
r-cran-mgcv
r-cran-nlme
r-cran-quadprog
r-cran-robustbase
r-cran-rpart
r-cran-survival
r-cran-vr
r-recommended
Q.4。我如何知道肯德尔是否在那里?
Q.5。如果不是,我如何找出它所在的包装?
Q.6。如果它不在适合安装apt-get
(aptitude
,synaptic
,dpkg
的软件包中,那么你有什么),如何在Ubuntu上安装它?
Q.7。安装步骤在哪里记录?
我有以下代码:
EXECUTE 'SELECT '
'regr_slope( amount, year_taken ),'
'regr_intercept( amount, year_taken ),'
'corr( amount, year_taken ),'
'sum( measurements ) AS total_measurements '
'FROM temp_regression'
INTO STRICT slope, intercept, correlation, total_measurements;
此代码调用PostgreSQL函数corr
来计算Pearson对数据的相关性。理想情况下,我想执行以下操作(通过切换corr
plr_kendall
):
EXECUTE 'SELECT '
'regr_slope( amount, year_taken ),'
'regr_intercept( amount, year_taken ),'
'plr_kendall( amount, year_taken ),'
'sum( measurements ) AS total_measurements '
'FROM temp_regression'
INTO STRICT slope, intercept, correlation, total_measurements;
Q.8。我是否必须自己写plr_kendall
?
Q.9。我在哪里可以找到一个简单的示例:
例如,最后两个步骤是否会是:
create or replace function plr_kendall( _float8, _float8 ) returns float as '
agg_kendall(arg1, arg2)
' language 'plr';
CREATE AGGREGATE agg_kendall (
sfunc = plr_array_accum,
basetype = float8, -- ???
stype = _float8, -- ???
finalfunc = plr_kendall
);
然后是上面的SELECT
?
谢谢!
答案 0 :(得分:6)
这些步骤列出了如何使用PL / R从PostgreSQL调用R函数。
您必须已安装PostgreSQL,R和PL / R.
Kendall
)sudo su - postgres
R
$HOME/R/x86_64-pc-linux-gnu-library/2.9/
):
install.packages("Kendall", dependencies = TRUE)
CREATE TABLE plr_modules (
modseq int4,
modsrc text
);
INSERT INTO plr_modules
VALUES (0, 'library(Kendall)' );
SELECT * FROM reload_plr_modules();
):
sudo /etc/init.d/postgresql-8.4 restart
CREATE OR REPLACE FUNCTION climate.plr_corr_kendall(
double precision[],
double precision[] )
RETURNS double precision AS
$BODY$
Kendall(arg1, arg2)
$BODY$
LANGUAGE 'plr' VOLATILE STRICT;
此函数执行从数据库收集数据并创建两个数组的工作。这些数组被传递到plr_corr_kendall
包装函数。
CREATE OR REPLACE FUNCTION climate.analysis_vector()
RETURNS double precision AS
$BODY$
DECLARE
v_year_taken double precision[];
v_amount double precision[];
i RECORD;
BEGIN
FOR i IN (
SELECT
extract(YEAR FROM m.taken) AS year_taken,
avg( m.amount ) AS amount
FROM
climate.city c,
climate.station s,
climate.station_category sc,
climate.measurement m
WHERE
c.id = 5148 AND
earth_distance(
ll_to_earth(c.latitude_decimal,c.longitude_decimal),
ll_to_earth(s.latitude_decimal,s.longitude_decimal)) <= 30 AND
s.elevation BETWEEN 0 AND 3000 AND
s.applicable AND
sc.station_id = s.id AND
sc.category_id = 1 AND
extract(YEAR FROM sc.taken_start) >= 1900 AND
extract(YEAR FROM sc.taken_end) <= 2009 AND
m.station_id = s.id AND
m.taken BETWEEN sc.taken_start AND sc.taken_end AND
m.category_id = sc.category_id
GROUP BY
extract(YEAR FROM m.taken)
ORDER BY
extract(YEAR FROM m.taken)
) LOOP
SELECT array_append( v_year_taken, i.year_taken ) INTO v_year_taken;
SELECT array_append( v_amount, i.amount::double precision ) INTO v_amount;
END LOOP;
RAISE NOTICE '%', v_year_taken;
RAISE NOTICE '%', v_amount;
RETURN climate.plr_corr_kendall( v_year_taken, v_amount );
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
按如下方式测试功能:
SELECT
*
FROM
climate.analysis_vector();
一个数字:-0.0578900910913944