如何在R中获得罐包装分析的uvtop类产品数据

如何在R中获得罐包装分析的uvtop类产品数据,r,plot,R,Plot,我使用来执行R中的某些计算。分析的输出存储在类uvtop的对象中。现在,我想导出分析结果,而不仅仅是在R窗口中绘制 下面是一个示例,使用此包中的示例数据 data(ardieres) events1 <- clust(ardieres, u = 6, tim.cond = 8/365, clust.max = TRUE) npy <- length(events1[,"obs"]) / (diff(range(ardieres[,"time"], na.rm = TRUE)) - d

我使用来执行R中的某些计算。分析的输出存储在类
uvtop
的对象中。现在,我想导出分析结果,而不仅仅是在R窗口中绘制

下面是一个示例,使用此包中的示例数据

data(ardieres)
events1 <- clust(ardieres, u = 6, tim.cond = 8/365, clust.max = TRUE)
npy <- length(events1[,"obs"]) / (diff(range(ardieres[,"time"], na.rm
= TRUE)) - diff(ardieres[c(20945,20947),"time"]))
mle <- fitgpd(events1[,"obs"], thresh = 6, est = "mle")
par(mfrow=c(2,2))
plot(mle, npy = npy)
数据(ardieres)

事件1如果您不想使用R以外的其他应用程序读取文件,只需使用以下程序保存文件:

save(mle, file="myfilename.Rdata")

要将其读回,请加载生成数据的库,然后使用

load("myfilename.Rdata")
将对象加载回工作区或

mle <- readRDS("myfilename.Rds")

mle要获得为返回水平绘制的数据,我们必须深入研究
retlev
函数。基本上,我尽了最大努力剥离所有的绘图,并构建一个所需点的data.frame

getRetLevData <- function (fitted, npy) {
  data  <- fitted$exceed
  loc   <- fitted$threshold[1]
  scale <- fitted$param["scale"]
  shape <- fitted$param["shape"]
  n     <- fitted$nat

  pot.fun <- function(T) {
    p <- rp2prob(T, npy)[, "prob"]
    return(qgpd(p, loc, scale, shape))
  }

  eps <- 10^(-3)

  if (!is.null(fitted$noy)){ 
    npy <- n/fitted$noy
    } else if (missing(npy)) {
    warning("Argument ``npy'' is missing. Setting it to 1.")
    npy <- 1
  }

  xlimsup    <- prob2rp((n - 0.35)/n, npy)[, "retper"]
  fittedLine <- pot.fun(seq(1/npy + eps, xlimsup, length.out = n))
  p_emp      <- (1:n - 0.35)/n
  xPoints    <- 1/(npy * (1 - p_emp))
  empPoints  <- sort(data)
  samp       <- rgpd(1000 * n, loc, scale, shape)
  samp       <- matrix(samp, n, 1000)
  samp       <- apply(samp, 2, sort)
  samp       <- apply(samp, 1, sort)
  ci_inf     <- samp[25, ]
  ci_sup     <- samp[975, ]

  rst <- data.frame(xPoints, fittedLine, empPoints, 
                    ci_inf, ci_sup)
}

x <- getRetLevData(mle, npy)
head(x)
#    fittedX fittedLine  xPoints empPoints   ci_inf   ci_sup
#1  1.001000   6.003716 1.011535      6.09 6.001557 6.239971
#2  3.891288  11.678503 1.029810      6.09 6.014536 6.363070
#3  6.781577  14.402517 1.048758      6.09 6.042065 6.470195
#4  9.671865  16.282306 1.068416      6.19 6.074348 6.583290
#5 12.562153  17.740710 1.088825      6.44 6.114193 6.684118
#6 15.452441  18.942354 1.110029      6.45 6.146098 6.812058
write.csv(x, "my_pot_results.csv")

getRetLevData请发布一个可复制的示例。例如,读。几乎可以肯定的是,我们可以做您想做的事情,但我们需要的信息远远不止是包名和对象类。(由函数
fitgpd
?)谢谢,我已经相应地更新了问题。第三条指令
npy您所说的“在其他地方复制返回水平图(右下面板)的数据”是什么意思?其他地方是否意味着:在其他人的计算机上的R、excel、python、SAS等?你所要求的需要一些调查工作。首先运行
str(mle)
,了解要打印的对象中有什么。然后,运行
getAnywhere(plot.uvpot)
。这将告诉您绘制第四个图形的函数是
getAnywhere(retlev.uvpot)
。如果你现在有了源代码,你就可以知道你需要什么样的数据结构了。谢谢,这看起来像是一个黑魔法,但它是有效的。最让我困惑的是“0.35”。你介意评论一下这部分吗?无论如何,我现在将尝试应用于我的特定问题,然后将问题标记为已解决。我不知道为什么在数学中使用
0.35
,也不知道
pot.fun
除了调用
rp2prob
。谢谢,我最终成功了,但它需要最后的调整(这花了我几个小时的绝望和逆向工程才弄明白)。拟合曲线的x坐标与点和虚线不同。我必须编辑函数以输出两个x向量,但它现在工作得很好。谢谢!我注意到了。这就是为什么我的函数返回
fittedx
xpoints
,我应该更清楚地说明这一点
getRetLevData <- function (fitted, npy) {
  data  <- fitted$exceed
  loc   <- fitted$threshold[1]
  scale <- fitted$param["scale"]
  shape <- fitted$param["shape"]
  n     <- fitted$nat

  pot.fun <- function(T) {
    p <- rp2prob(T, npy)[, "prob"]
    return(qgpd(p, loc, scale, shape))
  }

  eps <- 10^(-3)

  if (!is.null(fitted$noy)){ 
    npy <- n/fitted$noy
    } else if (missing(npy)) {
    warning("Argument ``npy'' is missing. Setting it to 1.")
    npy <- 1
  }

  xlimsup    <- prob2rp((n - 0.35)/n, npy)[, "retper"]
  fittedLine <- pot.fun(seq(1/npy + eps, xlimsup, length.out = n))
  p_emp      <- (1:n - 0.35)/n
  xPoints    <- 1/(npy * (1 - p_emp))
  empPoints  <- sort(data)
  samp       <- rgpd(1000 * n, loc, scale, shape)
  samp       <- matrix(samp, n, 1000)
  samp       <- apply(samp, 2, sort)
  samp       <- apply(samp, 1, sort)
  ci_inf     <- samp[25, ]
  ci_sup     <- samp[975, ]

  rst <- data.frame(xPoints, fittedLine, empPoints, 
                    ci_inf, ci_sup)
}

x <- getRetLevData(mle, npy)
head(x)
#    fittedX fittedLine  xPoints empPoints   ci_inf   ci_sup
#1  1.001000   6.003716 1.011535      6.09 6.001557 6.239971
#2  3.891288  11.678503 1.029810      6.09 6.014536 6.363070
#3  6.781577  14.402517 1.048758      6.09 6.042065 6.470195
#4  9.671865  16.282306 1.068416      6.19 6.074348 6.583290
#5 12.562153  17.740710 1.088825      6.44 6.114193 6.684118
#6 15.452441  18.942354 1.110029      6.45 6.146098 6.812058
write.csv(x, "my_pot_results.csv")