相关疑难解决方法(0)

ggplot2 geom_smooth,方法的扩展模型= lm

我想用来geom_smooth从某个线性回归模型中得到拟合线.

在我看来,公式只能采取x,y而不是任何额外的参数.

为了更清楚地显示我想要的东西:

library(dplyr)
library(ggplot2)
set.seed(35413)
df <- data.frame(pred = runif(100,10,100),
           factor = sample(c("A","B"), 100, replace = TRUE)) %>%
  mutate(
    outcome = 100 + 10*pred + 
    ifelse(factor=="B", 200, 0) + 
    ifelse(factor=="B", 4, 0)*pred +
    rnorm(100,0,60))
Run Code Online (Sandbox Code Playgroud)

ggplot(df, aes(x=pred, y=outcome, color=factor)) +
  geom_point(aes(color=factor)) +
  geom_smooth(method = "lm") +
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

我生产的装配线,由于color=factor选项,基本上是线性模型的输出lm(outcome ~ pred*factor, df)

在此输入图像描述

但是,在某些情况下,我更喜欢将线条作为不同模型拟合的输出,例如lm(outcome ~ pred + factor, df),我可以使用以下内容:

fit <- lm(outcome ~ pred+factor, df)
predval <- expand.grid(
  pred = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

17
推荐指数
1
解决办法
1839
查看次数

如何在ggplot2散点图上覆盖lm对象的一行

我有一些数据,

calvarbyruno.1<-structure(list(Nominal = c(1, 3, 6, 10, 30, 50, 150, 250), Run = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"), 
    PAR = c(1.25000000000000e-05, 0.000960333333333333, 0.00205833333333334, 
    0.00423333333333333, 0.0322333333333334, 0.614433333333334, 
    1.24333333333333, 1.86333333333333), PredLin = c(-0.0119152187070942, 
    0.00375925114245899, 0.0272709559167888, 0.0586198956158952, 
    0.215364594111427, 0.372109292606959, 1.15583278508462, 1.93955627756228
    ), PredQuad = c(-0.0615895732702735, -0.0501563307416599, 
    -0.0330831368244257, -0.0104619953693943, 0.100190275883806, 
    0.20675348710041, 0.6782336426345, 1.04748729725370)), .Names = c("Nominal", 
"Run", "PAR", "PredLin", "PredQuad"), row.names = c(NA, 8L), class = "data.frame")
calweight <- -2
Run Code Online (Sandbox Code Playgroud)

为此我创建了线性和二次lm模型

callin.1<-lm(PAR~Nominal,data=calvarbyruno.1,weight=Nominal^calweight)
calquad.1<-lm(PAR~Nominal+I(Nominal^2),data=calvarbyruno.1,weight=Nominal^calweight) …
Run Code Online (Sandbox Code Playgroud)

graphics r ggplot2

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

ggplot2 ×2

r ×2

graphics ×1