I am running the following code in R:
pkstwo <- function(x, tol = 1e-06) {
if (is.numeric(x))
x <- as.double(x)
else stop("argument 'x' must be numeric")
p <- rep(0, length(x))
p[is.na(x)] <- NA
IND <- which(!is.na(x) & (x > 0))
if (length(IND))
p[IND] <- .C(stats:::C_pkstwo, length(x[IND]), p = x[IND],
as.double(tol), PACKAGE = "stats")$p
p
}
But when I call pkstwo(0.1) I get the following error:
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'C_pkstwo' not found
Could anyone please help me fix this?
The C code is now called C_pKS2, and is used in the private function pkstwo() within ks.test()
Run ks.test with no parentheses to see its R code.
ks.test
Run stats:::C_pKS2 for some more info.
stats:::C_pKS2
Related
I have tried looking up an answer to this and have been unable to find a solution to my particular code. I am trying to make a summary table including medians and results of a Mann Whitney U. However I get the error:
Error in unique.default(x, nmax = nmax) : unique() applies only to vectors
The code I am using is below - this is my first time posting so if I have done something wrong with asking my question I apologise.
Thank you for your help.
library("table1")
library("MatchIt")
cog$Type_use <- factor(cog$Type_use, levels=c(0,1,2),
labels = c("Controls", "Patients", "P-value"))
rndr <- function(x, name, ...) {
if (length(x) == 0) {
y <- cog[[name]]
s <- rep("", length(render.default(x=y, name=name, ...)))
if (is.numeric(y)) {
p <- wilcox.test(y ~ cog$Type_use)$p.value
} else {
p <- wilcox.test(table(y ~ cog$Type_use)$p.value)
}
s[2] <- sub(">", "<", format.pval(p, digits=3, eps=0.001))
s
}
render.default(x=x, name=name, ...)
}
rndr.strat <- function(label, n, ...) {
ifelse(n==0, label, render.strat.default(label, n, ...))
}
table1(~ cog$Sex_1_USE + cog$Age.at.assessment_two_use | cog$Type_use,
data=cog, droplevels=F, render=rndr, render.strat=rndr.strat, overall=F)
Is it possible to use an old version of the stats package in R?
The function stats:::regularize.values causes me warnings and errors in the last R version (any version >3.5).
I have no possibility to get back to an old R version.
I have no clue where the regularize.values function is called in my code since I use several functions, some of them from different R packages.
I've tried to change the call to regularize.values in my code by doing
assignInNamespace("regularize.values", regularize.values.old.version, ns = "stats")
But I got the error:
Error in assignInNamespace("regularize.values", regularize.values.OV, :
locked binding of ‘regularize.values’ cannot be changed
Thanks in advance for your suggestions!
You can try this:
assignInNamespace("regularize.values", regularize.values.OV,
ns="stats", envir = as.environment("package:stats"))
However, it will only work if the error is not thrown by a package that depends on the already-loaded stats
Therefore, a working solution should be:
assignInNamespace("regularize.values", function(x, y, ties) {
x <- xy.coords(x, y)
y <- x$y
x <- x$x
if(any(na <- is.na(x) | is.na(y))) {
ok <- !na
x <- x[ok]
y <- y[ok]
}
nx <- length(x)
if (!identical(ties, "ordered")) {
o <- order(x)
x <- x[o]
y <- y[o]
if (length(ux <- unique(x)) < nx) {
# if (missing(ties))
# warning("collapsing to unique 'x' values")
y <- as.vector(tapply(y,match(x,x),ties))
x <- ux
stopifnot(length(y) == length(x))
}
}
list(x=x, y=y)
}, ns="stats", envir = as.environment("package:stats"))
I have a function that generates many scatterplots. It's giving me an error that I haven't been able to find in this same context in researching the problem.
ExploreBiNumeric <- function(df){
num_vars <- which(sapply(df, is.numeric))
X <- df[names(num_vars)]
for (i in 1:length(names(num_vars))){
for(j in 1:length(names(num_vars))){
if(i==j){}
else{
Y <- as.data.frame(cbind(X[i],X[j]))
A <- colnames(Y[1])
B <- colnames(Y[2])
print(A)
print(B)
print(cor(Y))
colnames(Y) <- c("i","j")
print(ggplot(Y,aes(i,j)) + geom_point() + xlab(A) + ylab(B))
}
}
}
print(cor(X))
print(corrplot(cor(X)))
print(chart.Correlation(X,histogram=TRUE,pch=19))
print(plot(X))
}
Error in grid.Call.graphics(C_setviewport, vp, TRUE) : non-finite
location and/or size for viewport
I thought it could be caused by the labels. I tried passing them directly as xlab(colnames(X[i])) and ylab(colnames(X[j])) and that had the same result.
I also read that I should update the package ggsn. I did that.
I am following code from a textbook called "Data Mining with R" and I've hit a stumbling block when trying to run the sample code myself.
Here is the code so far:
library(quantmod)
getSymbols("^GSPC")
colnames(GSPC) <- c("Open", "High", "Low", "Close", "Volume", "AdjClose")
T.ind <- function(quotes, tgt.margin=.025, n.days=10) {
v <- apply(HLC(quotes), 1, mean)
r <- matrix(NA, ncol=n.days, nrow=NROW(quotes))
for(x in 1:n.days) r[,x] <- Next(Delt(v,k=x), x)
x <- apply(r,1, function(x) sum(x[x>tgt.margin|x < -tgt.margin]))
if (is.xts(quotes))
xts(x, time(quotes))
else x
}
candleChart(last(GSPC, "1 year"), theme="white", TA=NULL)
avgPrice <- function(p) apply(HLC(p), 1, mean)
addAvgPrice <- newTA(FUN=avgPrice, col=1, legend="AvgPrice")
addT.ind <- newTA(FUN=T.ind, col="red", legend="tgtRet")
addAvgPrice(on=1)
addT.ind()
But for the last 2 lines I get the error message:
> addAvgPrice(on=1)
Error in addAvgPrice(on = 1) : could not find function "get.current.chob"
> addT.ind()
Error in addT.ind() : could not find function "get.current.chob"
What is additionally odd is that I ran the code on 1 machine & have no issues but on my main PC I keep getting the error.
I tried googling this error but could not find any real solutions or explanations.
Any insight or assistance would be greatly appreciated.
> get.current.chob
Error: object 'get.current.chob' not found
get.current.chob is in quantmod, so...
> quantmod:::get.current.chob
function ()
{
first.chob <- which(sapply(sys.frames(), function(x) exists("chob",
envir = x)))[1]
if (!is.na(first.chob)) {
lchob <- get("chob", envir = first.chob)
}
else {
gchob <- get.chob()
if (dev.cur() == 1 || length(gchob) < dev.cur())
stop("improperly set or missing graphics device")
current.chob <- which(sapply(gchob, function(x) {
ifelse(class(x) == "chob" && x#device == as.numeric(dev.cur()),
TRUE, FALSE)
}))
if (identical(current.chob, integer(0)))
stop("no current plot")
lchob <- gchob[[current.chob]]
}
return(lchob)
}
<environment: namespace:quantmod>
I am trying to solve an integral in R. However, I am getting an error when I am trying to solve for that integral.
The equation that I am trying to solve is as follows:
$$ C_m = \frac{{abs{x}}e^{2x}}{\pi^{1/2}}\int_0^t t^{-3/2}e^{-x^2/t-t}dt $$
The code that I am using is as follows:
a <- seq(from=-10, by=0.5,length=100)
## Create a function to compute integration
Cfun <- function(XX, upper){
integrand <- function(x)x^(-1.5)*exp((-XX^2/x)-x)
integrated <- integrate(integrand, lower=0, upper=upper)$value
(final <- abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) }
b<- sapply(a, Cfun, upper=1)
The error that I am getting is as follows:
Error in integrate(integrand, lower = 0, upper = upper) :
the integral is probably divergent
Does this mean I cannot solve the integral ?
Any possible ways to fix this problem will be highly appreciated.
Thanks.
You could wrap the call to Cfun in a try statement
# note using `lapply` so errors don't coerce the result to character
b <- lapply(a, function(x,...) try(Cfun(x, ...), silent = TRUE), upper = 1)
If you wanted to replace the errors with NA values and print a warning that the integration threw an error
Cfun <- function(XX, upper){
integrand <- function(x)x^(-1.5)*exp((-XX^2/x)-x)
int <- try(integrate(integrand, lower=0, upper=upper), silent = TRUE)
if(inherits(int ,'try-error')){
warning(as.vector(int))
integrated <- NA_real_
} else {
integrated <- int$value
}
(final <- abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) }
Edit (facepalm moment)
Your error arises because your function is not defined when t=0 (you divide by t within the integrand).
Cfun <- function(XX, upper){
integrand <- function(x)x^(-1.5)*exp((-XX^2/x)-x)
# deal with xx=0
if(isTRUE(all.equal(XX, 0)){
warning('The integrand is not defined at XX = 0')
return(NA_real_)
}
# deal with other integration errors
int <- try(integrate(integrand, lower=0, upper=upper), silent = TRUE)
if(inherits(int ,'try-error')){
warning(as.vector(int))
integrated <- NA_real_
} else {
integrated <- int$value
}
(final <- abs(XX)*pi^(-0.5)*exp(2*XX)*integrated) }