如何在 data.frame 中拟合数据组并获取每个组的拟合系数?

How to fit data groups in a data.frame and get fitting coefficients for each group?

我正在尝试在数据框的两列之间编写一个多项式函数。 在这两列中,我对名为 Group1Group2 的行进行了分组。 我想使用

适合这些组 R~V
fit_all <- summary(lm(R ~ poly(V,2,raw=TRUE), data = df, subset = state))

但我收到警告消息

In summary.lm(lm(R ~ poly(V, 2, raw = TRUE), data = df_rep, subset = state)) : essentially perfect fit: summary may be unreliable

我检查了这个可能与 NA 值相关的错误。由于我的真实数据和 df 数据中都没有 NA 值,所以我被困在了这一点上。

最后,对于每个 Group1Group2,我想为每个组配件提取 coefficients

请看看我的可重现示例

set.seed(1)
No <- rep(seq(1,4,1),each=21)
AC <- rep(rep(c(78,110),each=1),times=length(No)/2)
state <- rep(rep(c("Group 1","Group 2"),2),each=21)
V <- rep(seq(100,2100,100),times=4)
R = sort(replicate(4, sample(5000:6000,21)))

df <- data.frame(No,AC,V,R,state)

head(df)

   No  AC   V    R   state
 1  1  78 100 5004 Group 1
 2  1 110 200 5014 Group 1
 3  1  78 300 5030 Group 1
 4  1 110 400 5039 Group 1
 5  1  78 500 5057 Group 1
 6  1 110 600 5068 Group 1

检查这个使用 dplyr 和 broom 包的例子。

library(dplyr)
library(broom)

set.seed(1)
No <- rep(seq(1,4,1),each=21)
AC <- rep(rep(c(78,110),each=1),times=length(No)/2)
state <- rep(rep(c("Group 1","Group 2"),2),each=21)
V <- rep(seq(100,2100,100),times=4)
R = sort(replicate(4, sample(5000:6000,21)))

df <- data.frame(No,AC,V,R,state)


df2 = df %>% 
  group_by(state) %>% # group by variable state
  do(data.frame(model = tidy(lm(R~poly(V,2,raw=TRUE), data=.)))) %>% # for each group run a linear fit and save the output as a date table
  ungroup # forget about your initial grouping

现在您有一个数据集 (dt2),其中包含来自每个类别的线性模型输出的一些信息作为列。然后您可以像处理任何其他数据集一样处理 dt2。 例如:

df2 %>% filter(state=="Group 1") # get info only for Group 1
df2 %>% filter(state=="Group 1") %>% select(model.term, model.estimate) # get only variables and coefficients for Group 1