R-Studio中OLS多元回归模型系数的绘图比较

时间:2018-03-21 16:10:51

标签: r

任何人都可以帮我处理我的代码吗?我试图通过模型显示标记颜色的系数的比较。以下是我的代码。学校项目今晚到期。我希望在不同颜色的相同图上比较模型系数。我已搜索过,但无法找到确切的答案,以使我的代码正常工作。我有两个模型,我已合并。合并后,我使用GGPLOT。它绘制系数,但只绘制一个,不按模型比较或颜色代码。

    murderdeathModel <- 
    lm(murder~factor(death_pen)+gini+black_AA+white+poverty,DeathPen1)
    summary(murderdeathModel)

    MURDERDEATHOUTLIERSREMOVEDMODEL <- 
    lm(murder~factor(death_pen)+gini+black_AA+white+poverty,DeathPen1)
    summary(MURDERDEATHOUTLIERSREMOVEDMODEL)

    #TIBBLE of Coef
    co1<- summary(murderdeathModel)$coefficients
    co1

    murderdeathModel <- tibble(est =co1 [,1], # first column of coeftest object
                            se = co1[,2], # second col.
                            vars = row.names(co1),
                            modelname = "mdm") # variable names are the row 
    names of the matrix
    murderdeathModel
    MURDERDEATHOUTLIERSREMOVEDMODEL <- tibble(est =co1 [,1], # first column 
    of coeftest object
                                          se = co1[,2], # second col.
                                          vars = row.names(co1),
                                          modelname = "mdmo") # variable 
    names are the row names of the matrix
    MURDERDEATHOUTLIERSREMOVEDMODEL
    #calculate CIs
    murderdeathModel <- murderdeathModel %>%
    mutate(lb=est-se*1.96,
          ub=est+se*1.96) %>%
    slice(-1) # get rid of intercept since we generally do not plot

   MURDERDEATHOUTLIERSREMOVEDMODEL <- MURDERDEATHOUTLIERSREMOVEDMODEL %>%
   mutate(lb=est-se*1.96,
          ub=est+se*1.96) %>%
   slice(-1) # get rid of intercept since we generally do not plot
   MURDERDEATHOUTLIERSREMOVEDMODEL

   #binding
   comparison1 <- 
   bind_rows(murderdeathModel,MURDERDEATHOUTLIERSREMOVEDMODEL)
   comparison1



   #plot
   comparison1plot  <- comparison1 %>%
   ggplot (aes(x=est, xmin=lb, xmax=ub, y=reorder(vars, -est), label= 
   modelname))
   comparison1plot
   comparison1plot <- comparison1plot + geom_errorbarh(height=0.2) + 
   geom_point()
   comparison1plot
   comparison1plot <- comparison1plot + geom_vline(aes(xintercept=0), color= 
   "red" , linetype=2)  + labs(colour="modelname") 
   comparison1plot
    comparison1plot <- comparison1plot + labs(y="", x="Estimates with 95% 
    confidence intervals",
                                          title= "Comparison of 
    Coefficients of Models",
                                          "Dependent variable: Rate of 
    Murder")

    comparison1plot

以下是我的结果:我没有得到任何错误,但我只为每个变量得到一行系数。我需要一个用于另一种颜色的每个模型。感谢您的反馈到目前为止。

> #3.4 problem set 2 extra credit
> 
> 
> murderdeathModel <- lm(murder~factor(death_pen)+gini+black_AA+white+poverty,DeathPen1)
> summary(murderdeathModel)

Call:
lm(formula = murder ~ factor(death_pen) + gini + black_AA + white + 
    poverty, data = DeathPen1)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.3764 -1.2685 -0.2335  1.1873  4.0976 

Coefficients:
                               Estimate Std. Error t value Pr(>|t|)    
(Intercept)                    15.30933    8.22860   1.861   0.0695 .  
factor(death_pen)death_penalty -0.14468    0.59787  -0.242   0.8099    
gini                           -0.34977    0.18169  -1.925   0.0607 .  
black_AA                        0.21333    0.03356   6.356 1.01e-07 ***
white                          -0.04582    0.01882  -2.435   0.0190 *  
poverty                         0.54859    0.11498   4.771 2.04e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.836 on 44 degrees of freedom
Multiple R-squared:  0.7335,    Adjusted R-squared:  0.7032 
F-statistic: 24.22 on 5 and 44 DF,  p-value: 1.262e-11

> 
> MURDERDEATHOUTLIERSREMOVEDMODEL <- lm(murder~factor(death_pen)+gini+black_AA+white+poverty,DeathPen1)
> summary(MURDERDEATHOUTLIERSREMOVEDMODEL)

Call:
lm(formula = murder ~ factor(death_pen) + gini + black_AA + white + 
    poverty, data = DeathPen1)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.3764 -1.2685 -0.2335  1.1873  4.0976 

Coefficients:
                               Estimate Std. Error t value Pr(>|t|)    
(Intercept)                    15.30933    8.22860   1.861   0.0695 .  
factor(death_pen)death_penalty -0.14468    0.59787  -0.242   0.8099    
gini                           -0.34977    0.18169  -1.925   0.0607 .  
black_AA                        0.21333    0.03356   6.356 1.01e-07 ***
white                          -0.04582    0.01882  -2.435   0.0190 *  
poverty                         0.54859    0.11498   4.771 2.04e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.836 on 44 degrees of freedom
Multiple R-squared:  0.7335,    Adjusted R-squared:  0.7032 
F-statistic: 24.22 on 5 and 44 DF,  p-value: 1.262e-11

> 
> #TIBBLE of Coef
> co1<- summary(murderdeathModel)$coefficients
> co1
                                  Estimate Std. Error   t value     Pr(>|t|)
(Intercept)                    15.30932648 8.22860074  1.860502 6.950603e-02
factor(death_pen)death_penalty -0.14468497 0.59786688 -0.242002 8.099030e-01
gini                           -0.34976695 0.18169153 -1.925059 6.070169e-02
black_AA                        0.21333409 0.03356404  6.356032 1.010401e-07
white                          -0.04581836 0.01881759 -2.434869 1.902077e-02
poverty                         0.54859221 0.11497922  4.771229 2.044677e-05
> 
> murderdeathModel <- tibble(est =co1 [,1], # first column of coeftest object
+                             se = co1[,2], # second col.
+                             vars = row.names(co1),
+                             modelname = "mdm") # variable names are the row names of the matrix
> murderdeathModel
# A tibble: 6 x 4
       est     se vars                           modelname
     <dbl>  <dbl> <chr>                          <chr>    
1  15.3    8.23   (Intercept)                    mdm      
2 - 0.145  0.598  factor(death_pen)death_penalty mdm      
3 - 0.350  0.182  gini                           mdm      
4   0.213  0.0336 black_AA                       mdm      
5 - 0.0458 0.0188 white                          mdm      
6   0.549  0.115  poverty                        mdm      
> MURDERDEATHOUTLIERSREMOVEDMODEL <- tibble(est =co1 [,1], # first column of coeftest object
+                                           se = co1[,2], # second col.
+                                           vars = row.names(co1),
+                                           modelname = "mdmo") # variable names are the row names of the matrix
> MURDERDEATHOUTLIERSREMOVEDMODEL
# A tibble: 6 x 4
       est     se vars                           modelname
     <dbl>  <dbl> <chr>                          <chr>    
1  15.3    8.23   (Intercept)                    mdmo     
2 - 0.145  0.598  factor(death_pen)death_penalty mdmo     
3 - 0.350  0.182  gini                           mdmo     
4   0.213  0.0336 black_AA                       mdmo     
5 - 0.0458 0.0188 white                          mdmo     
6   0.549  0.115  poverty                        mdmo     
> #calculate CIs
> murderdeathModel <- murderdeathModel %>%
+   mutate(lb=est-se*1.96,
+          ub=est+se*1.96) %>%
+   slice(-1) # get rid of intercept since we generally do not plot
> murderdeathModel
# A tibble: 5 x 6
      est     se vars                           modelname      lb       ub
    <dbl>  <dbl> <chr>                          <chr>       <dbl>    <dbl>
1 -0.145  0.598  factor(death_pen)death_penalty mdm       -1.32    1.03   
2 -0.350  0.182  gini                           mdm       -0.706   0.00635
3  0.213  0.0336 black_AA                       mdm        0.148   0.279  
4 -0.0458 0.0188 white                          mdm       -0.0827 -0.00894
5  0.549  0.115  poverty                        mdm        0.323   0.774  
> MURDERDEATHOUTLIERSREMOVEDMODEL <- MURDERDEATHOUTLIERSREMOVEDMODEL %>%
+   mutate(lb=est-se*1.96,
+          ub=est+se*1.96) %>%
+   slice(-1) # get rid of intercept since we generally do not plot
> MURDERDEATHOUTLIERSREMOVEDMODEL
# A tibble: 5 x 6
      est     se vars                           modelname      lb       ub
    <dbl>  <dbl> <chr>                          <chr>       <dbl>    <dbl>
1 -0.145  0.598  factor(death_pen)death_penalty mdmo      -1.32    1.03   
2 -0.350  0.182  gini                           mdmo      -0.706   0.00635
3  0.213  0.0336 black_AA                       mdmo       0.148   0.279  
4 -0.0458 0.0188 white                          mdmo      -0.0827 -0.00894
5  0.549  0.115  poverty                        mdmo       0.323   0.774  
> 
> ##binding
> comparison1 <- 
+   bind_rows(murderdeathModel,MURDERDEATHOUTLIERSREMOVEDMODEL)
> comparison1
# A tibble: 10 x 6
       est     se vars                           modelname      lb       ub
     <dbl>  <dbl> <chr>                          <chr>       <dbl>    <dbl>
 1 -0.145  0.598  factor(death_pen)death_penalty mdm       -1.32    1.03   
 2 -0.350  0.182  gini                           mdm       -0.706   0.00635
 3  0.213  0.0336 black_AA                       mdm        0.148   0.279  
 4 -0.0458 0.0188 white                          mdm       -0.0827 -0.00894
 5  0.549  0.115  poverty                        mdm        0.323   0.774  
 6 -0.145  0.598  factor(death_pen)death_penalty mdmo      -1.32    1.03   
 7 -0.350  0.182  gini                           mdmo      -0.706   0.00635
 8  0.213  0.0336 black_AA                       mdmo       0.148   0.279  
 9 -0.0458 0.0188 white                          mdmo      -0.0827 -0.00894
10  0.549  0.115  poverty                        mdmo       0.323   0.774  
> #CI'S
> comparison1 <- comparison1 %>%
+   mutate(lb=est-se*1.96,
+          ub=est+se*1.96) %>%
+   slice(-1) # get rid of intercept since we generally do not plot
> comparison1
# A tibble: 9 x 6
      est     se vars                           modelname      lb       ub
    <dbl>  <dbl> <chr>                          <chr>       <dbl>    <dbl>
1 -0.350  0.182  gini                           mdm       -0.706   0.00635
2  0.213  0.0336 black_AA                       mdm        0.148   0.279  
3 -0.0458 0.0188 white                          mdm       -0.0827 -0.00894
4  0.549  0.115  poverty                        mdm        0.323   0.774  
5 -0.145  0.598  factor(death_pen)death_penalty mdmo      -1.32    1.03   
6 -0.350  0.182  gini                           mdmo      -0.706   0.00635
7  0.213  0.0336 black_AA                       mdmo       0.148   0.279  
8 -0.0458 0.0188 white                          mdmo      -0.0827 -0.00894
9  0.549  0.115  poverty                        mdmo       0.323   0.774  
> #TIBBLE OF COEFS
> 
> murderdeathModel <- tibble(est =co1 [,1], # first column of coeftest object
+                            se = co1[,2], # second col.
+                            vars = row.names(co1),
+                            modelname = "mdm") # variable names are the row names of the matrix
> murderdeathModel
# A tibble: 6 x 4
       est     se vars                           modelname
     <dbl>  <dbl> <chr>                          <chr>    
1  15.3    8.23   (Intercept)                    mdm      
2 - 0.145  0.598  factor(death_pen)death_penalty mdm      
3 - 0.350  0.182  gini                           mdm      
4   0.213  0.0336 black_AA                       mdm      
5 - 0.0458 0.0188 white                          mdm      
6   0.549  0.115  poverty                        mdm      
> #plot
> 
> #plot
> comparison1plot  <- comparison1 %>%
+   ggplot (aes(x=est, xmin=lb, xmax=ub, y=reorder(vars, -est), label= modelname))
> comparison1plot
> comparison1plot <- comparison1plot + geom_errorbarh(height=0.2) + geom_point()
> comparison1plot
> comparison1plot <- comparison1plot + geom_vline(aes(xintercept=0), color= "red" , linetype=2)  + labs(colour="modelname") 
> comparison1plot
> comparison1plot <- comparison1plot + labs(y="", x="Estimates with 95% confidence intervals",
+                                           title= "Comparison of Coefficients of Models",
+                                           "Dependent variable: Rate of Murder")
> 
> comparison1plot

下面是我的图表 the graph from the code above

我已将modelName重新编码为一个因子 - 以下是我的代码和结果

comparison1plot <- comparison1plot %>%
  mutate(modelName = factor(modelName, levels =0:1, labels  =c("MDM","MDMO")))
comparison1plot

结果:

> #plot
> comparison1plot <- comparison1plot %>%
+   mutate(modelName = factor(modelName, levels =0:1, labels  =c("MDM","MDMO")))
Error in UseMethod("mutate_") : 
  no applicable method for 'mutate_' applied to an object of class "c('gg', 'ggplot')"
> comparison1plot

我没有更改任何其他代码。我在#plot和#plot之间添加了一些代码。它仍然不会改变图形。

1 个答案:

答案 0 :(得分:0)

以下是如何使用不同颜色绘制不同数据点的简单示例:

df = data.frame(x = 1:200, y = c(1:100, -101:-200),group = as.factor(rep(c(1,2),each=100))) ggplot(df,aes(x,y,color=group))+geom_line()+theme_classic()

enter image description here

您需要为每个数据点(我的示例中的组列)添加一个标签,作为不同的因子级别。然后使用aes()中的factor标签指定颜色。