That code clip did a lot of downloading, here's the reprex()
install.packages("devtools", repos = "http://cran.r-project.org")
#> Installing package into 'C:/Users/userOne/Documents/R/win-library/3.5'
#> (as 'lib' is unspecified)
#> package 'devtools' successfully unpacked and MD5 sums checked
#>
#> The downloaded binary packages are in
#> C:\Users\userOne\AppData\Local\Temp\RtmpYV53Gm\downloaded_packages
devtools::install_github("r-lib/pkg")
#> Skipping install of 'pkg' from a github remote, the SHA1 (582d0d5f) has not changed since last install.
#> Use `force = TRUE` to force installation
if (require(pkg)) {
pkg::pkg_install("caret")
}
#> Loading required package: pkg
#> i Checking for package metadata updates
#> v Using cached package metadata
#> v 1 + 60 pkgs | kept 60, updated 0, new 0 | downloaded 0 (0 B) [3.7s]
@Taras mentioned that I should have multiple versions of R and rStudio. From what I know I have only one. Should I try to add more, or is there a good way that this should be done?
Sorry for the late response, I was updating the file and accidentally deleted it. I pieced it back together from my prior post. From what I can see library(caret) is now working. However, I'm getting an error about my data that I don't understand. I checked the excel and the "Age" column is there.
Reprex():
model <- train(Fentanyl~
Age+Sex+Race+
Oxycodone+Hydrocodone+Buprenorphine+
Morphine+Codeine+Norbuprenorphine+
Naloxone,
ToTrain,
method = "glm", family = "poisson",
trControl = ctrl,
tuneLength = 10)#end train
#> Error in train(Fentanyl ~ Age + Sex + Race + Oxycodone + Hydrocodone + : could not find function "train"
install.packages("devtools", repos = "http://cran.r-project.org")
#> Installing package into 'C:/Users/userOne/Documents/R/win-library/3.5'
#> (as 'lib' is unspecified)
#> package 'devtools' successfully unpacked and MD5 sums checked
#>
#> The downloaded binary packages are in
#> C:\Users\userOne\AppData\Local\Temp\Rtmp6HFdgS\downloaded_packages
devtools::install_github("r-lib/pkg")
#> Skipping install of 'pkg' from a github remote, the SHA1 (582d0d5f) has not changed since last install.
#> Use `force = TRUE` to force installation
if (require(pkg)) {
pkg::pkg_install("caret")
}
#> Loading required package: pkg
#> i Checking for package metadata updates
#> v Using cached package metadata
#> v 1 + 60 pkgs | kept 60, updated 0, new 0 | downloaded 0 (0 B) [3.8s]
library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2
predicted <- predict(model, ToTrain["Fentanyl"], type = "prob")
#> Error in predict(model, ToTrain["Fentanyl"], type = "prob"): object 'model' not found
#actual <- ToTrain["Fentanyl"]
I think I might be doing something fundamentally wrong. When I run a script in rStudio I put the cursor at the top and hit run on each line. Sometimes I have issues when running on an install.packages(somePackage) line. I will get a prompt asking if I want to restart rStudio, I'll hit yes, but it seem to get stuck in a loop asking me to restart over and over again. So what I do now is, under the packages tab in the user library, I just check each package I want and then run the library(somePackage) command.
Is all of this okay, or should I be doing it a different way?
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 the train function from caret package
Since you attempt to do this before you actually load the caret package, you get an error (obviously), and the object model is not created.
You then load the caret package.
When you try to create a predicted object, it fails. Why? Well, because you call your model 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.