-1

[![enter image description here][1]][1] I am trying to change dummy variables in r to my desire dummy variables but the r code I am using did not give the desire result. I have provided the r code and the result I want in the attach image above. Thank you helping.

lowbwt=read.csv("lowbwt_1.csv",header = T)

library(dplyr) lowbwt<-lowbwt %>% + mutate(Black=ifelse(RACE_1=="Black",1,0)) %>% + mutate(Other=ifelse(RACE_1=="Other",0,1)) %>% + mutate(White=ifelse(RACE_1=="White",0,0)) model_1=glm(LOW~RACE_1+AGE+LWT,data=lowbwt,family = "binomial") summary(model_1)

Call: glm(formula = LOW ~ RACE_1 + AGE + LWT, family = "binomial", data = lowbwt)

Deviance Residuals: Min 1Q Median 3Q Max
-1.4052 -0.8946 -0.7209 1.2484 2.0982

Coefficients: Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.310563 1.146923 2.015 0.0439 * RACE_1Other -0.560361 0.512830 -1.093 0.2745
RACE_1White -1.003822 0.498014 -2.016 0.0438 * AGE -0.025524 0.033252 -0.768 0.4427

LWT -0.014353 0.006523 -2.200 0.0278 *

Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 234.67  on 188  degrees of freedom

Residual deviance: 222.66 on 184 degrees of freedom AIC: 232.66

Number of Fisher Scoring iterations: 4

contrasts(lowbwt$RACE_1) Other White Black 0 0 Other 1 0 White 0 1

But I want produce is the following: Other White Black 1 0 Other 0 1 White 0 0 I White to be base with (0 0) rather than Black as r is giving

  • 6
    Please copy-paste your code and desired output as text, not an image. You are creating new variables, which will not affect the contrasts that are set for the original variable, you may want to look at `?relevel` – Marius Jun 29 '18 at 01:20
  • The title of this post was wrongly type the right title is: "Changing dummy variables in r to my desire dummy variables". – Mfon Ekpenyong Jun 29 '18 at 01:34
  • @MfonEkpenyong you can edit the post to change the title (and please add the code directly per Marius' suggestion – Scransom Jun 29 '18 at 01:46
  • 1
    Welcome to StackOverflow! For code debugging please always ask with [reproducible](https://stackoverflow.com/q/5963269/1422451) code/data per the [MCVE](https://stackoverflow.com/help/mcve) and [`r`](https://stackoverflow.com/tags/r/info) tag description, with the desired output. Please only use screenshots to display something inherently visual and nontabular like a plot or a GUI menu. – Hack-R Jun 29 '18 at 02:07

1 Answers1

0

Since you have not given us what to work with, I will use the inbuilt example for reproducibility:

utils::example(factor)
fff <- ff[, drop = TRUE]  # reduce to 5 levels.

Now we will use fff as our factor, all that we need to change is the base:

 contrasts(fff) # treatment contrasts by default
  c i s t
a 0 0 0 0
c 1 0 0 0
i 0 1 0 0
s 0 0 1 0
t 0 0 0 1
contrasts(C(fff, base=5))# Change the base to 5
  1 2 3 4
a 1 0 0 0
c 0 1 0 0
i 0 0 1 0
s 0 0 0 1
t 0 0 0 0
Onyambu
  • 67,392
  • 3
  • 24
  • 53