Skip to content

Commit e7adf02

Browse files
Antonov548maelle
authored andcommitted
add R_igraph_power_law_fit_new with p-value
1 parent a625da0 commit e7adf02

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

R/fit.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ power.law.fit.old <- function(x, xmin = NULL, start = 2, ...) {
198198
alpha
199199
}
200200

201-
power.law.fit.new <- function(data, xmin = -1, force.continuous = FALSE) {
201+
power.law.fit.new <- function(data, xmin = -1, force.continuous = FALSE, p.value = FALSE) {
202202
# Argument checks
203203
data <- as.numeric(data)
204204
xmin <- as.numeric(xmin)
205205
force.continuous <- as.logical(force.continuous)
206206

207207
on.exit(.Call(R_igraph_finalizer))
208208
# Function call
209-
res <- .Call(R_igraph_power_law_fit, data, xmin, force.continuous)
209+
res <- .Call(R_igraph_power_law_fit_new, data, xmin, force.continuous, p.value)
210210

211211
res
212212
}

src/cpp11.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ extern SEXP R_igraph_path_length_hist(SEXP, SEXP);
361361
extern SEXP R_igraph_permute_vertices(SEXP, SEXP);
362362
extern SEXP R_igraph_personalized_pagerank(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
363363
extern SEXP R_igraph_personalized_pagerank_vs(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
364-
extern SEXP R_igraph_power_law_fit(SEXP, SEXP, SEXP);
364+
extern SEXP R_igraph_power_law_fit_new(SEXP, SEXP, SEXP, SEXP);
365365
extern SEXP R_igraph_preference_game(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
366366
extern SEXP R_igraph_pseudo_diameter(SEXP, SEXP, SEXP, SEXP);
367367
extern SEXP R_igraph_pseudo_diameter_dijkstra(SEXP, SEXP, SEXP, SEXP, SEXP);
@@ -824,7 +824,7 @@ static const R_CallMethodDef CallEntries[] = {
824824
{"R_igraph_permute_vertices", (DL_FUNC) &R_igraph_permute_vertices, 2},
825825
{"R_igraph_personalized_pagerank", (DL_FUNC) &R_igraph_personalized_pagerank, 8},
826826
{"R_igraph_personalized_pagerank_vs", (DL_FUNC) &R_igraph_personalized_pagerank_vs, 8},
827-
{"R_igraph_power_law_fit", (DL_FUNC) &R_igraph_power_law_fit, 3},
827+
{"R_igraph_power_law_fit_new", (DL_FUNC) &R_igraph_power_law_fit_new, 4},
828828
{"R_igraph_preference_game", (DL_FUNC) &R_igraph_preference_game, 7},
829829
{"R_igraph_pseudo_diameter", (DL_FUNC) &R_igraph_pseudo_diameter, 4},
830830
{"R_igraph_pseudo_diameter_dijkstra", (DL_FUNC) &R_igraph_pseudo_diameter_dijkstra, 5},

src/rinterface_extra.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8331,6 +8331,59 @@ SEXP R_igraph_incident_edges(SEXP pgraph, SEXP pe, SEXP pmode) {
83318331
return result;
83328332
}
83338333

8334+
SEXP R_igraph_power_law_fit_new(SEXP data, SEXP xmin, SEXP force_continuous, SEXP pvalue)
8335+
{
8336+
igraph_vector_t c_data;
8337+
igraph_plfit_result_t c_res;
8338+
igraph_real_t c_xmin;
8339+
igraph_bool_t c_force_continuous, c_compute_pvalue;
8340+
SEXP result, names;
8341+
8342+
SEXP r_result;
8343+
8344+
R_SEXP_to_vector(data, &c_data);
8345+
IGRAPH_R_CHECK_REAL(xmin);
8346+
c_xmin = REAL(xmin)[0];
8347+
IGRAPH_R_CHECK_BOOL(force_continuous);
8348+
c_force_continuous = LOGICAL(force_continuous)[0];
8349+
IGRAPH_R_CHECK_BOOL(pvalue);
8350+
c_compute_pvalue = LOGICAL(pvalue)[0];
8351+
8352+
IGRAPH_R_CHECK(igraph_power_law_fit(&c_data, &c_res, c_xmin, c_force_continuous));
8353+
8354+
if (c_compute_pvalue) {
8355+
igraph_real_t p;
8356+
igraph_plfit_result_calculate_p_value(&c_res, &p, 0.001);
8357+
8358+
PROTECT(result=NEW_LIST(6));
8359+
PROTECT(names=NEW_CHARACTER(6));
8360+
8361+
SET_VECTOR_ELT(result, 5, Rf_ScalarReal(p));
8362+
SET_STRING_ELT(names, 5, Rf_mkChar("KS.p"));
8363+
} else {
8364+
PROTECT(result=NEW_LIST(5));
8365+
PROTECT(names=NEW_CHARACTER(5));
8366+
}
8367+
8368+
SET_VECTOR_ELT(result, 0, Rf_ScalarLogical(c_res.continuous));
8369+
SET_VECTOR_ELT(result, 1, Rf_ScalarReal(c_res.alpha));
8370+
SET_VECTOR_ELT(result, 2, Rf_ScalarReal(c_res.xmin));
8371+
SET_VECTOR_ELT(result, 3, Rf_ScalarReal(c_res.L));
8372+
SET_VECTOR_ELT(result, 4, Rf_ScalarReal(c_res.D));
8373+
8374+
SET_STRING_ELT(names, 0, Rf_mkChar("continuous"));
8375+
SET_STRING_ELT(names, 1, Rf_mkChar("alpha"));
8376+
SET_STRING_ELT(names, 2, Rf_mkChar("xmin"));
8377+
SET_STRING_ELT(names, 3, Rf_mkChar("logLik"));
8378+
SET_STRING_ELT(names, 4, Rf_mkChar("KS.stat"));
8379+
SET_NAMES(result, names);
8380+
8381+
r_result = result;
8382+
8383+
UNPROTECT(2);
8384+
return(r_result);
8385+
}
8386+
83348387
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++C */
83358388
/* C */
83368389
/* Given a HIERARCHIC CLUSTERING, described as a sequence of C */

0 commit comments

Comments
 (0)