R 从数据集中抽取20个样本,绘制回归线和总体回归线

R 从数据集中抽取20个样本,绘制回归线和总体回归线,r,regression,sample,R,Regression,Sample,我有一个数据集,包含两个变量:学习时间和成绩。我想从这个数据集中抽取大约100个样本,每个样本20个,并显示100条回归线以及原始回归线。有什么建议吗 库(ggplot2) #>警告:包“ggplot2”是在R版本3.6.3下生成的 等级=读取.csv(“https://www.dropbox.com/s/me6wiww943hzddj/grades.csv?dl=1") qplot(小时、等级、数据=等级、geom=“点”)+geom_平滑(方法=lm) #>`geom_smooth()`使用

我有一个数据集,包含两个变量:学习时间和成绩。我想从这个数据集中抽取大约100个样本,每个样本20个,并显示100条回归线以及原始回归线。有什么建议吗

库(ggplot2)
#>警告:包“ggplot2”是在R版本3.6.3下生成的
等级=读取.csv(“https://www.dropbox.com/s/me6wiww943hzddj/grades.csv?dl=1")
qplot(小时、等级、数据=等级、geom=“点”)+geom_平滑(方法=lm)
#>`geom_smooth()`使用公式'y~x'
使用循环:

n=100
for(i in 1:n){
  df = grades[sample(1:nrow(grades), 20),]
  g = g + geom_smooth(method = lm, data=df, color="red", size=0.5, alpha = 0)
}
plot(g)
输出:

我鼓励你把它的美学搞得一团糟,添加一条虚线,例如:

使用循环:

n=100
for(i in 1:n){
  df = grades[sample(1:nrow(grades), 20),]
  g = g + geom_smooth(method = lm, data=df, color="red", size=0.5, alpha = 0)
}
plot(g)
输出:

我鼓励你把它的美学搞得一团糟,添加一条虚线,例如:


我们也可以使用
sample\n

library(dplyr)
library(ggplot2)
g <- qplot(hours, grade, data = grades, geom = "point") +
      geom_smooth(method = lm)
n <- 100
for(i in seq_len(n)) {
       tmpdat <- grades %>%
                  sample_n(20)
        g <- g +
         geom_smooth(method = lm, data = tmpdat, color = 'red',
                 size = 0.5, alpha = 0)
    }

plot(g)
库(dplyr)
图书馆(GG2)

g我们也可以使用
sample\n

library(dplyr)
library(ggplot2)
g <- qplot(hours, grade, data = grades, geom = "point") +
      geom_smooth(method = lm)
n <- 100
for(i in seq_len(n)) {
       tmpdat <- grades %>%
                  sample_n(20)
        g <- g +
         geom_smooth(method = lm, data = tmpdat, color = 'red',
                 size = 0.5, alpha = 0)
    }

plot(g)
库(dplyr)
图书馆(GG2)

g
g=g+geom_平滑(method=lm,data=df,color=“red”,size=0.5,alpha=0)
在这行中,geom_平滑如何知道x轴和y轴变量?它使用传递到原始绘图的参数
g
,默认值为
qplot
g=g+geom_平滑(method=lm,data=df,color=“red”,size=0.5,alpha=0)
在这行中,geom_smooth是如何知道x轴和y轴变量的?它使用传递到原始绘图的参数
g
,默认值为
qplot