yes, absolutely.
You shouldn't be running install.package
more than 1 time per package ever. Just like you don't install a piece of software every time you need to run it, you don't install packages every time you run the script.
I suggest you remove every mentioning of any flavor of install.package
from your script.
Now to your script.
As a rule of thumb, a good habit is to load packages at the top of your script.
What we have here is:
- You attempt to create a
model
object using thetrain
function fromcaret
package - Since you attempt to do this before you actually load the
caret
package, you get an error (obviously), and the objectmodel
is not created. - You then load the
caret
package. - When you try to create a
predicted
object, it fails. Why? Well, because you call yourmodel
object, but remember that it doesn't exist because it failed earlier.
Your code should probably look something like this instead (my best guess based on reading your code, I haven't actually run it):
library(caret)
model <- train(Fentanyl~
Age+Sex+Race+
Oxycodone+Hydrocodone+Buprenorphine+
Morphine+Codeine+Norbuprenorphine+
Naloxone,
ToTrain,
method = "glm", family = "poisson",
trControl = ctrl,
tuneLength = 10)
predicted <- predict(model, ToTrain["Fentanyl"], type = "prob")
Here are a few suggestions beyond this particular problem that I hope might help you in the future.
- You may benefit from looking into programming fundamentals and understanding the process.
- A book about R fundamentals may also help you conquer the language better. I suggest starting with "R For Data Science"
- Error messages are sometimes helpful and may guide you in troubleshooting.