小编Sim*_*lon的帖子

如何使geom_boxplot异常值与抖动的geom_points"对齐"?

如何使geom_boxplot异常值与抖动的geom_points完美叠加?

例如,我希望geom_boxplot中的异常值在抖动后显示为geom_point实际点上的"十字线"?

library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg)) + 
  geom_boxplot(outlier.shape=10, outlier.size=8)  +
  geom_point(aes(factor(cyl), mpg, color=mpg),  position="jitter", size=4)
p
Run Code Online (Sandbox Code Playgroud)

情节

任何帮助将不胜感激.

graphics r ggplot2

5
推荐指数
2
解决办法
1377
查看次数

如何识别数组中最大值的位置?

我的阵列是

x <- array(1:24, dim=c(3,4,3))
Run Code Online (Sandbox Code Playgroud)

我的任务1是根据前两个维度找到最大值

x.max <- apply(x,c(1,2), function(x) ifelse(all(is.na(x)), NA, max(x, na.rm = TRUE)))    
Run Code Online (Sandbox Code Playgroud)

在有NA数据的情况下,我的任务2是在第三维上找到最大值位置.我试过了

x.max.position = apply(x, c(1,2),which.max(x))
Run Code Online (Sandbox Code Playgroud)

但这只能给我第二个维度的立场.

谁能帮我?

arrays r max

5
推荐指数
1
解决办法
3387
查看次数

将矢量分成R中不等长度的三个矢量

来自相对n00b的问题:我想将一个向量分成三个不同长度的向量,其中的值随机分配给每个向量.例如,我想将长度为12的矢量分成长度为2,3和7的向量

我可以使用这个获得三个相等大小的向量:

test<-1:12
split(test,sample(1:3))
Run Code Online (Sandbox Code Playgroud)

关于如何将测试分成2,3和7的矢量而不是3个长度为4的矢量的建议?

r

5
推荐指数
2
解决办法
8760
查看次数

如何修剪和替换字符串

string<-c("       this is a string  ")
Run Code Online (Sandbox Code Playgroud)

是否可以在弦的两侧(或根据需要只是一侧)修剪掉白色空间,并用R中的所需字符替换它?字符串两侧的白色空格数不同,必须在更换时保留.

"~~~~~~~this is a string~~"
Run Code Online (Sandbox Code Playgroud)

regex string r

5
推荐指数
3
解决办法
164
查看次数

剪切线以绘制区域并在绘制区域外显示文本

我想限制绘图的可见y范围。为了保留超出此范围的值,我需要设置oob超出范围),rescale_none并且效果很好。

但是,我还要在情节外的空白处添加一些文本。为此,我需要关闭剪辑。这样的结果是,超出界限的值在边距的绘图区域之外绘制。

无论如何,是否在页边距中绘制文本裁剪值以绘制区域?

#  Data
set.seed(1)
df <- data.frame( x=1:100,y=rnorm(100,mean=1,sd=1) )
# Basic plot

library(ggplot2)
library(scales)
library(grid)

g <- ggplot(df)+
geom_line(aes(x,y))


#  Values exceeding scale limits are dropped
g1 <- g + scale_y_continuous( limits = c(0,2) )
Run Code Online (Sandbox Code Playgroud)

OOB值下降

#  This is what I want
g2 <- g + scale_y_continuous( limits = c(0,2) , oob = rescale_none )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

#  ...But, I would like to plot some text outside the plotting region
#  and need …
Run Code Online (Sandbox Code Playgroud)

graphics plot r ggplot2

5
推荐指数
1
解决办法
1114
查看次数

tryCatch - 命名空间?

我对R很新,我对正确用法感到困惑tryCatch.我的目标是对大型数据集进行预测.如果预测无法适应内存,我想通过拆分数据来规避问题.

现在,我的代码大致如下:

tryCatch({
  large_vector = predict(model, large_data_frame)
}, error = function(e) { # I ran out of memory
  for (i in seq(from = 1, to = dim(large_data_frame)[1], by = 1000)) {
    small_vector = predict(model, large_data_frame[i:(i+step-1), ])
    save(small_vector, tmpfile)
  }
  rm(large_data_frame) # free memory
  large_vector = NULL
  for (i in seq(from = 1, to = dim(large_data_frame)[1], by = 1000)) {
    load(tmpfile)
    unlink(tmpfile)
    large_vector = c(large_vector, small_vector)
  }
})
Run Code Online (Sandbox Code Playgroud)

关键是如果没有错误发生,large_vector则按预期填充我的预测.如果发生错误,large_vector似乎只存在于错误代码的命名空间中 - 这是有道理的,因为我将其声明为函数.出于同样的原因,我收到一条警告说large_data_frame无法删除.

不幸的是,这种行为不是我想要的.我想large_vector …

r try-catch assign

4
推荐指数
1
解决办法
892
查看次数

逻辑运算符 - 短(关系)与长(向量)形式

在R中使用短形式和长形式的逻辑运算符让我感到有些困惑.

如果我有以下值

A <- FALSE
B <- TRUE
X <- 3
Y <- 2
Run Code Online (Sandbox Code Playgroud)

我想评估NOT(A)OR NOT(B) X <Y

我期待FALSE给出参数

这是我发现要评估R它的表达式,因此它会FALSE像我期望的那样返回:

!A & X < Y || !B & X < Y
Run Code Online (Sandbox Code Playgroud)

我可以消除重复X < Y比较吗?

comparison r relational vectorization logical-operators

4
推荐指数
1
解决办法
348
查看次数

如何从R中的预测中提取预测?

我在具有多个位置的时间序列上运行VAR.假设loc1,loc2和loc3是时间序列数据的列名.

fitVAR = VAR(data,p=order,type = "both", ic = "AIC") 
pred = predict(fitVAR,n.ahead = L)
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过pred$fcst$loc1[,1]等等来获得预测.但是假设我想编写一个函数来执行此操作,它将位置名称作为输入变量(例如,LOC=c("loc1","loc2","loc3")).我该怎么做?

r predict

4
推荐指数
1
解决办法
2376
查看次数

求解R/Black-Scholes-Merton模型中的非线性方程组

我正在写我的硕士论文,我在R代码中遇到了这个问题.在数学上,我想用R-package"nleqslv"来解决这个非线性方程组:

fnewton <- function(x){

y <- numeric(2)

d1 = (log(x[1]/D1)+(R+x[2]^2/2)*T)/x[2]*sqrt(T)

d2 = d1-x[2]*sqrt(T)

y1 <- SO1 - (x[1]*pnorm(d1) - exp(-R*T)*D1*pnorm(d2))

y2 <- sigmaS*SO1 - pnorm(d1)*x[2]*x[1]

y}

xstart <- c(21623379, 0.526177094846878)

nleqslv(xstart, fnewton, control=list(btol=.01), method="Newton")
Run Code Online (Sandbox Code Playgroud)

我已经尝试了这个代码的几个版本,现在我收到错误:

error: error in pnorm(q, mean, sd, lower.tail, log.p): not numerical.

Pnorm分别是d1和d2的累积标准正态分布.我真的不知道,当我在Teterevas幻灯片上定位我的模型时我做错了什么(在5号幻灯片上是她的模型代码),谁的演示是googeling的第一个结果

https://www.google.de/search?q=moodys+KMV+in+R&rlz=1C1SVED_enDE401DE401&aq=f&oq=moodys+KMV+in+R&aqs=chrome.0.57.1​​3309j0&sourceid=chrome&ie=UTF-8#q=distance+到+默认+在+ R

与我一样,无论多么成功,她都会通过Black-Scholes-Merton方法计算违约距离风险度量.在这个模型中,权益的价值(通常由市值表示, - > SO1)可以写成欧洲看涨期权 - 我在上面的代码中标记为y2,但是,之前的等式设置为0!

其他变量是:

x [1] - >我想要得到的变量,总资产的价值

x [2] - >我想得出的变量,总资产的波动性

D1 - >债务账面价值(1998-2009)

R - >无风险利率

T - >设置为1(时间)

sigmaS - >估计(历史)股票波动率

谢谢!我很高兴,任何人都可以帮助我.卡罗

r nonlinear-optimization

4
推荐指数
1
解决办法
4941
查看次数

当从data.tables绘制直方图时,maxn不是此j列长度的精确倍数

我正在尝试从data.table分组的结果绘制直方图,但似乎无法在发生跟随错误之前生成多个图表:

错误[.data.table(DT ,,, hist(V2,break = 2),by = V1):maxn(3)不是此j列长度的精确倍数(2)

这个玩具示例应该有希望证明这个问题

require( data.table ) 
DT = data.table(c(1,1,2,2), c(1,2,3,4))

# This works
DT[,plot(V2,V2), by = V1] 

# This fails after the first plot
DT[,hist(V2, breaks = 2), by = V1] 
Run Code Online (Sandbox Code Playgroud)

关于我做错了什么的任何想法?

r data.table

4
推荐指数
1
解决办法
71
查看次数