所以,我有一个棒球运动员的数据集,我试图计算他们的体重的高度的预期值。我知道如何在一个变量中计算预期值,但我不确定如何使用另一个变量?
我在下面添加了我的数据集。使用dput命令,我想目前我已经使用各种mutate命令,但我不知道如果weightpounds = weightpounds,如何编写类似expectedheight = mean(heightunches)的东西。因为期望值需要考虑重量。
structure(
list(Name = structure(1:3, .Label = c("Adam_Donachie", "Paul_Bako", "Ramon_Hernandez"), class = "factor"),
Team = structure(c(1L, 1L, 1L), .Label ="BAL", class = "factor"),
Position = structure(c(1L, 1L, 1L), .Label = "Catcher",class = "factor"),
Heightinches = c(74L, 74L, 72L), Weightpounds =c(180L, 215L, 210L),
Age = c(22.98999977, 34.68999863, 30.78000069)), row.names = c(NA, -3L),
.Names = c("Name", "Team", "Position", "Heightinches", "Weightpounds", "Age"),
class = "data.frame")
答案 0 :(得分:2)
例如,您可以使用线性回归
model <- lm( Weightpounds ~ Heightinches,dat)
model
Call:
lm(formula = Weightpounds ~ Heightinches, data = dat)
Coefficients:
(Intercept) Heightinches
660.00 -6.25
意思是
Weightpounds = 660 - 6.25*Heightinches
您也可以将模型应用于数据
> model <- lm( Weightpounds ~ Heightinches,dat)
> predict(model,dat)
1 2 3
197.5 197.5 210.0
这使用非常简单的线性模型。
您可以通过扩展公式 Weightpounds ~ Heightinches
来扩展模型,以进行语法检查?formula