1

使用R...

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

其中一个变量colr的值范围为 1 到 5。

定义为integer具有值 12345的 。

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

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)

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

感谢您的任何帮助。

4

3 回答 3

3

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

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

/编辑澄清。编造一些数据:

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

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

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

于 2020-02-16T19:50:06.867 回答
2

以 R 为基数。

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

df$colr.f2 <- factor(colr.f, labels = labels, levels = seq_along(labels))
于 2020-02-16T19:52:46.990 回答
2

如果你有这个:

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 包(v0.3.0)于 2020-02-16 创建

于 2020-02-16T19:56:07.267 回答