asnumeric for all the content of my function in R

Hi everyone,

I have a tiny problem in R which makes code generation longer, and interpretation of results more painful.
It looks as though I have to tell R that the content of variables in my (panel) dataframe are numbers (by using as.numeric; and even to tell it to look into my df by using df$ in front of the variable of interest.

Thus, I have to write all my functions as

panelivmodel = plm(as.numeric(dataset1$logHealthExpPC) ~ 
as.numeric(dataset1$program) + as.numeric(dataset1$BA2TOT) + 
lag(as.numeric(dataset1$logGDPpc),1) + 
lag(as.numeric(dataset1$lognetODApc),1) + 
lag(as.numeric(dataset1$democracy),1) + 
as.numeric(dataset1$dependency_total) +  as.numeric(dataset1$urbanpop) + as.numeric(dataset1$conflict), 
data = dataset1, effect = "twoways")

when I would like to write

panelivmodel = plm( logHealthExpPC ~ program + BA2TOT + 
lag(logGDPpc,1) +  lag(lognetODApc),1) + 
lag(as.numeric(dataset1$democracy,1) + 
dependency_total  +  urbanpop + 
conflict, data = dataset1, effect = "twoways")

The thing is, in the second case, R does not even seem to notice plm is a function that expects arguments in it; when I type something (e.g. effect) it suggests me the effects function, instead of the effect *argument

How should R know that plm is a function?
Did you define the function yourself or does it belong to a package?
The function is not familiar to me.
And with the information you provide I can not help you.

Try to make a very small program with your problem. Show where the plm function comes from.
In the ideal case you would use the reprex package to do this but in any case read the article whats-a-reproducible-example-reprex-and-how-do-i-do-one .

Hope this helps.

Also look at the placement of the ) characters :wink:

In the tidyverse, you can use mutate_all or mutate_at to convert all or some of the columns in dataset1 to numeric before running plm. So something like...

library(tidyverse)

dataset1 <- mutate_all(dataset1, as.numeric)
panelivmodel = plm(...)

Also, in that call to plm, when you specify data = dataset1, you don't need to put dataset1 in front of the column names in your formula. So just...

plm(logHealthExpPC ~ program + ... + conflict,
    data = dataset1, effect = "twoways")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.