第一次在R中使用optimize() - 我这样做对吗?

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

我超出了我的领域专业知识,尽管我已经创建了一个“答案”(通过大量谷歌搜索和 ChatGPT),但我需要有人让我知道我是否正在做我想做的事情。

我正在学习有关“创建受毕达哥拉斯期望启发的期望点模型”的教程。他使用 Excel 中的求解器通过寻找最低 mse 来找到最佳指数系数 c。据我了解,

optimize()
也有类似的工作。
我提出了以下函数,但不明白这些值如何传递给 

optimize()

以及为什么该函数看起来会产生三个值

predicted_xPts_perc
se
mean(se)
。不是应该提供两个吗?
# Objective function
objective_function <- function(c) {
  predicted_xPts_perc <- data$xg_for_per_match^c / (data$xg_for_per_match^c + data$xg_against_per_match^c)
  se <- (predicted_xPts_perc - data$actual_points_perc)^2
  mean(se)
}

# Optimize the objective
result <- optimize(f = objective_function, interval = c(0, 5), lower = 1, upper = 3)
optimal_c <- result$minimum
print(optimal_c)

这是数据(示例)和完整的代码块:

data <- structure(list(xg_for_per_match = c(2.431, 2.256, 2.399, 1.86777777777778, 2.56, 1.776, 1.59222222222222, 1.665, 0.907, 1.691, 1.52555555555556, 0.56, 1.46555555555556, 0.754, 1.955, 1.285, 1.38428571428571, 1.282, 1.86625, 1.5075), xg_against_per_match = c(0.757, 0.682, 0.739, 1.40555555555556, 0.849, 1.409, 1.17777777777778, 1.822, 1.938, 2.489, 1.70666666666667, 3.136, 2.37555555555556, 2.985, 0.78375, 1.56, 1.43, 0.812, 0.945, 0.94875), actual_points_perc = c(0.866666666666667, 0.766666666666667, 0.733333333333333, 0.666666666666667, 0.6, 0.6, 0.592592592592593, 0.566666666666667, 0.4, 0.4, 0.37037037037037, 0.2, 0.111111111111111, 0, 0.791666666666667, 0.625, 0.619047619047619, 0.6, 0.541666666666667, 0.416666666666667), se = c(0.0020194397996264, 0.0223794165299103, 0.0323996328399904, 0.000796304748754376, 0.0905484326863811, 0.00018817673840017, 0.00288909832711458, 0.0124545498241967, 0.0485423096732081, 0.0070889390780061, 0.0054423086667482, 0.0285940157162698, 0.027082829359042, 0.00359736139900119, 0.00488179512531288, 0.048737642866935, 0.0183025693952632, 0.0129244422758347, 0.0646460987510766, 0.0897732500472823)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame")) # Objective function objective_function <- function(c) { predicted_xPts_perc <- data$xg_for_per_match^c / (data$xg_for_per_match^c + data$xg_against_per_match^c) se <- (predicted_xPts_perc - data$actual_points_perc)^2 mean(se) } # Optimize the objective result <- optimize(f = objective_function, interval = c(0, 5), lower = 1, upper = 3) optimal_c <- result$minimum print(optimal_c)

	
r optimization
1个回答
0
投票
nls()

函数。

model <- nls(actual_points_perc ~ xg_for_per_match^c / (xg_for_per_match^c + xg_against_per_match^c), data=data, start=list(c=1))
print(model)

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