我的问题与这个问题密切相关,但是后续问题,而不是重复问题。postion_dodge()我的绘图问题与使用with有关geom_line()。
数据:
Behaviour Repeatability UCI LCI Age stage
Activity 0.1890000 0.2470000 0.1600000 PE A
Activity 0.5500000 0.7100000 0.3900000 PW B
Activity 0.5100000 0.6300000 0.4000000 A D
Activity 0.4100000 NA NA A2 D #NAs are real & important
Activity 0.4229638 0.4561744 0.3854906 A1 D
Activity 0.4660000 0.5910000 0.2320000 PW2 B
Activity 0.1812492 0.2111999 0.1522250 CY C
Aggression 0.2620000 0.3030000 0.1960000 PE A
Aggression 0.3700000 0.3800000 0.3600000 PW B
Aggression 0.4400000 0.5600000 0.3300000 A D
Aggression 0.3740000 …Run Code Online (Sandbox Code Playgroud) 我在编写的脚本中遇到错误,只有在我dplyr运行时才会发生.当我找到一个dplyr我想要使用的函数时,我第一次遇到它,之后我安装并运行了包.以下是我的错误示例:
首先,我从excel中读取一个表,其中包含我将用作索引的列值:
library(readxl)
examplelist <- read_excel("example.xlsx")
Run Code Online (Sandbox Code Playgroud)
该文件的内容是:
1 2 3 4
1 1 4 1
2 3 2 1
4 4 1 4
Run Code Online (Sandbox Code Playgroud)
然后我构建了一个数据框:
testdf = data.frame(1:12, 13:24, 25:36, 37:48)
Run Code Online (Sandbox Code Playgroud)
然后我有一个循环调用一个使用examplelistas作为索引值的函数.
testfun <- function(df, a, b, c, d){
value1 <- df[[a]]
value2 <- df[[b]]
value3 <- df[[c]]
value4 <- df[[d]]
}
for (i in 1:nrow(examplelist)){
testfun(testdf, examplelist[i, 1], examplelist[i, 2],
examplelist[i, 3], examplelist[i, 4])
}
Run Code Online (Sandbox Code Playgroud)
当我没有运行这个脚本时dplyr,一切都很好,但是dplyr它给了我错误:
Error in .subset2(x, i, …Run Code Online (Sandbox Code Playgroud) 我想使用优秀的rugarch包在两个不同的时间序列上估计 EGARCH 模型,但求解器无法收敛。我不想使用“混合”求解器选项,因为这会在循环通过“gosolnp”求解器时引入随机性。我的两个问题是:(1)我的数据是否有一些奇怪的东西导致收敛失败,(2)如果没有,有没有办法修改ugarchfit()函数,以便它“更努力地”找到解决方案?以下是我正在使用的数据和代码。
library("rugarch")
ABC <- c(-0.003311,-0.009967,-0.010067,-0.023729,0.006944,-0.010345,0.02439,-0.006803,-0.017123,-0.003484,0.017483,0.054983,0.032573,-0.018927,-0.006817,-0.019608,-0.003333,0.006689,0.006645,-0.009901,0,0.01,0.006601,0,0.009836,0.022727,-0.003175,-0.009554,0.022508,-0.003145,0.006309,-0.021944,0.012821,-0.015823,-0.028939,-0.009934,0.020067,0.045902,-0.012539,-0.003175,0.003185,0.012698,-0.003135,0.009434,-0.003115,-0.00625,0.003145,0.003135,-0.025,0.00641,0.012739,-0.003145,0.009464,-0.009375,0.009464,0,-0.0125,0,0.003165,-0.009464,0.006369,-0.028481,0.035831,-0.003145,0.009464,-0.00625,0.003145,-0.00627,-0.009464,-0.012739,0,-0.006452,0.016234,-0.003195,0.012821,0,0.003544,-0.003185,-0.00639,-0.022508,0.009868,0.006515,-0.003236,0,-0.012987,0.013158,-0.003247,-0.013029,0.0033,0,-0.016447,-0.006689,-0.003367,0.003378,0.013468,0.063123,0.0125,0.006173,-0.006135,-0.033951,-0.003195,-0.003205,0.022508,0.025157,0,-0.006135,-0.009259,-0.018692,0.009524,0.006289,-0.003125,0.015674,0.003086,0.003077,-0.009202,0,0.003096,-0.006173,-0.006211,0,-0.009375,-0.006309,-0.006349,0.00639,-0.003175,0,0.003185,-0.009524,0.009615,-0.003175,-0.009554,0.003215,-0.003205,0,0,0.003215,-0.009615,0.006472,-0.003215,0.000387,0.003257,-0.003247,0.006515,-0.003236,0.012987,0.022436,-0.003135,-0.006289,-0.003165,0.009524,0.044025,0.006024,0.005988,-0.005952,0,-0.017964,-0.003049,0,-0.006116,-0.009231,-0.018634,0.009494,-0.00627,-0.003155,0.009494,0.015674,0.021605,0,0.003021,-0.003012,0,-0.003021,-0.006061,-0.003049,-0.006116,-0.003077,0.003086,-0.006154,0.009288,-0.003067,-0.006154,0,0,-0.01548,0.012579,0.009317,-0.003077,-0.003086,0.006192,-0.006154,0,-0.012384,-0.00627,-0.006309,0.003175,-0.018987,0.016129,-0.009524,0.009615,0.003175,0.018987,-0.006211,0.025,-0.005732,-0.009288,0.00625,-0.006211,-0.009375,0.012618,-0.012461,0.009464,-0.00625,0.003145,-0.003135,0,0.003145,-0.003135,0,-0.006289,0.009494,-0.003135,0.009434,-0.006231,-0.015674,-0.009554,-0.025723,0.0033,-0.003289,-0.006601,0.006645,-0.013201,-0.006689,0.013468,-0.003322,-0.003333,0.006689,0.013289,-0.019672,0.006689,-0.006645,-0.003344,0.006711,0.036667,0.006431,0,-0.00639,0.009646,0.015924,0.003135,0.03125,0.012121,-0.005988,0.021084)
DEF <- c(0.004876,0.029923,-0.072242,-0.015235,-0.011603,0.015652,-0.021832,-0.015755,-0.008448,-0.038565,0.035914,-0.052679,0.005703,0.02741,-0.028059,0.004733,-0.00895,-0.035646,0.176934,-0.023869,-0.039468,-0.016079,0.00227,-0.015851,-0.02439,-0.021226,0.001928,-0.025493,0.027641,0.036023,0.02828,0.001803,-0.011251,0.015476,-0.035858,-0.003719,-0.0042,0.009372,-0.019499,0.023201,0.018047,0.005,0.037087,0.012647,-0.03273,0.036509,0.016323,0.040152,-0.001219,-0.002441,0.039967,0.023137,0.006899,0.007613,-0.007933,-0.026276,-0.003911,0.006677,0.023875,-0.014144,-0.002714,-0.031104,0.027689,0.003124,0.005839,-0.020898,0.030435,0.034906,0.036694,0.004648,-0.017438,-0.034408,-0.006752,0.010196,0.043738,-0.053725,0.008327,-0.035285,0.002724,-0.006209,-0.052714,-0.006595,0.025726,-0.024272,-0.011194,0.005451,-0.004587,0.002514,0.035102,-0.008478,0.052117,0.010836,0.009188,-0.016692,0.033179,-0.025766,0.013415,-0.00643,0.059764,0.002155,0.005376,-0.001069,-0.00571,0,0.005025,-0.001786,0.030411,0.003125,0.010038,-0.014051,-0.025721,-0.018195,0.005451,0.011926,-0.005714,0.002874,0.022206,0.018921,-0.016162,0.013632,-0.048276,-0.018841,0.038405,0.043385,0.000341,0.001363,-0.006805,0.030832,-0.000332,0.016955,0.019941,-0.019551,-0.033998,0.016582,0,0.008655,-0.00099,0.008259,-0.017038,0.007,-0.011917,0.01206,0.005958,0.009543,0.088983,-0.027237,0,-0.004615,0.007728,0.003681,-0.012836,0.017028,0,0.005784,-0.006659,-0.001828,0.000611,-0.012508,0.022552,-0.01148,0.008863,0.003332,0.003925,0.005714,-0.007775,-0.009946,-0.007915,-0.013194,-0.000622,0.015557,0.026961,-0.002387,-0.009569,-0.020229,0.00678,-0.015611,0.001866,0.007759,-0.020942,0.003146,0.017874,0.029883,0.014358,-0.009142,-0.004167,0.002926,0.003287,-0.010125,-0.000903,-0.003312,-0.010876,0.006109,-0.006679,-0.005807,0.006148,0.001528,-0.00244,0.017431,-0.011422,-0.00304,0.021653,-0.017612,-0.005773,-0.018643,0.000934,-0.009023,-0.000314,-0.009736,-0.001269,-0.005081,-0.019151,0.020827,0.000956,-0.018153,-0.013947,0.008224,-0.014356,-0.012248,0.009048,-0.003985,-0.012671,-0.008105,0.011236,-0.017508,0.019877,0.014113,-0.003976,-0.018629,0.002373,-0.002705,-0.014242,-0.02924,-0.00567,0.002851,0.000711,0.01598,0.019224,0.00823,-0.009524,0.015797,-0.025693,0.001388,-0.014553,0.014065,-0.003467,-0.008699,-0.004914,0.00388,-0.002811,-0.003524,-0.004597,-0.004263)
ugarchfit(spec = ugarchspec(mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), variance.model = list(model = "eGARCH", garchOrder = c(1, 1))), data = ABC)
ugarchfit(spec = ugarchspec(mean.model = list(armaOrder = c(0, 0), include.mean = TRUE), variance.model = list(model = "eGARCH", garchOrder = c(1, 1))), data = DEF)
Run Code Online (Sandbox Code Playgroud) 我试图绘制4个不同回归模型的置信区间的点须图.
数据可在此处获得.
#first importing data
Q1<-read.table("~/Q1.txt", header=T)
# Optionally, read in data directly from figshare.
# Q1 <- read.table("https://ndownloader.figshare.com/files/13283882?private_link=ace5b44bc12394a7c46d", header=TRUE)
library(dplyr)
#splitting into female and male
female<-Q1 %>%
filter(sex=="F")
male<-Q1 %>%
filter(sex=="M")
library(lme4)
#Female models
#poisson regression
ab_f_LBS= lmer(LBS ~ ft + grid + (1|byear), data = subset(female))
#negative binomial regression
ab_f_surv= glmer.nb(age ~ ft + grid + (1|byear), data = subset(female), control=glmerControl(tol=1e-6,optimizer="bobyqa",optCtrl=list(maxfun=1e19)))
#Male models
#poisson regression
ab_m_LBS= lmer(LBS ~ ft + grid + (1|byear), data = subset(male)) …Run Code Online (Sandbox Code Playgroud) 我试图将我的传奇元素的某些部分改为斜体。我已经能够使用此OP中的答案来获取要斜体化的子集,但这会在文本显示方式方面造成问题。
以下是数据(略作修改):
Behaviour Repeatability Age stage
Activity 0.1 PE A
Activity 0.5 PE2 A
Activity 0.5 PW B
Activity 0.5 A C
Activity 0.4 A2 C
Activity 0.4 PW1 B
Aggression 0.2 PE A
Aggression 0.3 PE2 A
Aggression 0.3 PW B
Aggression 0.4 A C
Aggression 0.3 A2 C
Aggression 0.0 PW1 B
Run Code Online (Sandbox Code Playgroud)
当我尝试斜体是基于这个括号中的图例文本的解决方案,该地块被搞砸:
下面是我用来获取该图的代码(减去不需要的格式):
ggplot(df, aes(x = stage, y = Repeatability, shape = …Run Code Online (Sandbox Code Playgroud) 我有df1:
Account Score1 Score2 Score3 Score4 Score5 Score6 Random Random2
23 F30 G1 G5 H10 J18 NULL 3 4
42 NULL NULL NULL NULL NULL NULL 5 6
56 D10 D11 NULL NULL NULL NULL 6 2
59 X14 D3 F4 A11 A12 A13 8 2
41 D11 D12 NULL NULL NULL NULL 7 7
45 C3 C10 R4 T5 NULL NULL 1 1
30 C4 NULL NULL NULL NULL NULL 1 5
33 D2 D3 NULL NULL …Run Code Online (Sandbox Code Playgroud) 当我使用 重塑数据时遇到问题pivot_wider()。
我的数据如下所示:
df <- data.frame(area = c( "Area1","Area1","Area1","Area2","Area2","Area2","Area3","Area3","Area3"),
species = c("species1","species2","species3","species1","species2","species3","species1","species2","species3"),
season= c("Season1","Season1","Season1","Season2","Season2","Season2","Season3","Season3","Season3"),
value= c(2,3,5,7,9,2,6,9,3))
Run Code Online (Sandbox Code Playgroud)
我可以将数据框更改为宽格式,如下所示。
df_wide <- df %>%
mutate(row = row_number()) %>%
pivot_wider(id_cols= c(row,species),
,names_from = "season",
values_from = "value") %>%
select(-row)
Run Code Online (Sandbox Code Playgroud)
这是输出的数字。
我的问题是它引入了 NA,因为pivot_wider()为每个值创建一个新列。
如果你帮助我,我会很棒...
我在安装时遇到了问题Flextable,这似乎是由于gdtools包装所致。看起来gdtools依赖于.dylib文件吗?有任何想法吗?
> install.packages("flextable")
There is a binary version available but the source version is later:
binary source needs_compilation
flextable 0.4.0 0.4.2 FALSE
installing the source package ‘flextable’
trying URL 'https://cran.rstudio.com/src/contrib/flextable_0.4.2.tar.gz'
Content type 'application/x-gzip' length 135276 bytes (132 KB)
==================================================
downloaded 132 KB
Warning in strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
unknown timezone 'default/Australia/Melbourne'
* installing *source* package ‘flextable’ ...
** package ‘flextable’ successfully unpacked and MD5 sums checked
** R
** …Run Code Online (Sandbox Code Playgroud) 我是R和统计学的新手.请在这个问题上忍受我.
我想从1:8找到数字的四分位数和IQR.我计算得出Q2 =(4 + 5)/ 2 = 4.5 Q1 =(2 + 3)/ 2 = 2.5和Q3 =(6 + 7)/ 2 = 6.5
IQR = 6.5-2.5 = 4
但是R给出了一些不同的值.我无法理解我错在哪里.
> quantile(1:8)
0% 25% 50% 75% 100%
1.00 2.75 4.50 6.25 8.00
> IQR(1:8)
[1] 3.5
Run Code Online (Sandbox Code Playgroud)
如果有人能够指出我的错误,那就太好了.
我正在尝试用这些断点处的指定断点和标签制作等高线图.我尝试添加使用无论是在休息的标签direct.label或者geom_dl,但没有成功.
dat <- melt(volcano)
brks <- c(100, 120, 140, 160)
g <- ggplot(dat, aes(x = Var1, y = Var2, z = value)) +
geom_contour(colour = 'black', breaks = brks)
g
Run Code Online (Sandbox Code Playgroud)
该部分工作正常,但当我尝试添加标签时:
direct.label(g, list("bottom.pieces", colour='black'))
Run Code Online (Sandbox Code Playgroud)
我收到错误: Need colour or fill aesthetic to infer default direct labels.
而且,当我尝试:
g + geom_dl(aes(label = brks), method = 'bottom.pieces')
Run Code Online (Sandbox Code Playgroud)
我明白了: Error: Aesthetics must be either length 1 or the same as the data (5307): label, x, y, z
有什么建议?
r ×10
ggplot2 ×4
dplyr ×2
broom ×1
convergence ×1
dataframe ×1
flextable ×1
grepl ×1
legend ×1
lme4 ×1
statistics ×1
tidyverse ×1
typography ×1
volatility ×1