假设我使用的是R中的iris个数据集:

data(iris)

summary(iris)
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
 Min.   : 4.300   Min.   : 2.000   Min.   : 1.000   Min.   : 0.100  
 1st Qu.: 5.100   1st Qu.: 2.800   1st Qu.: 1.600   1st Qu.: 0.300  
 Median : 5.800   Median : 3.000   Median : 4.350   Median : 1.300  
 Mean   : 5.843   Mean   : 3.057   Mean   : 3.758   Mean   : 1.199  
 3rd Qu.: 6.400   3rd Qu.: 3.300   3rd Qu.: 5.100   3rd Qu.: 1.800  
 Max.   : 7.900   Max.   : 4.400   Max.   : 6.900   Max.   : 2.500  
       Species  
 setosa    : 50  
 versicolor: 50  
 virginica : 50

我想进行线性回归,其中Petal.Length是因变量,Sepal.Length是自变量.在R中,我如何一次对每Species个类别执行此回归,以获得每个测试的P、R?和F值?

推荐答案

使用by.

by(iris, iris$Species, \(x) summary(lm(Petal.Length ~ Sepal.Length, x)))
# iris$Species: setosa
# 
# Call:
#   lm(formula = Petal.Length ~ Sepal.Length, data = x)
# 
# Residuals:
#   Min       1Q   Median       3Q      Max 
# -0.40856 -0.08027 -0.00856  0.11708  0.46512 
# 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)  
# (Intercept)   0.80305    0.34388   2.335   0.0238 *
#   Sepal.Length  0.13163    0.06853   1.921   0.0607 .
# ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.1691 on 48 degrees of freedom
# Multiple R-squared:  0.07138, Adjusted R-squared:  0.05204 
# F-statistic:  3.69 on 1 and 48 DF,  p-value: 0.0607
# 
# --------------------------------------------------------- 
#   iris$Species: versicolor
# 
# Call:
#   lm(formula = Petal.Length ~ Sepal.Length, data = x)
# 
# Residuals:
#   Min       1Q   Median       3Q      Max 
# -0.68611 -0.22827 -0.04123  0.19458  0.79607 
# 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
# (Intercept)   0.18512    0.51421   0.360     0.72    
# Sepal.Length  0.68647    0.08631   7.954 2.59e-10 ***
#   ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.3118 on 48 degrees of freedom
# Multiple R-squared:  0.5686,  Adjusted R-squared:  0.5596 
# F-statistic: 63.26 on 1 and 48 DF,  p-value: 2.586e-10
# 
# --------------------------------------------------------- 
#   iris$Species: virginica
# 
# Call:
#   lm(formula = Petal.Length ~ Sepal.Length, data = x)
# 
# Residuals:
#   Min       1Q   Median       3Q      Max 
# -0.68603 -0.21104  0.06399  0.18901  0.66402 
# 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
# (Intercept)   0.61047    0.41711   1.464     0.15    
# Sepal.Length  0.75008    0.06303  11.901  6.3e-16 ***
#   ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.2805 on 48 degrees of freedom
# Multiple R-squared:  0.7469,  Adjusted R-squared:  0.7416 
# F-statistic: 141.6 on 1 and 48 DF,  p-value: 6.298e-16

编辑

为了详细说明我的 comments ,我们可以非常容易地提取所需的值

by(iris, iris$Species, \(x) lm(Petal.Length ~ Sepal.Length, x)) |>
  lapply(\(x) {
    with(summary(x), c(r2=r.squared, f=fstatistic, 
                       p=do.call(pf, c(as.list(unname(fstatistic)), lower.tail=FALSE))))
  }) |> do.call(what=rbind)
#                    r2    f.value f.numdf f.dendf            p
# setosa     0.07138289   3.689765       1      48 6.069778e-02
# versicolor 0.56858983  63.263024       1      48 2.586190e-10
# virginica  0.74688439 141.636664       1      48 6.297786e-16

R相关问答推荐

为什么st_join(ob1,ob2,left = True)返回具有比ob1更多功能的sf对象?

根据R中两个变量的两个条件删除带有dspirr的行

当两个图层映射到相同的美学时,隐藏一个图层的图例值

二维样条,严格以一个参数递增

使用Facet_WRAP时更改框图中线的 colored颜色

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

当我们有多个反斜杠和/特殊字符时使用Gsubing

如何在PackageStatus()中列出&q;不可用的包&q;?

根据1个变量绘制 colored颜色 发散的 map ,由另一个变量绘制饱和度,ggplot2不工作

在R函数中使用加号

汇总数据的Sheffe检验的P值(平均值和标准差)

将箭头绘制在图形外部,而不是图形内部

如何使这些react 表对象相互独立?

如何将一个方阵分解成没有循环的立方体

为什么我对圆周率图的蒙特卡罗估计是空的?

如何在R中使用混合GAM模型只对固定的影响因素进行适当的预测?

是否有可能从边界中找到一个点值?

替换在以前工作的代码中有x行&q;错误(geom_sf/gganimate/dow_mark)

如何使投篮在R中保持一致

带查找数据的FCT_REORDER.帧