cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Converting R to JMP script

Hello,

 

I am not familiar with R and have been working with JMP (still beginner).  I have R code that I need to convert to JMP script.  I called JMP and they suggested I post here to see if someone can help me?

 

The R code is contained here abcd_discrimination/demographics_baseline_clean.R at main · barzilab1/abcd_discrimination · GitHub

 

and I have the datasheet in JMP, just no idea how to convert the script.

 

Is there anyone that can assist?

I attached the file in txt

Thank you in advance for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
mark_anawis
Level IV

Re: Converting R to JMP script

What is occurring in these steps is data cleanup. "demographics_set" is a data table which is similar to a JMP data table. 

R uses the convention: demographics_set[rows,columns]. Therefore, when you have the following line below, it is applied to all the rows since there is nothing inside the brackets to the left of the comma. For the column "sex_br", it assigns a value of 1 for "F" and 0 for "M" (and anything NOT "F"). The logic is that (sex == "F") returns 1 when TRUE and 0 when FALSE. The ":=" is a left assignment operator (infix). R may not recognize this operator at first so you may need to run the line: `:=` <- `<-` before the rest of the code. This assigns the more typical assignment operator ("<-") to ":=".

demographics_set[, sex_br := (sex == "F")*1]

 Here's another example where there is row subsetting below. For the rows where "demo_ethn_v2" is equal to "1", assign a value of 1 to the column "ethnicity_hisp".

 

demographics_set[demo_ethn_v2 == "1", ethnicity_hisp := 1]

 The is.na() function selects the rows which are not NA for the column inside the brackets.

The as.character() and as.numeric() function convert the column to either character or numeric as stated. Sometimes you need to first convert to character and then to numeric, hence the nested functions which occur in the code.

 

After all this data cleaning, the following line drops factor levels that are no longer used due to subsetting the data frame.

 

demographics_set = droplevels(demographics_set)

  That said, I agree with the others in that it would probably be easier for you to embed this R code inside a JMP script and use the R functions to run the R code. Check out the Scripting Index in JMP under Help menu to find the R functions. You'll probably need R Init() to initialize the connection to R, R Send() to wrap around any file or variable you want to input into the R code, R Submit() to wrap around your R code, R Get() to retrieve output from R back into JMP (or R Get Graphics() if you are retrieving a graphical object), and R Term() to terminate the R interface. Sounds more complicated than it is, but probably still easier than re-writing all the R code as JSL (JMP scripting language). The scripting index has some nice examples to help you along.

View solution in original post

7 REPLIES 7
pmroz
Super User

Re: Converting R to JMP script

Might be easier to use JMP as a front end and call the existing R program from JSL (JMP Script Language).

 

Re: Converting R to JMP script

Hi Everyone, I'm trying to make my inquiry a bit more specific and JMP-focused. From the following R-code generated from the accompanied data, can someone explain to me the logic for what R is doing in creating these new variables from the raw data? I intend to reproduce these new variables on the raw data in JMP using column formulas.  Specifically, this line 23 to line 114 of the attached R-code: abcd_discrimination/demographics_baseline_clean.R at main · barzilab1/abcd_discrimination · GitHub

########### sex
#convert the NIH sex at birth (equal to demo_sex_v2)
demographics_set[, sex_br := (sex == "F")*1]
demographics_set[,demo_sex_v2 := NULL]

 

########### age
#interview age will be used instead of age
demographics_set[,demo_brthdat_v2:=NULL]
demographics_set[, age := as.numeric(as.character(interview_age))]

 

########### gender
demographics_set[,gender := as.numeric(as.character(demo_gender_id_v2))]
demographics_set[(gender %in% c(777,999)) ,gender := NA]
demographics_set[, gender:= gender-1]
demographics_set[, demo_gender_id_v2:= NULL]

 

########### ethnicity
demographics_set[demo_ethn_v2 == "1", ethnicity_hisp := 1]
demographics_set[demo_ethn_v2 == "2", ethnicity_hisp := 0]
demographics_set[(demo_ethn_v2 %in% c(777,999)), ethnicity_hisp:= NA]
demographics_set = demographics_set[, demo_ethn_v2 := NULL]

 

########### child race
#"refuse to answer" and "dont know" will be 0

 

# White
demographics_set[, race_white:= (demo_race_a_p___10 == "1")*1 ]

 

# Black
demographics_set[, race_black:= (demo_race_a_p___11 == "1")*1 ]

 

# Asian
demographics_set[, race_asian:= 0]
demographics_set[ (demo_race_a_p___18 == "1" | demo_race_a_p___19 == "1" | demo_race_a_p___20 == "1" |
demo_race_a_p___21 == "1" | demo_race_a_p___22 == "1" | demo_race_a_p___23 == "1" |
demo_race_a_p___24 =="1"), race_asian:= 1 ]

 

# AIAN: American Indian and Alaska Native
demographics_set[, race_aian:= 0]
demographics_set[ (demo_race_a_p___12 == "1" | demo_race_a_p___13 == "1"), race_aian:=1 ]


#NHPI: Native Hawaiian and Other Pacific
demographics_set[, race_nhpi:= 0]
demographics_set[ demo_race_a_p___14 == "1" | demo_race_a_p___15 == "1" | demo_race_a_p___16 == "1" |
demo_race_a_p___17 == "1", race_nhpi:= 1 ]

 

# Other
demographics_set[, race_other:= 0 ]
demographics_set[ demo_race_a_p___25 == "1", race_other:= 1 ]

 

# Mixed
demographics_set[, race_mixed:= (race_white + race_black + race_asian + race_aian + race_nhpi + race_other)]
demographics_set[, table(race_mixed, useNA = "if")]
demographics_set[ race_mixed <= 1, race_mixed:= 0]
demographics_set[ race_mixed > 1, race_mixed:= 1]
demographics_set[, table(race_mixed, useNA = "if")]

demographics_set[,colnames(demographics_set)[(grepl("^demo_race_a_p___",colnames(demographics_set)))] := NULL]


########### child's country of birth
demographics_set[, born_in_usa := 0]
demographics_set[demo_origin_v2 %in% c(777,999), born_in_usa := NA]
demographics_set[demo_origin_v2 == 189, born_in_usa := 1]


########### parents education
demographics_set[(demo_prnt_ed_v2 %in% c(777,999)), demo_prnt_ed_v2:= NA]
demographics_set[(demo_prtnr_ed_v2 %in% c(777,999)), demo_prtnr_ed_v2:= NA]
demographics_set[, parents_avg_edu:= (as.numeric(as.character(demo_prnt_ed_v2)) + as.numeric(as.character(demo_prtnr_ed_v2)))/2]
#in case of edu is missing in one of the parents, it will be the edu the of other parent
demographics_set[is.na(parents_avg_edu), parents_avg_edu:= as.numeric(as.character(demo_prnt_ed_v2))]
demographics_set[is.na(parents_avg_edu), parents_avg_edu:= as.numeric(as.character(demo_prtnr_ed_v2))]

 

########### family income
demographics_set[,household_income:= demo_comb_income_v2]
demographics_set[( household_income %in% c(777,999)) ,household_income:= NA]
demographics_set[,demo_comb_income_v2 := NULL]

 

########### parents married status
demographics_set[demo_prnt_marital_v2 == 777 , demo_prnt_marital_v2:= NA]

demographics_set[,separated_or_divorced := 0]
demographics_set[(demo_prnt_marital_v2 %in% c(3,4)), separated_or_divorced := 1]

demographics_set[,parents_married := 0]
demographics_set[(demo_prnt_marital_v2 == 1), parents_married := 1]


demographics_set = droplevels(demographics_set)

 

Re: Converting R to JMP script

@GraphicalScore9, Thanks for making your questions more specific here.  This might be of help in general: The JMP to R Add-In Builder - JMP User Community.

@txnelson, Is this something you can help out with?  The scriptable logic in the formula editor is straightforward to understand in JMP for the non-scripter - seems like what the user needs here is some basic interpretation of the logic for creating these new variables names from the R source code herein - then from there, if they are a JMP user familiar with column formulas (inside the script editor or the formula editor), it should be pretty trivial to implement inside the JMP data table environment.

mark_anawis
Level IV

Re: Converting R to JMP script

What is occurring in these steps is data cleanup. "demographics_set" is a data table which is similar to a JMP data table. 

R uses the convention: demographics_set[rows,columns]. Therefore, when you have the following line below, it is applied to all the rows since there is nothing inside the brackets to the left of the comma. For the column "sex_br", it assigns a value of 1 for "F" and 0 for "M" (and anything NOT "F"). The logic is that (sex == "F") returns 1 when TRUE and 0 when FALSE. The ":=" is a left assignment operator (infix). R may not recognize this operator at first so you may need to run the line: `:=` <- `<-` before the rest of the code. This assigns the more typical assignment operator ("<-") to ":=".

demographics_set[, sex_br := (sex == "F")*1]

 Here's another example where there is row subsetting below. For the rows where "demo_ethn_v2" is equal to "1", assign a value of 1 to the column "ethnicity_hisp".

 

demographics_set[demo_ethn_v2 == "1", ethnicity_hisp := 1]

 The is.na() function selects the rows which are not NA for the column inside the brackets.

The as.character() and as.numeric() function convert the column to either character or numeric as stated. Sometimes you need to first convert to character and then to numeric, hence the nested functions which occur in the code.

 

After all this data cleaning, the following line drops factor levels that are no longer used due to subsetting the data frame.

 

demographics_set = droplevels(demographics_set)

  That said, I agree with the others in that it would probably be easier for you to embed this R code inside a JMP script and use the R functions to run the R code. Check out the Scripting Index in JMP under Help menu to find the R functions. You'll probably need R Init() to initialize the connection to R, R Send() to wrap around any file or variable you want to input into the R code, R Submit() to wrap around your R code, R Get() to retrieve output from R back into JMP (or R Get Graphics() if you are retrieving a graphical object), and R Term() to terminate the R interface. Sounds more complicated than it is, but probably still easier than re-writing all the R code as JSL (JMP scripting language). The scripting index has some nice examples to help you along.

Re: Converting R to JMP script

Thank you Mark! This is very helpful!

 

Regarding some of the resources you mentioned connected to the R functions available in JMP, I wanted to note here that a good reference would also be the online support documentation: https://www.jmp.com/support/help/en/16.2/#page/jmp/import-data-from-r.shtml#

 

Yours, Patrick

Re: Converting R to JMP script

I should also note that another user from the community contacted me and tried to run the R code, but had some errors; noting that the likely root-cause from the R script written "as-is" has to do with outdated path settings in the utility files. 

 

@GraphicalScore9 I recommend that you reach out to the owner of the GIT repository or maybe see if the csv file already exists.  Unfortunately, it's not uncommon for R code to stop working (this one of the problems with open source software that you do not have with JMP or SAS).  

Re: Converting R to JMP script

THANK YOU SO MUCH.  I appreciate your time and help!