如何处理拟合值太多的二次模型?

问题描述 投票:0回答:1

我正在尝试将二次回归模型拟合到数据集,然后将曲线绘制在散点图上。该数据集是关于电视节目中人物的剧集数量和上映时间。

我绘制了一个散点图,在x轴上有情节,在y轴上有屏幕时间,这很好用。

然后我按如下方式创建模型:

#ordering
gottemp <- got[order(got$episodes),]

#plotting
plot(screentime~episodes, data = gottemp, xlab ="Number of episodes", ylab = "Screentime (minutes)", col=c("blue","red")[gender], pch=c(1,2)[gender])
legend("topleft",pch = c(1,2),col=c("blue","red"),c("female","male"))
title("Plot of Screentimes vs Number of Episodes")

#creating 3model and plotting line
model <- lm(screentime~episodes+I(episodes^2), data = got)
lines(fitted(model))

这为我提供了具有正确系数的模型,但是绘制的线并不是预期的。当我查看模型时,我发现有113个拟合值,我认为这是由于某些角色具有相同的情节数,因此要解决此问题,我认为每种情节数都应该只有一个拟合值。

r regression linear-regression
1个回答
0
投票

类似

nd <- data.frame(episodes=seq(min(episodes), max(episodes), length=51)
nd$screentime <- predict(model, newdata=nd)
with(nd, lines(episodes, screentime))

应该做你想做的。某处可能有重复...

© www.soinside.com 2019 - 2024. All rights reserved.