2

thank you for your time!

I am trying to run a chi-squared test through each of the variables in my dataset, and I am executing it with a loop that runs through a list. However, I am trying to figure out the syntax to make the loop print the variable name as it iterates through. Here is my code

#Defining Datalist
dataList = list(db.binom$Immigrant,db.binom$`Known to Police or FBI`, db.binom$`Criminal Record`,
                db.binom$`History of Physical Altercations`, db.binom$`History of Animal Abuse`,
                db.binom$`History of Domestic Abuse`,db.binom$`History of Sexual Offenses`,db.binom$`Gang Affiliation`
,db.binom$`Terror Group Affiliation`,db.binom$Bully,db.binom$Bullied,db.binom$`Raised by Single Parent`
,db.binom$`Parental Divorce / Separation`,db.binom$`Parental Death in Childhood`
,db.binom$`Parental Suicide`,db.binom$`Childhood Trauma`,db.binom$`Physically Abused`
,db.binom$`Sexually Abused`,db.binom$`Emotionally Abused`,db.binom$Neglected
,db.binom$`Mother Violent Treatment`,db.binom$`Parental Substance Abuse`
,db.binom$`Parent Criminal Record`,db.binom$`Family Member Incarcerated`
,db.binom$`Signs of Being in Crisis`,db.binom$`Inability to Perform Daily Tasks`
,db.binom$`Notably Depressed Mood`,db.binom$`Unusually Calm or Happy`,db.binom$`Rapid Mood Swings`
,db.binom$`Increased Agitation`,db.binom$`Abusive Behavior`,db.binom$`Isolation`
,db.binom$`Losing Touch with Reality`,db.binom$`Paranoia`,db.binom$`Prior Hospitalization`  
,db.binom$`Prior Counseling`,db.binom$`Psychiatric Medication`,db.binom$`Treatment 6 Months Prior to Shooting`
,db.binom$`Autism Spectrum`,db.binom$`Health Issues`,db.binom$`Head Injury / Possible TBI`
,db.binom$`Motive: Racism/Xenophobia`,db.binom$`Motive: Religious Hate`,db.binom$`Motive: Misogyny`
,db.binom$`Motive: Homophobia`,db.binom$`Motive: Employment Issue`  
,db.binom$`Motive: Economic Issue`,db.binom$`Motive: Legal Issue`,db.binom$`Motive: Relationship Issue`
,db.binom$`Motive: Fame-Seeking`,db.binom$`Motive: Unknown`,db.binom$`Role of Psychosis in the Shooting`
,db.binom$`Interest in Past Mass Violence`,db.binom$`Relationship with Other Shooting(s)`
,db.binom$Planning,db.binom$Performance,db.binom$`Interest in Firearms`)

#Running the for-loop
for(i in dataList){
  print(names(dataList[[i]]))
  print(chisq.bintest(as.factor(db.binom$shooter) ~ (i), db.binom, correct = FALSE, alpha = 0.05, p.method = "fdr"))
}
lorrsto
  • 23
  • 3
  • You could have creatd the `dataList` easily, i.e. `dataList <- c(db.binom[yoursubsetofcolumnnames])`. Regarding the formula, it can be `print(chisq.bintest(reformulate(i, response = 'shooter'), db.binom, correct = FALSE, alpha = 0.05, p.method = "fdr")` where `db.binom$shooter <- factor(db.binom$shooter)` – akrun Nov 14 '22 at 16:52

1 Answers1

1

Create a vector of column names and then loop over the column names

library(janitor)
db.binom <- clean_names(db.binom)
db.binom$shooter <- factor(db.binom$shooter)
nm1 <- c("Immigrant", ..., "Interest in Firearms")
out <- vector("list", length(nm1))
names(out) <- nm1
for(nm in nm1)
  {
 print(nm)
 out[[nm]] <- chisq.bintest(reformulate(i, response = 'shooter'), db.binom,
     correct = FALSE, alpha = 0.05, p.method = "fdr")
print(out[[nm]])
}

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Sorry if this is a silly question, when defining the list, you use "Immigrant", ... "Interest in Firearms", am I correct in assuming that the ... should be filled in manually with all the other variable names? Or is there a way to make the machine iterate through this for me – lorrsto Nov 14 '22 at 17:03
  • @lorrsto I was thinking that you want only a subset of column names. If it is with all the column names except `shooter`. you can use `nm1 <- setdiff(names(db.binom), "shooter")` or you can index the column names i.e. `nm1 <- names(db.binom)[c(1:5, 7:10,....)]` etc – akrun Nov 14 '22 at 17:05
  • After running, I keep getting the following error: 1] "Sexual Orientation" Error in str2lang(termtext) : :1:8: unexpected symbol 1: Sexual Orientation ^ – lorrsto Nov 14 '22 at 17:22
  • @lorrsto what is the error you got – akrun Nov 14 '22 at 17:22
  • Error in str2lang(termtext) : :1:8: unexpected symbol 1: Sexual Orientation – lorrsto Nov 14 '22 at 17:23
  • @lorrsto can you try this on a subset of columns and see if the error is only for the 'Sexual Orientation' column? i.e. `nm1 <- names(db.binom)[1:3]` – akrun Nov 14 '22 at 17:23
  • @lorrsto I am guessing the spaces in the column names is causing the error. Can you try the update in the code i.e. `library(janitor);db.binom <- clean_names(db.binom)` and then use this cleaned dataset – akrun Nov 14 '22 at 17:26