[英]Linear fit predictions with changing values in R?
I've got some golf data that looks like this:我有一些看起来像这样的高尔夫数据:
ID round GIR score
Tiger Woods 3 0.666666667 68
Tiger Woods 2 0.611111111 75
Tiger Woods 1 0.666666667 71
Adam Scott 3 0.611111111 68
Adam Scott 2 0.888888889 68
Adam Scott 1 0.666666667 66
And I'm trying to make a linear model that says "based on greens in regulation, my round four score is going to be this."我正在尝试制作一个线性模型,上面写着“根据规则中的果岭,我的第四轮得分将是这个。” Here's my script so far.到目前为止,这是我的脚本。
#load in data
gir2 <- read.csv("girforscore.csv")
#establish linear model
fit <- lm(score ~ GIR * ID, data = gir2)
#apply linear model
lmresultsGIR <- setNames(predict(fit, newdata = data.frame(ID = unique(gir2$ID), GIR = .6111111)),
unique(gir2$ID))
#show model
head(lmresultsGIR, n=10)
My question is, assuming I have round 4 GIR data:我的问题是,假设我有第 4 轮 GIR 数据:
ID round GIR
Tiger Woods 4 0.666666667
Tiger Woods 4 0.611111111
how do I update my script to pick round 4 GIR data by ID, instead of just hardcoding a magic value of .6111111
like I have now?我如何更新我的脚本以按 ID 选择第 4 轮 GIR 数据,而不是像我现在那样硬编码.6111111
的魔术值?
Try giving this a shot.试一试。
#load in data
gir2 <- read.csv("girforscore.csv")
#establish linear model
model <- na.omit(gir2)
fit <- lm(score ~ ID + GIR, data = model)
#subset data for round 4
round4 <- subset(gir2, round == 4)
#apply linear model
predict <- predict(fit, newdata = round4, se.fit = TRUE)
#easier than setNames for this particular example
round4$score <- predict$fit
#view round 4 predicted scores
round4
ID round GIR score
7 Tiger Woods 4 0.6666667 71.29545
8 Tiger Woods 4 0.6111111 71.40909
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.