【问题标题】:Nonlinear regression in R with multiple categorical dependent variablesR中具有多个分类因变量的非线性回归
【发布时间】:2018-07-13 17:59:48
【问题描述】:

我必须使用如下所示的数据执行非线性多元回归:

ID    Customer   Country   Industry      Machine-type    Service hours**
1     A          China     mass          A1              120
2     B          Europe    customized    A2              400
3     C          US        mass          A1               60
4     D          Rus       mass          A3              250
5     A          China     mass          A2              480
6     B          Europe    customized    A1              300
7     C          US        mass          A4              250
8     D          Rus       customized    A2              260
9     A          China     Customized    A2              310
10    B          Europe    mass          A1              110
11    C          US        Customized    A4               40
12    D          Rus       customized    A2              80

因变量:服务时间 自变量:客户、国家、行业、机器类型

我进行了线性回归,但由于线性假设不成立,我必须进行非线性回归。

我知道非线性回归可以用 nls 函数来完成。如何将分类变量添加到非线性回归,以便在 R 中获得统计摘要

添加假人后的列名:table with dummies

ID  Customer.a  Customer.b  Customer.c  Customer.d  Country.China   Country.Europe  Country.Rus Country.US  Industry.customized industry.Customized Industry.mass   Machine type.A1 Machine type.A2 Machine type.A3 Service hours
1 1 0 0 0 1 0 0 0 0 0 1 1 0 0 120 
2 0 1 0 0 0 1 0 0 1 0 0 0 1 0 400 
3 0 0 1 0 0 0 0 1 0 0 1 0 0 1 60 
4 0 0 0 1 0 0 1 0 0 0 1 1 0 0 250 
5 1 0 0 0 1 0 0 0 1 0 0 0 0 1 480 
6 0 1 0 0 0 1 0 0 0 1 0 1 0 0 300 
7 0 0 1 0 0 0 0 1 0 0 1 0 0 1 250 
8 0 0 0 1 0 0 1 0 1 0 0 0 1 0 260 
9 1 0 0 0 1 0 0 0 0 0 1 0 1 0 210 
10 0 1 0 0 0 1 0 0 1 0 0 0 1 0 110 
11 0 0 1 0 0 0 0 1 0 0 1 0 0 1 40 
12 0 0 0 1 0 0 1 0 0 0 1 1 0 0 80

【问题讨论】:

  • 不确定您使用的函数是否可以采用因子变量,或者您可能需要创建虚拟变量。看看dummies
  • 您好,感谢您这么快的回答!是的,我使用了 dummies 包。所以现在我有多个假人,但是如何将这些假人放入统计汇总结果的非线性函数中?
  • > datadum names(datadum) [1] "ID" "Customer.a" "Customer.b" [4] "Customer.c" "Customer.d" "Country.China" [7] "Country.Europe" "Country.Rus" "Country.US" [10] "Industry.customized" "Industry.Customized" "Industry.mass " [13] "机器类型.A1" "机器类型.A2" "机器类型.A3" [16] "服务时间"
  • 这些是我得到的假人。我想将它们添加到非线性回归中。提前感谢您对我的帮助!
  • 能否更新问题中的数据框

标签: r categorical-data non-linear-regression


【解决方案1】:

处理分类预测变量的方式取决于预测变量可以容纳的级别数。

对于只能采用 2 种形式(男性或女性)的性别等预测变量,您可以简单地将它们表示为二元 (1,0) 变量。

对于超过 2 个级别的预测变量,我们使用 1-of-k 虚拟编码,其中 k 是特定变量所采用的级别数。有用的功能见dummies包!

在此之后,您可以使用公式拟合模型:

nls(Service.hours ~ predictor1 + predictor2 + predictorN, data = df)

【讨论】:

  • 如果我使用这个 nls 函数我得到以下错误: > test2nls
猜你喜欢
  • 2020-05-20
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 2023-03-23
  • 2020-11-25
  • 1970-01-01
相关资源
最近更新 更多