0

I have created a function in R "EffectSize" which gives me a table of data when I run it.

i.e.

EffectSize=function(data,conf.level,statistic){
...
}

and running

EffectSize(epidural, 0.95, "OR")

I get:

    Author    OR ln(OR) Var ln(OR)            95% CI
1    A 0.894 -0.112   0.309967  (0.3001, 2.6611)
2   B 0.908 -0.097   0.064500  (0.5519, 1.4934)
3  C 0.824 -0.194   2.130252 (0.0471, 14.3893)
4 D 0.874 -0.135   0.015054  (0.6871, 1.1115)
5     E 0.764 -0.269   0.021964  (0.5712, 1.0212)
6 F 0.328 -1.116   0.415859  (0.0926, 1.1599)

Now I want to create another function which uses values from this table.

I want the function to be structured in the same way so that it is:

InVar=function(data,conf.level,statistic){
...
}

Just as a bit of background to the problem, I want that

weight=1/varodds
muhat=sum(weight*logodds)/sum(weight)
varmuhat=1/sum(weight)

To achieve this I have tried to do things like

InVar=function(data,conf.level,statistic){
weight=1/EffectSize[,4]
...
}

but I am not getting any luck.

I hope I have explained the kind of thing I am after well enough and would appreciate any help you can offer.

Many thanks

2
  • 1
    EffectSize is a function not a dataframe/matrix. The way you are referencing it won't work. I would suggest reading up on R data structures and functions. cran.r-project.org/doc/manuals/r-release/R-intro.html Commented Dec 6, 2014 at 12:35
  • It is always nice to have questions where as much as possible of the things unrelated to the problem (my function does not work) is stripped away and the things that are actually posted work (in this case break) as posted. So try to show real, minimal code instead of bloated pseudo-code - please. Put differently: I had to read a lot and still had to guess and make assumptions about the problem and how it might have happened. Commented Dec 6, 2014 at 12:58

2 Answers 2

2

I can't tell exactly how your EffectSize function works, but I assume it's generating the table as a side-effect and not returning an object.

Assign the table to an object inside the function and then have the function return that object:

EffectSize=function(data,conf.level,statistic){
...
table <- functionThatMakesTable
return(table)
}

Then in your second function use EffectSize and extract information from the resulting table object:

InVar=function(data,conf.level,statistic){
weight=1/EffectSize(data,conf.level,statistic)[,4]
...
return(weight)
}

A cleaner approach would be to calculate the weight inside EffectSize and either return both the table and weight as a list or just the weight:

EffectSize=function(data,conf.level,statistic){
...
table <- functionThatMakesTable

print(table) # see the table in the output

weight <- 1/table[,4]

return(list(table, weight)) # or return(weight) if you just want to see the table and not use it later
}

Regarding your original code, as @Avinash points out, you have to apply a function to index the object it returns - you can't index the function itself.

Sign up to request clarification or add additional context in comments.

Comments

2

To give a short answer: You cannot subset functions.

suppose you have ...

dummy <- function(){
  # doing some stuff
  results <- data.frame(1:3, letters[1:3])
  return(results)
}

what you cannot do is the following ...

dummy[1,]

.. but that one will work ...

dummy()[1,]

dummy[1,] tries to subset the object dummy. BEcause dummy is a function and not matrix or an data.frame it does not support this method.

On the other hand dummy()[1,] works because it tries subsetting the result produced by dummy() instead of the function itself.


Therefore, I suppose that instead of this line ...

weight=1/EffectSize[,4]

that one should work for you:

weight=1/EffectSize()[,4]

2 Comments

Brilliant answer to a not-that-much question. +1!
Thanks for the help. Sorry I struggled to express what I really wanted to do but this helped me thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.