用 R 和 dplyr 显示拟合值
Showing fitted values with R and dplyr
我有数据框DF。我正在使用 R 和 dplyr 来分析它。
DF 包含:
> glimpse(DF)
Observations: 1244160
Variables:
$ Channel (int) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
$ Row (int) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,...
$ Col (int) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
$ mean (dbl) 776.0667, 786.6000, 833.4667, 752.3333, 831.6667, 772.9333...
我适合它:
Fit <- DF %>%
group_by(Channel) %>%
do(fit = lm(mean ~ Col + poly(Row, 2), data = .))
如何使用数据在 DF 中获取另一列(给定 Channel、Row 和 Col) 由 Fit?
拟合
如果您可以提供除 Channel、Col 和 Row 之外的更多字段,我们会给出更好的指导。现在,我已经为给定的 Channel & Col 准备了解决方案。您可以随时添加 Row 并使用您拥有的其他字段定义 lm。
我认为以下内容对您有用,
library(dplyr)
df = data.frame(Channel=c(rep(0,50),rep(1,50),rep(2,100)),
Row = 1:200,
Col = c(rep(1,50),rep(2,100),rep(3,50)),
mean = rnorm(200))
glimpse(df)
Fit <- df %>%
group_by(Channel,Col) %>%
do(fit = lm(mean ~ poly(Row, 2), data = .))
Fit
Source: local data frame [4 x 3]
Groups: <by row>
Channel Col fit
1 0 1 <S3:lm>
2 1 2 <S3:lm>
3 2 2 <S3:lm>
4 2 3 <S3:lm>
Fit$fit
[[1]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
0.1403 0.2171 -0.6281
[[2]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
-0.07416 -0.39332 0.57889
[[3]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
0.1349 -0.3405 1.5679
[[4]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
0.0379 1.2867 -1.1028
我根据这个问题找到了一个简单的解决方案dplyr::do() requires named function?
Fit <- DF %>%
group_by(Channel) %>%
do({
fit = lm(mean ~ Col + poly(Row, 2), data = .)
pred <- predict(fit)
data.frame(., pred)
})
我有数据框DF。我正在使用 R 和 dplyr 来分析它。
DF 包含:
> glimpse(DF)
Observations: 1244160
Variables:
$ Channel (int) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
$ Row (int) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,...
$ Col (int) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
$ mean (dbl) 776.0667, 786.6000, 833.4667, 752.3333, 831.6667, 772.9333...
我适合它:
Fit <- DF %>%
group_by(Channel) %>%
do(fit = lm(mean ~ Col + poly(Row, 2), data = .))
如何使用数据在 DF 中获取另一列(给定 Channel、Row 和 Col) 由 Fit?
拟合如果您可以提供除 Channel、Col 和 Row 之外的更多字段,我们会给出更好的指导。现在,我已经为给定的 Channel & Col 准备了解决方案。您可以随时添加 Row 并使用您拥有的其他字段定义 lm。
我认为以下内容对您有用,
library(dplyr)
df = data.frame(Channel=c(rep(0,50),rep(1,50),rep(2,100)),
Row = 1:200,
Col = c(rep(1,50),rep(2,100),rep(3,50)),
mean = rnorm(200))
glimpse(df)
Fit <- df %>%
group_by(Channel,Col) %>%
do(fit = lm(mean ~ poly(Row, 2), data = .))
Fit
Source: local data frame [4 x 3]
Groups: <by row>
Channel Col fit
1 0 1 <S3:lm>
2 1 2 <S3:lm>
3 2 2 <S3:lm>
4 2 3 <S3:lm>
Fit$fit
[[1]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
0.1403 0.2171 -0.6281
[[2]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
-0.07416 -0.39332 0.57889
[[3]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
0.1349 -0.3405 1.5679
[[4]]
Call:
lm(formula = mean ~ poly(Row, 2), data = .)
Coefficients:
(Intercept) poly(Row, 2)1 poly(Row, 2)2
0.0379 1.2867 -1.1028
我根据这个问题找到了一个简单的解决方案dplyr::do() requires named function?
Fit <- DF %>%
group_by(Channel) %>%
do({
fit = lm(mean ~ Col + poly(Row, 2), data = .)
pred <- predict(fit)
data.frame(., pred)
})