SSAS (статистика половой сегрегации и агрегации) в R - вызов C - PullRequest
0 голосов
/ 30 января 2019

Я использую следующий код, найденный в этом приложении к статье https://wiley.figshare.com/articles/Supplement_1_R_code_used_to_format_the_data_and_compute_the_SSAS_/3528698/1, для расчета статистики половой сегрегации и агрегации в R - но продолжаю получать следующую ошибку - предположительно, существует проблема с вызовом функциииз C, но я не могу ее решить.

# Main function, computes both the SSAS (Sexual Segregation and
# Aggregation Statistic) and the 95% limits of SSAS
# under the assumption of random association of both sexes

SSAS <- function(x, conf.int = 0.95, B = 10000)
{
 x <- as.matrix(x)
 nr <- nrow(x)
 nc <- ncol(x)
 sr <- rowSums(x)
 sc <- colSums(x)
 n <- sum(x)
 E <- outer(sr, sc, "*")/n
 dimnames(E) <- dimnames(x)
 tmp <- .C("chisqsim", as.integer(nr), as.integer(nc),as.integer(sr),
 as.integer(sc), as.integer(n), as.integer(B), as.double(E), integer(nr * nc),
 double(n + 1), integer(nc), results = double(B), PACKAGE = "stats")
 obs <- sum(sort((x - E)^2/E, decreasing = TRUE))/n
 sim <- tmp$results/n
 p0 <- (1 - conf.int)/2
 return(c(obs, quantile(sim, p0), quantile(sim, 1 -p0)))
}

# This function formats data to be run with the SSAS function

splitmfd <- function(mfd) {
 loca1 <- function(x) {
  x <- t(x[, 1:2])
  dimnames(x) <- list(c("mal", "fem"), as.character(1:ncol(x)))
  x
 }
 l0 <- split(mfd, mfd$mon)
 lapply(l0, loca1)
}

# Example 1: Isard

rup <- read.table("http://pbil.univ-lyon1.fr/R/donnees/mfdrupicapra.txt",
h = T)
  # Load data from the web

plot1 <- function(w, titre = "") {
 plot(1:12, w[, 1], ylim = range(w), ax = F, pch = 19,
 type = "n", ylab = "IK", xlab = "")
 title(main = titre)
 box()
 axis(1, 1:12, c("Jan", "Feb", "Mar", "Apr", "May",
 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
 axis(2, pretty(range(w)), tck = 1)
 polyx <- c(1:12, 12:1)
 polyy <- c(w[, 3], rev(w[, 2]))
 polygon(polyx, polyy, col = grey(0.9))
 points(w[, 1], pch = 19, type = "b")
}
  # Function to plot data and 95\ levels of significance

l1 <- splitmfd(rup)
  # Format data to be used with SSAS function
w <- matrix(unlist(lapply(l1, SSAS)), ncol = 3, byrow = T)
  # "w" is a matrix having 3 columns and 12 rows. In the first columns are
  # the SSAS estimates for each month, and the lower and upper limits in columns 2
  # and 3 respectively.

plot1(w, "Isard")
  # Plot figure 3a

# Example 2: Red deer

cer <- read.table("http://pbil.univ-lyon1.fr/R/donnees/mfdcervus.txt",
h = T)
l1 <- splitmfd(cer)
w <- matrix(unlist(lapply(l1, SSAS)), ncol = 3, byrow = T)
plot1(w, "Red deer")
  # Plot figure 3c

# Example 3: Roe deer

cap <- read.table("http://pbil.univ-lyon1.fr/R/donnees/mfdcapreolus.txt",
h = T)
l1 <- splitmfd(cap)
w <- matrix(unlist(lapply(l1, SSAS)), ncol = 3, byrow = T)
plot1(w, "Roe deer")
# Plot figure 3e

Вот моя ошибка:

w <- матрица (unlist (lapply (l1, SSAS))), ncol = 3, byrow = T) Ошибка в .C ("chisqsim", as.integer (nr), as.integer (nc), as.integer (sr),: "chisqsim" недоступен для .C () для пакета "stats)"</p>

1 Ответ

0 голосов
/ 30 января 2019

Это простой пример, почему вы не должны вызывать внутренние функции C в коде R пользователя.R внутренние органы могут (и делают) измениться.Здесь эти изменения актуальны: «больше использования .Call» профессора Рипли .

Таким образом, вы можете изменить функцию на:

SSAS <- function(x, conf.int = 0.95, B = 10000)
{
  x <- as.matrix(x)
  nr <- nrow(x)
  nc <- ncol(x)
  sr <- rowSums(x)
  sc <- colSums(x)
  n <- sum(x)
  E <- outer(sr, sc, "*")/n
  dimnames(E) <- dimnames(x)
  tmp <- .Call(stats:::C_chisq_sim, sr, sc, B, E)
  obs <- sum(sort((x - E)^2/E, decreasing = TRUE))/n
  sim <- tmp/n
  p0 <- (1 - conf.int)/2
  return(c(obs, quantile(sim, p0), quantile(sim, 1 -p0)))
}

Затем код работает, но я не проверил правильность.Таким образом, никаких гарантий (как обычно на Stack Overflow).

...