2

我在下面有一段代码。我无法理解“decompose.graph”究竟是如何工作的。在下面的代码中,我想看看“comps”中有什么。但它显示为一些列表结构,我无法理解。

我还可以使用哪个函数来查看“comps”的图形表示(我尝试了绘图,但它不起作用)

gr<-graph(c(1,2,1,3,1,4,2,3,2,4,3,4),directed=FALSE)

cl<-cliques(gr,min=2,max=2)

  edges <- c()
  for (i in seq_along(cl)) {
    for (j in seq_along(cl)) {
      if ( length(unique(c(cl[[i]], cl[[j]]))) == 3 ) {
        edges <- c(edges, c(i,j))
      }
    }
  }

  plot(clq.graph) <- simplify(graph(edges))
  V(clq.graph)$name <- seq_len(vcount(clq.graph))
  comps <- decompose.graph(clq.graph)

  lapply(comps, function(x) {
    unique(unlist(cl[ V(x)$name ]))
  })
4

1 回答 1

0

一般来说,如果你想在 R 中查看函数背后的代码,你可以在命令中输入函数名并回车。这将为您提供代码的概述。例如:

> decompose

会给你:

function (x, type = c("additive", "multiplicative"), filter = NULL) 
{
    type <- match.arg(type)
    l <- length(x)
    f <- frequency(x)
    if (f <= 1 || length(na.omit(x)) < 2 * f) 
        stop("time series has no or less than 2 periods")
    if (is.null(filter)) 
        filter <- if (!f%%2) 
            c(0.5, rep(1, f - 1), 0.5)/f
        else rep(1, f)/f
    trend <- filter(x, filter)
    season <- if (type == "additive") 
        x - trend
    else x/trend
    periods <- l%/%f
    index <- seq(1L, l, by = f) - 1
    figure <- numeric(f)
    for (i in 1L:f) figure[i] <- mean(season[index + i], na.rm = TRUE)
    figure <- if (type == "additive") 
        figure - mean(figure)
    else figure/mean(figure)
    seasonal <- ts(rep(figure, periods + 1)[seq_len(l)], start = start(x), 
        frequency = f)
    structure(list(x = x, seasonal = seasonal, trend = trend, 
        random = if (type == "additive") x - seasonal - trend else x/seasonal/trend, 
        figure = figure, type = type), class = "decomposed.ts")
}
<environment: namespace:forecast>

我对 decompose.graph 进行了同样的尝试,但我似乎没有可用的功能。这是一个特殊的图书馆吗?我在执行您的代码时也遇到了一些挑战,因为该功能cliques似乎也不可用;包括您正在使用的图书馆会有所帮助。

于 2012-11-27T06:52:58.403 回答