How to pass function arguments to R model object - r

I would like to pass function arguments to R glm model objects. However, it seems my code below didnot work as R didn't recognize the passing argument.
I kept getting the error: non-numeric argument to binary operator
grid = (cbind(c('wl', 'livingd', 'deceasedt'), c('wl_time', 'ld_time', 'dec_time')))
for (k in 1:nrow(grid)){
f=function(y=as.name(grid[k,1]), offset=as.name(grid[k,2])){
m=glm(y~chain_class2+sex_new+age_cat+race_new,
family=poisson(link='log'),
data=poissonset,
offset=log(offset/12))
}
}
Is there a way to pass the variable names to the function? Thank you!

Try this:
grid = (cbind(c('wl', 'livingd', 'deceasedt'), c('wl_time', 'ld_time', 'dec_time')))
for (k in 1:nrow(grid)){
f=function(y=as.name(grid[k,1]), offset=as.name(grid[k,2])){
m=glm(get(y)~chain_class2+sex_new+age_cat+race_new,
family=poisson(link='log'),
data=poissonset,
offset=log(get(offset)/12))
}
}

Related

unused argument error when printing from RC class method

I am creating an RC class and while trying to print(.self$something) within a class method I am getting:
Error in print(.self$something) : unused argument (.self$something)
I am sort of new to R, so am I missing something here?
This is for an assignment which asks us to use RC classes, using R6 is not an option.
myclass <- setRefClass("myclass",
fields = list (
formula = "formula",
data = "data.frame",
something = "numeric"
),
methods = list (
initialize = function(formula, data) {
...
},
print = function() {
...
print(.self$something)
},
)
)
a <- myclass$new(formula,data)
a$print()
> Error in print(.self$something) : unused argument (.self$something)
Edit: Extra info, if I try a$something I get what I should get.
Try to use cat in your print function, you are now in your local print function environment and trying to call your system "print" function. I suggest you use cat as follows:
cat(.self$something)
It will do the job
As #Mohammed mentions, this happened because I was in printing within my own print environment. Though cat() could be an option, later I faced other issues in which cat did not print the object (that could be a thread on its own so I will not go deeper on that here).
What I ended up doing was calling the print function for that specific data type. For instance, if something was a data.frame I called print.data.frame(.self$something) and worked as expected.

argument "term" is missing, with no default ,when creating a function

I want to calculate the loan outstanding at valuation date for a data set. I define a function but when I call the function its giving an error saying the fifth argument is missing(term).
Function is as follows:
loan_outstanding<-function(c_date,v_date,l_amt,int ,pmt,term){
l_amt<-as.numeric(l_amt)
freq<-12
term_in_months<-as.numeric(term)*freq
c_date<-as.Date(c_date,"%d/%m/%Y")
Interest_Rate<-per_to_num(int)
v_date<-as.Date(v_date,"%Y-%m-%d")
date_<-numeric(500)
date_[1]<-as.character(c_date)
int_cal<-numeric(500)
cap_repay<-numeric(500)
loan_out<-numeric(500)
loan_out[1]<-l_amt
i<-2
while(as.Date(date_[i-1],"%Y-%m-%d")<v_date){
date_[i]<-as.character(AddMonths(as.Date(date_[i-1],"%Y-%m-%d"),1),"%Y-%m-%d")
int_cal[i]<-loan_out[i-1]*((1+Interest_Rate)^(1/freq)-1)
cap_repay[i]<-pmt-int_cal[i]
loan_out[i]<-max(loan_out[i-1]-cap_repay[i],0)
i<-i+1
}
val<- loan_out[i-2]
return(val)
}
The error :
>loan_outstanding("28/07/2011","2017-03-31",500000,7,9629.918)
argument "term" is missing, with no default
Is there a mistake in my code?
This is not a pretty solution, but I just made another variable and put it after the last one in the function definition:
...pmt, term, [new var]) {
It solved the problem for me.

Insert variable name into a function in R

I'm trying to construct a function that calculate some variances using the survey package. The problem is that I need to insert the name of the variable (not the values of the variables) into a specific function (svyby)
Is something like this:
myfun=function(variable) {
svyby(~variable,~subpop,design,svymean)
}
myfun(P16)
It gives me error. I also tried with
*base[,variable]*
instead of
*variable*
the problem here that base[,variable] gives me the vector with the values of the variable, but I need the name of the variable to be read in the design object. What I mean is, I need that the function insert the name like this
svyby(~P16,~subpop,design,svymean)
I will appreciate any help, thank you in advance,
Gonzalo
Looks like it needs a formula. You can paste a "~" to a string and use as.formula, like this:
myfun = function(variable) {
svyby(as.formula(paste("~", variable)),
~subpop, design, svymean)
}
And then call is like this: myfun("P16"). Note that you will need to use a quoted column name because you are treating it like a string.
Alternatively, you could have your function take a formula:
myfun2 = function(formula) {
svyby(formula,
~subpop, design, svymean)
}
And call it like this: myfun2(~P16).

Warning meassage: number of items to replace is not a multiple of replacement length

I got warnings when running this code.
For example, when I put
tm1<- summary(tmfit)[c(4,8,9)],
I can get the result, but I need to run this code for each $i$.
Why do I get this error?
Is there any way to do this instead of via a for loop?
Specifically, I have many regressants ($y$) with the same two regressors ($x$'s).
How I can get these results of regression analysis(to make some comparisons)?
dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm=dreg[,4]
for(i in 2:num)
{
tmfit=lm(fundr[,i]~exr+tm)
tm1[i]<- summary(tmfit)[c(4,8,9)]
}
Any help is highly appreciated
Try storing your result into a list instead of a vector.
dreg=read.csv("dayreg.csv")
fundr=read.csv("fundreturnday.csv")
num=ncol(fundr)
exr=dreg[,2]
tm = list()
for(i in 2:num)
{
tmfit=lm(fundr[,i]~exr+tm)
tm1[[i]]<- summary(tmfit)[c(4,8,9)]
}
You can look at an element in the list like so
tm1[[2]]

How to use function with variable in R?

in R,the pdf function can save graph in c:/test:
pdf("c:/test")
I want to make a variable substitue pdf ,how can i make it run ?
str<-"pdf"
str("c:/test")
get() does this:
get(str)("c:/test")
s = "pdf" ; do.call(s, list("c:/test"))
or in two steps,
cl <- call(s, "c:/test")
eval(cl)
You can extract the function specified by the name in str with match.fun:
match.fun(str)("c:/test")
By the way: It is not a good idea to name an object str since this is the name of a basic function in R.

Resources