Firstly I aplogise because this question has no doubt been answered in another page eg How do I use a macro variable in R? (Similar to %LET in SAS) and Pass string as name of attached data column name but I’ve read them, watched Youtube tutorials, thought that the function command was what I wanted and I just can’t get it to work. I’m very new to programming as a whole, and only few days in for R, and I’m sure I’m missing something due to my lack of experience or understanding of even where to start.
In SAS I would do it like this
%Macro DV (var1, var2);
Proc mixed …
Model &var1 = &var2 …
Data &var1_&var2…
%mend DV;
%DV (VAS, Hyd)
%DV (SOM, Hyd)
%DV (GAS, Hyd)
The variables (eg. VAS, Hyd) that I’m calling in are column labels, they get called in, the column analysed in a mixed model and, with a bit more code, the dataset gets exported with a filename according to the variables analysed. I’m not trying to do a lmer in R I’m using the rmcorr package and want to change the columns that get analysed. The original expression, that works, looks like
VASCor.rmc <- rmcorr(participant = Subject, measure1 = Hyd, measure2 = VAS, dataset = mydata)
plot(VASCor.rmc,mydata, overall = T, lty=1, xlab = "Hyd", ylab = "VAS")
A working dataset can be found below. So just working on the first expression I tried the following, thinking that the var1 and var2 would get inserted in the correct places but I just end up with an error "object 'var2' not found".
Allrmcorr <- function(var1, var2){
var1.var2.rmc <- rmcorr(participant = Subject, measure1 = var2, measure2 = var1 , dataset = Results2)
}
Allrmcorr("VAS","Hyd")
Allrmcorr("VAS","Hyd")
I've attempted a number of expressions and probably spent long enough trying to figure it out to have just rewritten the expression time, I'm not closer but being stubborn I want to know the better way for next time. Any help would be greatly appreciated.
mydata <- data.frame(
Subject = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
Time = c(1:5,1:5,1:5),
VAS= runif(15),
Hyd =runif(15),
GAS= runif(15)
)
I am not familiar with rmcorr but your problem stems from how your function takes input paramets. Look carefully at your code:
rmcorr(participant = Subject, measure1 = var2, measure2 = var1 , dataset = Results2)
takes measure1 and measure as bare names input (so, a language element, not a string literal like "var1" (in quotes).
While you pass string literals to the function with Allrmcorr("VAS","Hyd"), rmcorr wants bare names that it evaluets in them frame of dataset. All this is probably a bit complicated an off-putting if you are new to R. I recommend reading "Advanced R" by Hadley Wickham if you want to dive deeper into the topic (especially the chapters on functions and lazy evaluation).
A simple solution to your problem - which is similar to how macro variables work - is constructing your expression as a string and then evaluating it:
Allrmcorr <- function(var1, var2, dat){
ds <- dat
eval(parse(text = (sprintf(
"rmcorr(participant = Subject, measure1 = %s, measure2 = %s, dataset = ds)", var1, var2
))))
}
Allrmcorr("VAS", "Hyd", mydata)
This has severe disadvantages when it comes to performance and debuggability of your code; however, I was not able to find a better solution to your problem since the internal implementation of rcmorr is somewhat weird.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With