代码之家  ›  专栏  ›  技术社区  ›  owl

在R中为变量上标

  •  2
  • owl  · 技术社区  · 9 年前

    我是R的初学者,我试图把一个包含上标变量的方程放在绘图上。我知道如何在绘图上打印上标字母,但我想不出插入变量的方法。以下是我的代码:

    DF <- data.frame(X <- c(1, 2, 3, 4, 5, 6, 7), Y <- c(0, 0, 1, 0, 1, 1, 1))
    
    # Logistic regression
    model <- glm(Y ~ X, family = binomial, data = DF)
    
    # Plot raw data
    raw_plot <- plot(DF$X, DF$Y,
            xlab = 'X', ylab = 'Y'
            )
    
    # Add prediction curve
    curve(predict(model, data.frame(X = x), type = 'response'), add = TRUE)
    
    # Get coefficients
    intercept <- summary(model)$coefficients[1] # -4.361418
    coefficient <- summary(model)$coefficients[2] # 1.250679
    
    superscript.part <- sprintf('%.2f + %.2f*x', intercept, coefficient)
    
    text(5, 0.2, labels = expression(paste('y = 1/(1 + 1/e'^'superscript.part'*')')))
    # This will superscript 'superscript' and not the actual variable
    

    这就是我得到的。

    enter image description here

    1 回复  |  直到 9 年前
        1
  •  3
  •   Mako212    9 年前

    text(5,.2, bquote("y = 1/(1 + 1/e" ^~{.(superscript.part)} ~ ")"))