我想要应用GroupWise多项式模型,并求解它,以获得每个组的y最高时的x值.我可以一次为一组人做这件事,比如
library(tidyverse)
#Create soem data
df = data.frame(Group = c("A", "A","A","A","A","A","A","A","A","A","A","A",
"B", "B","B","B","B","B","B","B","B","B","B","B"),
price = c(448, 560, 575, 500, 612, 600, 610, 590, 589, 532, 577, 560,
454, 568, 584, 506, 617, 608, 617, 599, 598, 537, 583, 567),
score = c(83, 86, 89, 85, 89, 90, 91, 91, 91, 85, 88, 93,
89, 94, 98, 91, 94, 98, 98, 100, 100, 90, 94, 100))
#Subset group A
data <- df %>%
subset(Group %in% "A")
#Plot it
with(data, plot(x = score, y = price,
pch=19, xlab = "Score", ylab = "Price"))
#Develop the polynomial model
model <- lm(price ~ score + I(score^2), data = data)
xx <- seq(min(data$score, na.rm = T), max(data$score, na.rm = T), 0.01)
lines(xx, model$coefficients[[3]]*xx^2 + model$coefficients[[2]]*xx +
model$coefficients[[1]], col='blue', lwd = 3)
#Optimum score calculation
xoptimum <- -model$coefficients[[2]]/2/model$coefficients[[3]]
abline(v=xoptimum, col="red")
现在,我如何使用tidyverse
框架来拟合和求解这两组的多项式模型?我的原始数据集中有很多组.