【问题标题】:Error in seq(min(x), max(x), length = timesteps) : object 'a' not found within functionseq (min(x), max(x), length = timesteps) 中的错误:在函数中找不到对象“a”
【发布时间】:2017-09-18 14:31:51
【问题描述】:

我正在尝试构建一个函数来绘制我的结果。函数中的第二行会给我以下错误:

seq 中的错误(min(x), max(x), length = timesteps) : object 'a' not found'

显然 a 和 b 存在,因为 gam 能够读取它们,并且函数的第一行也可以正常工作。当我在函数之外使用代码时,它也可以工作......我不知道我在那里缺少什么。

library(mgcv)
library(ggplot2)
theme_set(theme_bw())

set.seed(100)
dd <- data.frame(a=1:100,b=round(rnorm(100,mean=100),1))

m <- gam(formula = b ~ s(a, k = 64, bs = "ad"), data = dd, method = "REML", select = TRUE)

plot<-function(model,x,y,timesteps){
ggplot(model, aes_string(x = deparse(substitute(x)), y=deparse(substitute(y)))) +  geom_point()
pred <- with(model, data.frame(x = seq(min(x), max(x), length = timesteps)))

# Error in seq(min(x), max(x), length = timesteps) : object 'a' not found

pred <- cbind(pred, as.data.frame(predict(m, pred, se.fit = TRUE, unconditional = TRUE)))
pred <- transform(pred,
                  Fitted = fit,
                  Upper = fit + (2 * se.fit),
                  Lower = fit - (2 * se.fit))


ggplot(model, aes(x = x, y=y)) +
    geom_point() +
    geom_ribbon(data = pred, mapping = aes(x = x, ymin = Lower, ymax = Upper),
                fill = "#9ecf7f", colour = NA, alpha = 0.7, inherit.aes  = FALSE) +
    geom_path(data = pred, mapping = aes(x = x, y = Fitted), inherit.aes = FALSE,
              size = 1)
}

plot(dd,a,b,64)

【问题讨论】:

    标签: r function object parameter-passing gam


    【解决方案1】:

    请检查这是否有效。我刚刚覆盖了你所有的功能:

    plot <- function(model, x, y, timesteps){
        library(ggplot2)
    
        # Use get instead of aes_string
        ggplot(model, aes(get(x), get(y))) +  
            geom_point()
    
        # To extract min X value we'll use it's position in data frame
        whichX <- colnames(model) == x
        pred <- data.frame(seq(min(model[, whichX]), max(model[, whichX]), 
                               length = timesteps))
        colnames(pred) <- x
        pred <- cbind(pred, as.data.frame(predict(m, pred, se.fit = TRUE, unconditional = TRUE)))
        pred <- transform(pred,
                          Fitted = fit,
                          Upper = fit + (2 * se.fit),
                          Lower = fit - (2 * se.fit))
    
        ggplot(model, aes(get(x), get(y))) +
            geom_point() +
            geom_ribbon(data = pred, mapping = aes(x = get(x), ymin = Lower, ymax = Upper),
                        fill = "#9ecf7f", colour = NA, alpha = 0.7, inherit.aes  = FALSE) +
            geom_path(data = pred, mapping = aes(x = get(x), y = Fitted), inherit.aes = FALSE,
                      size = 1)
    }
    # quote your variables before passing them
    # e.g.: a is not defined and you have to "a"
    plot(dd, "a", "b", 64)
    

    【讨论】:

    • 改正错字即可。错字:在&lt; 之后和- 之前plot &lt; -function 中的空格。
    • @RuiBarradas 谢谢!
    • 非常感谢,一切正常!我永远不会想到使用 get
    猜你喜欢
    • 2020-07-22
    • 1970-01-01
    • 2013-11-18
    • 2021-12-18
    • 2022-07-27
    • 2018-06-28
    • 2016-04-22
    • 2021-04-03
    • 2022-01-16
    相关资源
    最近更新 更多