Can anyone figure out why I get the error below after running the following code?
library(haven)
library(survival)
library(dplyr)
library(readr)
library(glmmLasso)
par(mar=c(1,1,1,1))
library(discSurv)
HH<- as.data.frame(read_dta("https://www.stata.com/data/jwooldridge/eacsap/recid.dta") )
HHC <- contToDisc(dataShort = HH, timeColumn = "durat", intervalLimits = 20,equi = TRUE)
dtLong<-dataLong(dataShort = HHC, timeColumn = "timeDisc",
eventColumn = "cens",timeAsFactor = FALSE)
formula.1<-y~factor(black)+factor(alcohol)
family<-binomial(link = "logit")
lambda <- 20
penal.vec<-20
next.try<-TRUE
BIC_vec<-rep(Inf,length(lambda))
Deltama.glm2<-as.matrix(t(rep(0,3)))#coefficients + Intercept
Smooth.glm2<-as.matrix(t(rep(0,20)))
j<-1;test.step<-1;
glm2 <- glmmLasso(formula.1,
rnd = NULL,family = family, data = dtLong, lambda=lambda[j],final.re=T,switch.NR=F,
control = list(smooth=list(formula=~- 1+as.numeric(timeInt),nbasis=20,spline.degree=3,
diff.ord=2,penal=penal.vec[test.step],start=Smooth.glm2[j,]),
method.final="EM", print.iter=T,print.iter.final=T,
eps.final=1e-4,epsilon=1e-4,complexity="non.zero",
start=Deltama.glm2[j,]))
Iteration 41
Final Re-estimation Iteration 9Error in Z_aktuell * D : non-conformable arrays
when I change Deltama.glm2<-as.matrix(t(rep(0,3))) to Deltama.glm2<-as.matrix(t(rep(0,2)))
I get the error
Iteration 1Error in grad.lasso[b.is.0] <- score.beta[b.is.0] - lambda.b * sign(score.beta[b.is.0]) :
NAs are not allowed in subscripted assignments
I have tried to remove the starting values as suggested on glmmLasso error and warning without success
Switching from R version 4.2.2 to 3.6.0 solved the issue. Seems there is a compatibility issue between glmmlasso and the newer versions of R.
Related
I'm trying to use the knn function in r but I keep getting this error message when I try to compute it.
> knn(Taxi_train,Taxi_test,cl,k=100)
Error in knn(Taxi_train, Taxi_test, cl, k = 100) :
NA/NaN/Inf in foreign function call (arg 6)
In addition: Warning messages:
1: In knn(Taxi_train, Taxi_test, cl, k = 100) : NAs introduced by coercion
2: In knn(Taxi_train, Taxi_test, cl, k = 100) : NAs introduced by coercion
I don't know what exactly is wrong with my code so I need some help to get it working.
I tried making sure that all the variables are numeric but that didn't change anything. It may also be an issue with my cl factor in the knn equation.
Here is what my code is currently:
date<-chicago_taxi$date
class(date)
Date <- as.Date(date)
class(Date)
Julian <- yday(Date)
class(Julian)
head(Julian)
chicago_taxi <- cbind(chicago_taxi,Julian)
chicago_taxi$seconds <- as.numeric(chicago_taxi$seconds)
set.seed(7777)
train_set <- sample(1:13081,10400,replace = FALSE)
Taxi_train <- chicago_taxi[train_set,]
Taxi_test <- chicago_taxi[-train_set,]
cl <- Taxi_train$payment_type
scale(chicago_taxi$miles)
scale(chicago_taxi$seconds)
scale(chicago_taxi$Julian)
knn(Taxi_train,Taxi_test,cl,k=100)
I try to recalculate exactly published Stata code in R. In the first step I exported the same dataset from Stata and Imported it in R. Nevertheless I am fighting with errors in my code.
What am I doing wrong?
Original Stata Code:
xtmelogit redpref1 c.incomedif c.incomedif#c.forpop forpop i.year if (brncntr==1) || country:
My Approach in R was:
dataset <- dataset%>%
filter(brncntr==1) %>%
mutate(c.incomedif = factor(incomedif))%>%
mutate(c.forpop = factor(forpop)) %>%
mutate(i.year = as.integer(year)))
library(lme4)
logit <- glmer(redpref1~ c.incomedif+ i.year|country,family=binomial,rueda4, nAGQ=0L)
summary(logit)
This approach generates an error as follows:
Error in if (ctrl$npt > (2 * n + 1)) warning("Setting npt > 2 * length(par) + 1 is not recommended.") :
missing value where TRUE/FALSE needed
In addition: Warning message:
In (n + 1L) * (n + 2L) : NAs produced by integer overflow
>
I tried the following codes:
library(quantreg) # to load the package
library(foreign) # to load the package
.Fortran("rqfn", PACKAGE = "quantreg")
but I get the following error:
Error in .Fortran("rqfn", PACKAGE = "quantreg") :
"rqfn" not available for .Fortran() for package "quantreg"
I have installed Rtools. But it does not solve the problem.
I also checked the issues concerning system paths (as in this site: https://github.com/stan-dev/rstan/wiki/Install-Rtools-for-Windows), but there is no problem about that.
Could anyone give me a hand? Thank you very much.
You can build your own library:
Download rqfn.f and rqfnb.f. The latter is needed for stepy method.
Call R CMD SHLIB rqfn.f rqfnb.f
use the function like this:
data(stackloss)
x <- stack.x
y <- stack.loss
n <- length(y)
p <- ncol(x)
dyn.load(paste0("rqfn", .Platform$dynlib.ext))
.Fortran("rqfn",
as.integer(n),
as.integer(p),
a = as.double(t(as.matrix(x))),
c = as.double(y),
rhs = double(p),
d = double(n),
beta = as.double(0.99995),
eps = as.double(1e-6),
tau = as.double(0.5),
wn = double(10 * n),
wp = double((p + 3) * p),
aa = double(p * p),
it.count = integer(2),
info = integer(1))
I'm working the npreg example in the R np package documentation (by T. Hayfield, J. Racine), section 3.1 Univariate Regression.
library("np")
data("cps71")
model.par = lm(logwage~age + I(age^2),data=cps71)
summary(model.par)
#
attach(cps71)
bw = npregbw(logwage~age) # thislne not in example 3.1
model.np = npreg(logwage~age,regtype="ll", bwmethod="cv.aic",gradients="TRUE",
+ data=cps71)
This copied directly from the example, but the npreg call results in error message
*Rerun with Debug
Error in npreg.rbandwidth(txdat = txdat, tydat = tydat, bws = bws, ...) :
NAs in foreign function call (arg 15)
In addition: Warning message:
In npreg.rbandwidth(txdat = txdat, tydat = tydat, bws = bws, ...) :
NAs introduced by coercion*
The npreg R documentation indicates the first argument should be BW specificaion. I tried setting bws=1
model.np = npreg(bws=1,logwage~age,regtype="ll",
+ bwmethod="cv.aic",gradients="TRUE", data=cps71)
which gives the following error
*Error in toFrame(xdat) :
xdat must be a data frame, matrix, vector, or factor*
First time working with density estimation in R. Please suggest how to resolve these errors.
I'm trying to use cor.ci to obtain polychoric correlations with significance tests, but it keeps giving me an error message. Here is the code:
install.packages("Hmisc")
library(Hmisc)
mydata <- spss.get("S-IAT for R.sav", use.value.labels=TRUE)
install.packages('psych')
library(psych)
poly.example <- cor.ci(mydata(nvar = 10,n = 100)$items,n.iter = 10,poly = TRUE)
poly.example
print(corr.test(poly.example$rho), short=FALSE)
Here is the error message it gives:
> library(psych)
> poly.example <- cor.ci(mydata(nvar = 10,n = 100)$items,n.iter = 10,poly = TRUE)
Error in cor.ci(mydata(nvar = 10, n = 100)$items, n.iter = 10, poly = TRUE) :
could not find function "mydata"
> poly.example
Error: object 'poly.example' not found
> print(corr.test(poly.example$rho), short=FALSE)
Error in is.data.frame(x) : object 'poly.example' not found
How can I make it recognize mydata and/or select certain variables from this dataset for the analysis? I got the above code from here:
Polychoric correlation matrix with significance in R
Thanks!
You have several problems.
1) As previously commented upon, you are treating mydata as a function, but you need to treat it as a data.frame. Thus the call should be
poly.example <- cor.ci(mydata,n.iter = 10,poly = TRUE)
If you are trying to just get the first 100 cases and the first 10 variables, then
poly.example <- cor.ci(mydata[1:10,1:100],n.iter = 10,poly = TRUE)
2) Then, you do not want to run corr.test on the resulting correlation matrix. corr.test should be run on the data.
print(corr.test(mydata[1:10,1:100],short=FALSE)
Note that corr.test is testing the Pearson correlation.