The below quadratic model example (including data) is from Steve's Data Tips and Tricks.
# Sample Data and Quadratic Modelstudy_hours <- c(6, 9, 12, 14, 30, 35, 40, 47, 51, 55, 60)exam_scores <- c(14, 28, 50, 70, 89, 94, 90, 75, 59, 44, 27)data <- data.frame(study_hours, exam_scores)model <- lm(exam_scores ~ study_hours + I(study_hours^2), data = data)
I can calculate the value of study_hours
where the exam_score
is maximized (inflection point or critical value) using a solution in Extract critical points of a polynomial model object in R?.
model %>% mosaic::makeFun() %>% optimize(interval = c(min(df$study_hours), max(df$study_hours)), maximum = TRUE)
Another dataset and model (with error)
Now, I have another dataset and model below. I would like to get the value of variable ceoten
where the log(salary)
is maximized.However, I get the error below.
I am not sure if I correctly understood how the optimize
function works.
(1) Can anyone share some insights?
(2) Would there be any other way to achieve the task?
library(wooldridge)data("ceosal2")ceosalary <- ceosal2model2 <- lm(log(salary) ~ log(sales) + comten + ceoten + I(ceoten^2), data=ceosalary) library(mosaic)model2 %>% # This function is used to extract the formula of the model as a functionmosaic::makeFun() %>% optimize(interval = c(min(ceosalary$ceoten), max(ceosalary$ceoten)), maximum = TRUE)
Error in f(arg, ...) : argument "comten" is missing, with no default