Sorkin, John
2023-Oct-24 23:22 UTC
[R] by function does not separate output from function with mulliple parts
Colleagues, I have written an R function (see fully annotated code below), with which I want to process a dataframe within levels of the variable StepType. My program works, it processes the data within levels of StepType, but the usual headers that separate the output by levels of StepType are at the end of the listing rather than being used as separators, i.e. I get Regression results StepType First Contrast results StepType First Regression results StepType Second Contrast results StepType Second and only after the results are displayed do I get the usual separators: mydata$StepType: First NULL ------------------------------------------------------------------------------ mydata$StepType: Second NULL What I want to get is output that includes the separators i.e., mydata$StepType: First Regression results StepType First Contrast results StepType First ------------------------------------------------------------------------------ mydata$StepType: Second Regression results StepType Second Contrast results StepType Second Can you help me get the separators included in the printed otput? Thank you, John #################### # Create Dataframe # #################### mydata <- structure(list(HipFlex = c(19.44, 4.44, 3.71, 1.95, 2.07, 1.55, 0.44, 0.23, 2.15, 0.41, 2.3, 0.22, 2.08, 4.61, 4.19, 5.65, 2.73, 1.46, 10.02, 7.41, 6.91, 5.28, 9.56, 2.46, 6, 3.85, 6.43, 3.73, 1.08, 1.43, 1.82, 2.22, 0.34, 5.11, 0.94, 0.98, 2.04, 1.73, 0.94, 18.41, 0.77, 2.31, 0.22, 1.06, 0.13, 0.36, 2.84, 5.2, 2.39, 2.99), jSex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("Male", "Female"), class = "factor")), row.names = c(NA, 50L), class = "data.frame") mydata[,"StepType"] <- rep(c("First","Second"),25) mydata ######################## # END Create Dataframe # ######################## ############################ # Define function to be run# ############################ DoReg <- function(x){ fit0<-lm(as.numeric(HipFlex) ~ jSex,data=x) print(summary(fit0)) cat("\nMale\n") print(contrast(fit0, list(jSex="Male"))) cat("\nFemale\n") print(contrast(fit0, list(jSex="Female"))) cat("\nDifference\n") print(contrast(fit0, a=list(jSex="Male"), b=list(jSex="Female"))) } ################################ # END Define function to be run# ################################ ######################################### # Run function within levels of Steptype# ######################################### by(mydata,mydata$StepType,DoReg) ############################################# # END Run function within levels of Steptype# ############################################# John David Sorkin M.D., Ph.D. Professor of Medicine, University of Maryland School of Medicine; Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center;? PI?Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; Senior Statistician University of Maryland Center for Vascular Research; Division of Gerontology and Paliative Care, 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 Cell phone 443-418-5382
Rui Barradas
2023-Oct-25 15:24 UTC
[R] by function does not separate output from function with mulliple parts
?s 00:22 de 25/10/2023, Sorkin, John escreveu:> Colleagues, > > I have written an R function (see fully annotated code below), with which I want to process a dataframe within levels of the variable StepType. My program works, it processes the data within levels of StepType, but the usual headers that separate the output by levels of StepType are at the end of the listing rather than being used as separators, i.e. I get > > Regression results StepType First > Contrast results StepType First > Regression results StepType Second > Contrast results StepType Second > > and only after the results are displayed do I get the usual separators: > mydata$StepType: First > NULL > ------------------------------------------------------------------------------ > mydata$StepType: Second > NULL > > > What I want to get is output that includes the separators i.e., > > mydata$StepType: First > Regression results StepType First > Contrast results StepType First > ------------------------------------------------------------------------------ > mydata$StepType: Second > Regression results StepType Second > Contrast results StepType Second > > Can you help me get the separators included in the printed otput? > Thank you, > John > > #################### > # Create Dataframe # > #################### > mydata <- structure(list(HipFlex = c(19.44, 4.44, 3.71, 1.95, 2.07, 1.55, > 0.44, 0.23, 2.15, 0.41, 2.3, 0.22, 2.08, 4.61, 4.19, 5.65, 2.73, > 1.46, 10.02, 7.41, 6.91, 5.28, 9.56, 2.46, 6, 3.85, 6.43, 3.73, > 1.08, 1.43, 1.82, 2.22, 0.34, 5.11, 0.94, 0.98, 2.04, 1.73, 0.94, > 18.41, 0.77, 2.31, 0.22, 1.06, 0.13, 0.36, 2.84, 5.2, 2.39, 2.99), > jSex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, > 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, > 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, > 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("Male", "Female"), class = "factor")), > row.names = c(NA, 50L), class = "data.frame") > > mydata[,"StepType"] <- rep(c("First","Second"),25) > mydata > ######################## > # END Create Dataframe # > ######################## > > ############################ > # Define function to be run# > ############################ > DoReg <- function(x){ > fit0<-lm(as.numeric(HipFlex) ~ jSex,data=x) > print(summary(fit0)) > > cat("\nMale\n") > print(contrast(fit0, > list(jSex="Male"))) > > cat("\nFemale\n") > print(contrast(fit0, > list(jSex="Female"))) > > cat("\nDifference\n") > print(contrast(fit0, > a=list(jSex="Male"), > b=list(jSex="Female"))) > } > ################################ > # END Define function to be run# > ################################ > > ######################################### > # Run function within levels of Steptype# > ######################################### > by(mydata,mydata$StepType,DoReg) > ############################################# > # END Run function within levels of Steptype# > ############################################# > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine, University of Maryland School of Medicine; > Associate Director for Biostatistics and Informatics, Baltimore VA Medical Center Geriatrics Research, Education, and Clinical Center; > PI?Biostatistics and Informatics Core, University of Maryland School of Medicine Claude D. Pepper Older Americans Independence Center; > Senior Statistician University of Maryland Center for Vascular Research; > Division of Gerontology and Paliative Care, > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > Cell phone 443-418-5382 > > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, What you are seeing is the prints and cats in the function DoReg, not its output. The code below works as expected. Also, you didn't load the package where function contrast() is found. library(contrast) ############################ # Define function to be run# ############################ DoReg <- function(x){ fit0 <- lm(as.numeric(HipFlex) ~ jSex,data=x) contrast( fit0, a=list(jSex="Male"), b=list(jSex="Female") ) } ################################ # END Define function to be run# ################################ ######################################### # Run function within levels of Steptype# ######################################### by(mydata,mydata$StepType,DoReg) #> mydata$StepType: First #> lm model parameter contrast #> #> Contrast S.E. Lower Upper t df Pr(>|t|) #> 1 2.99114 1.956013 -1.05518 7.037461 1.53 23 0.1399 #> ------------------------------------------------------------ #> mydata$StepType: Second #> lm model parameter contrast #> #> Contrast S.E. Lower Upper t df Pr(>|t|) #> 1 -2.435 1.819421 -6.198759 1.328759 -1.34 23 0.1939 Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com