Can't find the function after require(thePackage) - r

I am trying to get the plot.cuminc() function in the cmprsk package.
I installed the package and used require(cmprsk) as well as library(cmprsk) but R still can't find the function.
p.s. I used ?plot.cuminc() and find the help document
Dose anyone know what's wrong with the R , or my code?

If you run plot on a cuminc object, it will automatically run plot.cuminc. Try running this example code:
set.seed(2)
ss <- rexp(100)
gg <- factor(sample(1:3,100,replace=TRUE),1:3,c('a','b','c'))
cc <- sample(0:2,100,replace=TRUE)
strt <- sample(1:2,100,replace=TRUE)
print(xx <- cuminc(ss,cc,gg,strt))
plot(xx,lty=1,color=1:6)
If you really want to look at the function, or run it directly for some reason, you can use cmprsk:::plot.cuminc.

Related

I'm having trouble getting R to calculate the VIF

The following is the input I've put in R. I'm just wondering do i need to update R or have i done something wrong?
attach(crimedat)
crimedata <- crimedat[,-2]
lm1 <- lm(Expenditure+YouthUnemploy+MatureUnemploy+Wage~CrimeRate)
vif(lm1)
Error in vif(lm1) : could not find function "vif"

Error "t.haven_labelled()` not supported" when trying to substitute NA with mice package

Total R noob here, trying to figure out how to implement mice package to account for NAs in my dataset.
This is my code so far (i left out the unimportant stuff like trimming the data set down to relevant variables, recoding etc.)
install.packages("haven")
install.packages("survey")
library(haven)
library(data.table)
library(survey)
library(car)
dat <- read_dta("ZA5270_v2-0-0.dta")
dat_wght <- svydesign(ids= ~1, data=dat, weights =~wghtpew)
install.packages("mice")
library(mice)
dat_wght[["variables"]]$sex = as.factor(dat_wght[["variables"]]$sex)
dat_imp <- mice(dat_wght[["variables"]], m=5, maxit=10)
The error message I get is:
iter imp variable
1 1 px03Error in `t()`:
! `t.haven_labelled()` not supported.
I already did some research and apparantly it has to do with label values since haven package causes lots of weird problems. I already tried to remove all label values with sapply(dat_wght[["variables"]], haven::zap_labels)but the error still occurs (same when I try it with remove_val_labels()) Does anyone know how to solve this problem?
I'm really grateful for every single piece of advice :) Thanks in advance!

Attempting to save intermediate states when running rmh yields error

I am trying to simulate a multitype point process, saving the intermediate states every 1000 steps in rmhcontrol. However, I can't simulate whenever I specify nsave. As an example, whenever I run the code block below, I get the error:
Error in factor(Cmprop, levels = Ctypes) : object 'Cmprop' not found
The code is:
library(spatstat)
library(optimbase)
num_marks <- length(unique(marks(amacrine)))
iradii <- .1*ones(nrow=num_marks,ncol=num_marks)
MSH1 <- MultiStraussHard(iradii=iradii)
x <- ppm(amacrine, trend =~polynom(x,y,3), interaction=MSH1)
control <- rmhcontrol(nsave=1e3)
rmh(x,control=control)
Thanks for the help!
This is a bug in spatstat versions 1.62-1 and 1.62-2.
It has already been fixed in the current development version 1.62-2.006 which you can download from the GitHub repository for spatstat. The next public release on CRAN will be at the end of January 2020.
Please note: the code in the original question generates an error because ones has formal arguments nx, ny rather than nrow, ncol. The following code tests the bug:
library(spatstat)
nm <- length(levels(marks(amacrine)))
ir <- matrix(0.1, nm, nm)
MSH1 <- MultiStraussHard(iradii=ir)
fit <- ppm(amacrine ~ polynom(x,y,3), MSH1)
rmh(fit, nsave=1e3, verbose=FALSE)

Is there a way to make an R function return its internal variable?

I am new to R. I am currently trying to implement a regression based on instrumental variable from the sysid R-package. I chose this package since it can predict my instrument.
I found a suitable method ("iv" is the function here) to solve my problem. But the R function is not returning the "Predicted Instrument" as one of its return argument. I am very much interested in that predicted variable. Is there any way to get this variable as an argument?
I already tried to create a clone of this function but it has many dependent function from sysid package so it failed. I also tried to use the "source" command to link this modified function in my R code but rest of the libraries are delinked from my current script. Please provide me any solution to get the predicted instrument. The source code is available below:
https://rdrr.io/cran/sysid/src/R/iv.R.
iv4 <- function(z,order=c(0,1,0)){
na <- order[1]; nb <- order[2]
# Steps 1-2
mod_iv <- iv(z,order)
# Step 3
w <- resid(mod_iv)
mod_ar <- ar(w,aic = F,order.max =na+nb)
Lhat <- signal::Arma(b=c(1,-mod_ar$ar),a=1)
# Step 4
x2 <- matrix(sim(mod_iv$sys,inputData(z)))
ivcompute(z,x2,order,Lhat)
}
I want predicted instrument- Lhat to be returned. I also welcome suggestion for using any other package or regression method which can do the same(predict instrument).

Weird behaviour of the car::boxCox() function when wrap into a homemade function

I'm trying to wrap the car::boxCox function into a homemade function so I can mapply it to a list of datasets. I'm using the boxCox function from the car package and not the MASS package because I want to use the family="yjPower". My problem is weird and it's either something fondamental I don't understand or some kind of bug. Here is a reproducible example:
library(car)
le.mod <- function(val.gold,val.bad){
donn <- data.frame(val.gold,val.bad)
res.lm <- lm(val.gold ~ val.bad, data=donn)
bcres <- boxCox(res.lm, family="yjPower", plotit=F)
lambda <- bcres$x[which.max(bcres$y)]
donn$val.bad.t <- donn$val.bad^lambda
res.lm <- lm(val.gold ~ val.bad.t, data=donn)
list(res.lm=res.lm, lambda = lambda)
}
xx <- runif(1000,1,100)
xxt1 <- xx^0.6 + runif(1000,1,10)
yy <- 2*xx + 10 + rnorm(1000,0,2)
le.mod(yy,xxt1)
This gives me the error message:
## Error in is.data.frame(data) : object 'donn' not found
I pin-pointed the problem to the line:
bcres <- boxCox(res.lm, family="yjPower", plotit=F)
boxCox is suppose to be able to take a lm class object, it just doesn't find the associated data that were created 2 lines before.
It works well outside of the function le.mod(). It's probably a problem related to environment management, the boxCox fonction looking for "donn" in the global environment but not finding it and for a reason I ignore not looking for it in the function specific environment.
Anybody have an idea to fix this or explain to me what I don't understand here? I've been turning my head over this problem for days and I can't get it working.
Thanks
I've found the answer (!), however I can't understand the reason of the behaviour so if somebody have an explanation, don't hesitate to post it.
The solution by adding y=TRUE in the second line of the function:
res.lm <- lm(val.gold ~ val.bad, data=donn,y=TRUE)
For some reasons, this allows it to get throught.

Resources