【问题标题】:Renaming integers within a data.frame重命名 data.frame 中的整数
【发布时间】:2020-05-31 19:09:13
【问题描述】:

使用R...

我有一个带有五个变量的data.frame

其中一个变量colr 的值介于1 到5 之间。

定义为integer,其值为12345

问题: 我想建立一个回归模型,其中 colr 内的值、整数 12345报告为具有以下名称的自变量。

1 = 银, 2 = 蓝色, 3 = 粉红色, 4 = 银色、蓝色或粉色除外, 5 = 未报告颜色。

问题:有没有办法以不同于以下的方式提取或重命名这些值(因为此过程不会重命名,例如 1Silver 在总结回归输出):

lm(dependent variable ~ + I(colr.f == 1) + 
                          I(colr.f == 2) + 
                          I(colr.f == 3) + 
                          I(colr.f == 4) + 
                          I(colr.f == 5),
                          data = df)

我对任何允许我独立创建和命名这些不同值的方法持开放态度,但我更愿意看看是否有办法使用tidyversedplyr 这样做,因为这是我必须要做的事情在构建多变量模型时经常这样做。

感谢您的帮助。

【问题讨论】:

  • 您应该使用 Ben T 建议的因子。在您发布的方法中,您没有基础级别,这将导致完美的共线性
  • 我试图运行提供的代码,但它告诉我这个...Error in factor(colr.f, levels = c(1:5), labels = c("Silver", "Blue", : object 'colr.f' not found
  • 在这种情况下,您在数据框中的变量没有名称 colr.f 我想

标签: r dplyr statistics regression forcats


【解决方案1】:

我不确定我是否以正确的方式理解您的问题,但您不能直接使用

library(dplyr)
df <- df %>%
 mutate(color=factor(colr.f, levels=c(1:5), labels=c("silver", "blue", "pink", "not s, b, p", "not reported"))

然后只在color 上运行回归。

/edit 进行澄清。编造一些数据:

df <- data.frame(
  x=rnorm(100),
  color=factor(rep(c(1,2,3,4,5), each=20), 
               labels=c("Silver", "Blue", "Pink", "Not S, B, P", "Not reported")),
  y=rnorm(100, 4))

m1 <- lm(y~x+color, data=df)
m2 <- lm(y~x+color-1, data=df)
summary(m1)
Call:
lm(formula = y ~ x + color, data = df)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.96394 -0.59647  0.00237  0.56916  2.13392 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
(Intercept)        3.93238    0.19312  20.362   <2e-16 ***
x                  0.13588    0.09856   1.379    0.171    
colorBlue         -0.07862    0.27705  -0.284    0.777    
colorPink         -0.02167    0.27393  -0.079    0.937    
colorNot S, B, P   0.15238    0.27221   0.560    0.577    
colorNot reported  0.14139    0.27230   0.519    0.605    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.8606 on 94 degrees of freedom
Multiple R-squared:  0.0268,    Adjusted R-squared:  -0.02496 
F-statistic: 0.5177 on 5 and 94 DF,  p-value: 0.7623

summary(m2)

Call:
lm(formula = y ~ x + color - 1, data = df)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.96394 -0.59647  0.00237  0.56916  2.13392 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
x                  0.13588    0.09856   1.379    0.171    
colorSilver        3.93238    0.19312  20.362   <2e-16 ***
colorBlue          3.85376    0.19570  19.692   <2e-16 ***
colorPink          3.91071    0.19301  20.262   <2e-16 ***
colorNot S, B, P   4.08477    0.19375  21.083   <2e-16 ***
colorNot reported  4.07377    0.19256  21.156   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.8606 on 94 degrees of freedom
Multiple R-squared:  0.9578,    Adjusted R-squared:  0.9551 
F-statistic: 355.5 on 6 and 94 DF,  p-value: < 2.2e-16

第一个模型是具有截距的模型,因此必须删除一个因子水平以避免完全多重共线性。在这种情况下,银色的“效果”是截距值,而其他颜色的“效果”是截距系数值+各自的系数值。

第二个模型是在没有截距(没有常数)的情况下估计的,因此您可以看到各个效果。但是,在估计没有截距的模型之前,您可能应该知道自己在做什么。

【讨论】:

  • 谢谢本。也许这会简化它。如果没有mutating,回归模型将是lm(price ~ colr, data = df) but when i summarize the regression output for these results I would not see each level, eg 1`,我想将其重命名为silver,独立拆分以查看silverprice 的颜色单独影响。这会改变你的答案吗?再次感谢
【解决方案2】:

使用基础 R。

labels <- c("Silver", "Blue", "Pink", "Other Color", "Color Not Reported")

df$colr.f2 <- factor(colr.f, labels = labels, levels = seq_along(labels))

【讨论】:

    【解决方案3】:

    如果你有这个:

    df <- data.frame(int = sample(5, 20, TRUE), value = rnorm(20))
    df
    #>    int       value
    #> 1    3 -0.62042198
    #> 2    4  0.85009260
    #> 3    5 -1.04971518
    #> 4    1 -2.58255471
    #> 5    1  0.62357772
    #> 6    4  0.00286785
    #> 7    4 -0.05981318
    #> 8    4  0.72961261
    #> 9    4 -0.03156315
    #> 10   1 -2.05486209
    #> 11   5  1.77099554
    #> 12   1  1.02790956
    #> 13   1 -0.70354012
    #> 14   1  0.27353731
    #> 15   2 -0.04817215
    #> 16   2  0.17151374
    #> 17   5 -0.54824346
    #> 18   2  0.41123284
    #> 19   5  0.05466070
    #> 20   1 -0.41029986
    

    你可以这样做:

    library(tidyverse)
    
    df <- df %>% mutate(color = factor(c("red", "green", "orange", "blue", "pink"))[int])
    df
    #>    int       value  color
    #> 1    3 -0.62042198 orange
    #> 2    4  0.85009260   blue
    #> 3    5 -1.04971518   pink
    #> 4    1 -2.58255471    red
    #> 5    1  0.62357772    red
    #> 6    4  0.00286785   blue
    #> 7    4 -0.05981318   blue
    #> 8    4  0.72961261   blue
    #> 9    4 -0.03156315   blue
    #> 10   1 -2.05486209    red
    #> 11   5  1.77099554   pink
    #> 12   1  1.02790956    red
    #> 13   1 -0.70354012    red
    #> 14   1  0.27353731    red
    #> 15   2 -0.04817215  green
    #> 16   2  0.17151374  green
    #> 17   5 -0.54824346   pink
    #> 18   2  0.41123284  green
    #> 19   5  0.05466070   pink
    #> 20   1 -0.41029986    red
    

    允许这样的回归:

    lm(value ~ color, data = df) %>% summary()
    #> 
    #> Call:
    #> lm(formula = value ~ color, data = df)
    #> 
    #> Residuals:
    #>      Min       1Q   Median       3Q      Max 
    #> -2.03595 -0.33687 -0.00447  0.46149  1.71407 
    #> 
    #> Coefficients:
    #>             Estimate Std. Error t value Pr(>|t|)
    #> (Intercept)   0.2982     0.4681   0.637    0.534
    #> colorgreen   -0.1200     0.7644  -0.157    0.877
    #> colororange  -0.9187     1.1466  -0.801    0.436
    #> colorpink    -0.2413     0.7021  -0.344    0.736
    #> colorred     -0.8448     0.6129  -1.378    0.188
    #> 
    #> Residual standard error: 1.047 on 15 degrees of freedom
    #> Multiple R-squared:  0.1451, Adjusted R-squared:  -0.0829 
    #> F-statistic: 0.6364 on 4 and 15 DF,  p-value: 0.6444
    

    reprex package (v0.3.0) 于 2020-02-16 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-09
      • 2019-06-30
      • 2017-06-03
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      相关资源
      最近更新 更多