Chaning dummy variable in r to my desire variables dummy variables,


Chaning dummy variable in r to my desire variables dummy variables,



[![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



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





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 at 1:20


?relevel





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 at 1:34





@MfonEkpenyong you can edit the post to change the title (and please add the code directly per Marius' suggestion
– Mob
Jun 29 at 1:46





Welcome to StackOverflow! For code debugging please always ask with reproducible code/data per the MCVE and r 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 at 2:07


r




1 Answer
1



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:


fff


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux