简体   繁体   English

R glm函数更改我的列名

[英]R glm function changing my column names

I have what I think is a relatively simple question, but I can't seem to find the answer. 我有一个相对简单的问题,但似乎找不到答案。

I have a 200 X 8 matrix temp and a response matrix (200X1) Binomial Vector When I run the following line: 运行以下行时,我有一个200 X 8的矩阵temp和一个响应矩阵(200X1) Binomial Vector

CLog=glm(BinomialVector~temp,family= binomial(logit)) 

I am able to run the logistic regression. 我能够运行逻辑回归。 What I think this is doing is really BinomialVector~tempcol1 +tempcol2+tempcol3 and so on. 我认为这样做实际上是BinomialVector~tempcol1 +tempcol2+tempcol3 ,依此类推。

However, when I press summary(CLog) the names of my factors have changed. 但是,当我按summary(CLog) ,我的因素名称已更改。 If the first column was called trees then it has change do temptrees .Is there a way to prevent this? 如果第一列称为trees则它会更改temptrees 。是否有防止这种情况的方法?

As requested: 按照要求:

  BinomialVector
   [,1]
  [1,]    0
  [2,]    1
  [3,]    1
  [4,]    0
  [5,]    0
  [6,]    0
  [7,]    1



temp

  Net.Income.Y06. Return.on.Assets.Y06.
A         0.1929241                27.947    
AA        1.1405694                12.427
AAP       1.0302481                17.117
ABT       2.1006512                13.826

Return.on.Investment.Y06. Total.Current.Assets.Y06.
A                      39.844                 0.9274886  
AA                     20.003                 0.8830403
AAP                    30.927                 1.0439536
ABT                    21.376                 1.2447154


  Total.Current.Liabilities.Y06. IntersectionMostAdmired.2006.
A                        1.0812744                         0.000
AA                       0.9842055                         7.255
AAP                      1.1010472                         0.000
ABT                      0.7617044                         6.715

This is what possible columns of my temp matrix look like. 这就是我的临时矩阵的可能列。 The reason I don't like using that additive notation is that the number of columns changes, as I am using this inside a user defined function where I feed it in the temp matrix. 我不喜欢使用这种加法表示法的原因是,列数发生了变化,因为我在用户定义的函数中使用此函数,并将其输入到临时矩阵中。 As for using the data frame, I was under the impression that data frame is indeed the correct thing to use but I seem to get an error when it is not as.matrix. 至于使用数据框,我给人的印象是数据框确实是正确的东西,但是当它不是as.matrix时,我似乎会出错。 :s :s

Can you post a representative subset of your data and also the actual output glm gives you for that subset? 您是否可以发布数据的代表性子集,并且实际输出glm可以为您提供该子集?

Then it will be easier to diagnose/replicate. 然后,将更易于诊断/复制。

In the meantime, I suggest you use a data frame instead of a matrix. 同时,我建议您使用数据框而不是矩阵。 Here is how: 方法如下:

mydf<-data.frame(y=BinomialVector,temp);
CLog = glm(BinomialVector~tempcol1+tempcol2+tempcol3,data=mydf,family=binomial(logit));

Matrices are a bad format to use as data sources for regression models (for one thing, they coerce all columns to the same data type, which may or may not be part of the problem here), so I never use them. 矩阵是一种不好的格式,不能用作回归模型的数据源(一方面,它们将所有列强制为相同的数据类型,这在这里可能是问题的一部分,也可能不是问题的一部分),所以我从不使用它们。 But if I had to guess, your model might be converting the matrix into one long vector? 但是,如果我不得不猜测,您的模型可能是将矩阵转换为一个长向量? And perhaps there's a variable somewhere in there that has the value "tree"? 也许那里某个变量的值是“ tree”? But without example data and output, it's all guesswork. 但是没有示例数据和输出,这全都是猜测。 It's likely that when you run the above commands, the nature of the problem will reveal itself right there. 当您运行上述命令时,问题的性质很可能会立即显示出来。

Using a data frame is the way to go. 使用数据帧是必经之路。 For one, it'll make getting predictions on new data much easier; 首先,它将使对新数据的预测变得容易得多。 and it'll also let you use nominal predictors (factors) without having to code up the dummy variables yourself. 而且还可以让您使用名义预测变量(因子),而不必自己编写虚拟变量。 If the number of predictors is not fixed and you want to fit a model on all of them, use . 如果预测变量的数量不是固定的,并且您要在所有预测变量上拟合模型,请使用. in the formula. 在公式。

df <- data.frame(y=BinomialVector, temp)
glm(y ~ ., family=binomial, data=df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM