I have recently run into a problem running a GAM model from a previously working code. I believe it is related to an updated R-Version and an updated Version of the mgcv package. It would be great to know if anyone has the same problem or has a solution to it.
I am currently running R version 3.2.2 (2015-08-14) -- "Fire Safety"
on Windows. I am using the mgcv Package 1.8-7. Below is an example code that re-produces the error message, when run on my computer.
###Load package
library(mgcv)
This is mgcv 1.8-7.
###Simulate some example data
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=400,dist="normal",scale=2)
###Run normal model
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat, family=gaussian())
This works.
###change the smoothness selection method to REML
b0 <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat,method="REML")
Gives the following error message:
Error in .C(C_gdi1, X = as.double(x[good, ]), E = as.double(Sr), Eb = as.double(Eb), : Incorrect number of arguments (48), expecting 47 for 'gdi1'
Thanks for your help!
I have re-installed R and the mgcv package and it seems as if this has resolved the issue.
Related
I am using R version 3.6.0 and I am following this guide in order to run a zero-inflated poisson regression on cholera data: https://stats.idre.ucla.edu/r/dae/zip/
I have installed the pscl package as instructed, but when I run my code, an error message shows.
cholera2 <- within(cholera, {wateraccess <- factor(wateraccess, order=TRUE, levels=c(1,2,3,4))})
summary(cholera2)
summary(m1 <- zeroinfl(cases ~ wateraccess + atpc, data = cholera2))
I tried using a different version of R and it has the same issue.
Error message: Error in zeroinfl(cases ~ wateraccess + atpc, data = cholera2) : could not find function "zeroinfl"
I'm trying to explore the difference in how the gam function works in the mgcv package versus the gam package. But I'm not able to run both gam functions in one R session. I thought if I preface with mgcv::gam or gam::gam it would be able to run the right function, but it looks like I have to detach mgcv in order to run the gam function in the gam package.
library(ISLR)
library(mgcv)
library(gam)
# I get an error message when it runs this
gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
# No error message when I detach mgcv
detach(package:mgcv)
gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Is there a way I can run both gam functions in one session?
Below is the output:
> library(ISLR)
> library(mgcv)
> library(gam)
> #I get an error message when it runs this
> gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Error in terms.formula(reformulate(term[i])) :
invalid model formula in ExtractVars
> #No error message when I detach mgcv
> detach(package:mgcv)
> gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Warning message:
In model.matrix.default(mt, mf, contrasts) :
non-list contrasts argument ignored
Update: I re-ran this with a clean R session and the story is different. Before, I cleared the workspace but did not have a clear R session. Now, if I run with a clean session the gam.m3 model seems to work. BUT - if I change the order of the library load, and load gam before mgcv, I get the same error. When mgcv is loaded after gam is loaded, I do get this message:
The following objects are masked from ‘package:gam’:
gam, gam.control, gam.fit, s
So I guess just part of the deal of loading mgcv is that you can no longer use certain functions in gam? That is annoying. FYI I get the analogous warning message when loading gam after mgcv is loaded - that certain objects will be masked from package:mgcv.
As shown in my answer to your other question, you can't use gam::s.
However, you can tell R to evaluate the call in the gam package namespace:
library(ISLR)
library(gam)
fit1 <- gam(wage~s(year,4)+s(age,5)+education,data=Wage)
library(mgcv)
gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
#errors
fit2 <- eval(quote(gam(wage~s(year,4)+s(age,5)+education,data=Wage)),
envir = asNamespace("gam"))
#works
all.equal(coef(fit1), coef(fit2))
#[1] TRUE
I have the R version 3.2.0 in one computer and the version 3.2.2 in another and the same version of the package "forecast" (6.1) in both of them. When I apply the auto.arima function with d=1 to the vector fec1 (data below) in the computer with version 3.2.0, it gives me an ARIMA(1,1,1). In the computer with version 3.2.2, it gives me an ARIMA(1,1,0) with drift instead. Why does such a difference happens? When I omit the option d=1, I get the same result in both computers.
fec1 <- c(9.53408021, 9.48993631, 9.44261613, 9.38585482, 9.31611858,
9.24743801, 9.19483663, 9.11690414, 9.05586032, 8.96781034,
8.85437896, 8.73956021, 8.62587246, 8.49758284, 8.41877774,
8.29737132, 8.02294327, 7.62604834, 7.32936532, 7.10006394,
6.87039464, 6.56332757, 6.27195705, 5.97089310, 5.69537808,
5.46074967, 5.19061116, 4.90749261, 4.61822473, 4.39635507,
3.96883450, 3.71230296, 3.43604354, 3.15150833, 2.82864081,
2.51857560, 2.27264470, 2.06785600, 1.86116599, 1.61452534,
1.41214847, 1.26673153, 1.15033592, 1.00646045, 0.83493055,
0.65399085, 0.47450499, 0.30408840, 0.18216540, 0.09006937,
0.00000000)
See PR#16278, "nested arima model has higher log-likelihood", which as noted in the change log for 3.2.1, affects arima calculations with d >= 1:
arima(*, xreg = .) (for d >= 1) computes estimated variances
based on a the number of effective observations as in R version
3.0.1 and earlier. (PR#16278)
Been looking into Harrell's rms package for R and I am getting an error whenever I try to use the spline function rcs() inside a formula. This happens with all datasets. Is there some R setting I am missing?
library(rms)
df = read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
m = lrm(admit~ rcs(gre), df)
Error in rcspline.eval(x, nk = nknots, inclx = TRUE, pc = pc, fractied = fractied) :
unused argument (fractied = fractied)
What is "fractied"? I haven't been able to find a clear explanation.
I'm using Windows 7, R version 3.1.0, rms version 4.2.0
i am newbie to R
i am using R 3.0.1 version,
I have installed rpart package using
install.packages("rpart")
selected USA(CA 1) in the pop up box and it is installed successfully
> p_data = read.csv(file="/home/sudeep/Desktop/mysql.tsv",sep="\t",dec=".",header=TRUE)
> dtree <- rpart(paid ~ .,data = p_data, method="class")
Error: could not find function "rpart"
so what is wrong with me??
You don't actually need to install rpart manually; it comes with the base R distribution. However, you do need to load it into your R session with a call to library:
library(rpart)
rpart(paid ~ ., data=....)