使用MAE作为线性模型的误差函数

时间:2017-10-22 05:19:20

标签: r linear-regression

我想执行线性回归,但是我不想使用RMSE作为我的错误函数,而是想使用MAE(平均绝对误差)。

是否有允许我这样做的套餐?

2 个答案:

答案 0 :(得分:1)

您可以使用插入指标包。

static jobject getGlobalContext(JNIEnv *env)
{

    jclass activityThread = (*env)->FindClass(env,"android/app/ActivityThread");
    jmethodID currentActivityThread = (*env)->GetStaticMethodID(env,activityThread, "currentActivityThread", "()Landroid/app/ActivityThread;");
    jobject at = (*env)->CallStaticObjectMethod(env,activityThread, currentActivityThread);

    jmethodID getApplication = (*env)->GetMethodID(env,activityThread, "getApplication", "()Landroid/app/Application;");
    jobject context = (*env)->CallObjectMethod(env,at, getApplication);
    return context;
}

答案 1 :(得分:0)

聚会可能要迟到了,但这是使用CVXR软件包进行优化的解决方案。

library(CVXR)

# defining variables to be tuned during optimisation
coefficient <- Variable(1)
intercept <- Variable(1)

# defining the objective i.e. minimizing the sum af absolute differences (MAE)
objective <- Minimize(sum(abs(mtcars$disp - (mtcars$hp * coefficient) - intercept)))

# optimisation
problem <- Problem(objective)
result <- solve(problem)

# result
result$status
mae_coefficient <- result$getValue(coefficient)
mae_intercept <- result$getValue(intercept)

lm_coeff_intrc <- lm(formula = disp ~ hp, data = mtcars)$coefficients

library(tidyverse)

ggplot(mtcars, aes(hp, disp)) +
  geom_point() +
  geom_abline(
    slope = lm_coeff_intrc["hp"],
    intercept = lm_coeff_intrc["(Intercept)"],
    color = "red"
  ) +
  geom_abline(
    slope = mae_coefficient,
    intercept = mae_intercept,
    color = "blue"
  )

df <- mtcars %>%
  select(disp, hp) %>%
  rownames_to_column() %>%
  mutate(
    mae = disp - hp * mae_coefficient - mae_intercept,
    lm = disp - hp * lm_coeff_intrc["hp"] - lm_coeff_intrc["(Intercept)"]
  )

df %>%
  select(mae, lm) %>%
  pivot_longer(cols = 1:2) %>%
  group_by(name) %>%
  summarise(
    mae = sum(abs(value))
  )