小编AFH*_*AFH的帖子

在 Apple 芯片(Big Sur、Monterey、Ventura)上为 Rcpp 和其他工具配置编译器

我正在尝试Rcpp在我的 M1 Mac 上使用 R 中需要的软件包,但在购买这台计算机后我一直无法启动并运行它。我将其更新到蒙特利,希望这能解决一些安装问题,但事实并非如此。我尝试从此页面Rcpp运行检查,但出现以下错误:

\n
> Rcpp::sourceCpp("~/github/helloworld.cpp")\n
Run Code Online (Sandbox Code Playgroud)\n
ld: warning: directory not found for option \'-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0\'\nld: warning: directory not found for option \'-L/opt/R/arm64/gfortran/lib\'\nld: library not found for -lgfortran\nclang: error: linker command failed with exit code 1 (use -v to see invocation)\nmake: *** [sourceCpp_4.so] Error 1\nclang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include   -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppArmadillo/include" -I"/Users/afredston/github" -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c helloworld.cpp -o helloworld.o\nclang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined …
Run Code Online (Sandbox Code Playgroud)

macos r build r-package apple-silicon

20
推荐指数
2
解决办法
2万
查看次数

从GAM预测R中分组数据帧的值

我有一个不同年份不同纬度的年平均温度值的数据集。我想用它来预测在一年中可以找到给定温度的纬度;也就是说,“在1980年,在什么纬度下,年平均气温为20摄氏度?”

我需要为此使用特定年份的模型,因为纬度和温度之间的关系随时间而改变(尽管下面的示例数据中并未随机生成)。这将涉及:

  1. 将GAM拟合到按年份分组/拆分的数据集。
  2. 对于每个不同的GAM(即每年),用于predict.gam计算温度列表中每个元素的预测值。
  3. 重新组合这些数据,以得到一个数据框,其中的列分别代表yearnewdata_value (用于预测的温度值)和predicted_value(将其馈newdata_value入特定年份的GAM中的纬度)。

这是一个玩具数据集:

years <- seq(1968, 2018, 1)
lat <- seq(34.5, 44.5, 1)
dat <- expand.grid(years, lat)
names(dat) <- c("years","lat")
dat$temp <- runif(dim(dat)[1], 5, 20) # add random temperature data points 
newdata_values <- seq(2, 16, 2) # temperature values to use for prediction
Run Code Online (Sandbox Code Playgroud)

我已经试过各种purrrsplit-apply-combine解决方案,也没有想通什么了。有什么建议么?

r apply gam purrr

6
推荐指数
1
解决办法
107
查看次数

将功能应用于数据框中的分组行

我创建了一个函数,可以计算许多生物统计信息,例如物种范围边缘。这是该函数的简化版本:

range_stats <- function(rangedf, lat, lon, weighting, na.rm=T){
  cent_lat <- weighted.mean(x=rangedf[,lat], w=rangedf[,weighting], na.rm=T)
  cent_lon <- weighted.mean(x=rangedf[,lon], w=rangedf[,weighting], na.rm=T)
out <- data.frame(cent_lat, cent_lon)    
return(out)
} 
Run Code Online (Sandbox Code Playgroud)

我想将其应用于大型数据框,其中每一行都是一个物种的观察结果。这样,我希望函数按一组指定的列对行进行分组,然后为每个组计算这些统计信息。这是一个测试数据帧:

LATITUDE <- c(27.91977, 21.29066, 26.06340, 28.38918, 25.97517, 27.96313)
LONGITUDE <- c(-175.8617, -157.8645, -173.9593, -178.3571, -173.9679, -175.7837)
BIOMASS <- c(4.3540488, 0.2406332, 0.2406332, 2.1419699, 0.3451426, 1.0946017)
SPECIES <- c('Abudefduf abdominalis','Abudefduf abdominalis','Abudefduf abdominalis','Chaetodon lunulatus','Chaetodon lunulatus','Chaetodon lunulatus')
YEAR <- c('2005', '2005', '2014', '2009', '2009', '2015')
testdf <- data.table(LATITUDE, LONGITUDE, BIOMASS, SPECIES, YEAR)
Run Code Online (Sandbox Code Playgroud)

我想将此功能应用于物种和年份的每个唯一组合以计算汇总统计信息,即以下内容:

testresult <- testdf %>%
  group_by(SPECIES, YEAR) …
Run Code Online (Sandbox Code Playgroud)

r dplyr purrr

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

错误:“rjags”的包或命名空间加载失败

我有一台运行 OS Big Sur 的 M1 Macbook Pro,只是尝试安装rjagsJAGS. 我从https://sourceforge.net/projects/mcmc-jags/下载 JAGS没有问题,然后运行install_packages("rjags"),但是当我运行时library(rjags)出现此错误:

\n
Loading required package: coda\nError: package or namespace load failed for \xe2\x80\x98rjags\xe2\x80\x99:\n .onLoad failed in loadNamespace() for \'rjags\', details:\n  call: dyn.load(file, DLLpath = DLLpath, ...)\n  error: unable to load shared object \'/Users/afredston/Library/Caches/org.R-project.R/R/renv/cache/v5/R-4.1/aarch64-apple-darwin20/rjags/4-12/e8a71b3a154c8e68c152b4cfbebfdd97/rjags/libs/rjags.so\':\n  dlopen(/Users/afredston/Library/Caches/org.R-project.R/R/renv/cache/v5/R-4.1/aarch64-apple-darwin20/rjags/4-12/e8a71b3a154c8e68c152b4cfbebfdd97/rjags/libs/rjags.so, 10): Library not loaded: /opt/R/arm64/lib/libjags.4.dylib\n  Referenced from: /Users/afredston/Library/Caches/org.R-project.R/R/renv/cache/v5/R-4.1/aarch64-apple-darwin20/rjags/4-12/e8a71b3a154c8e68c152b4cfbebfdd97/rjags/libs/rjags.so\n  Reason: image not found\n
Run Code Online (Sandbox Code Playgroud)\n

我理解这意味着 R 无法“找到”JAGS 的安装。我正在这个项目中使用renv,但即使我停用它并重新运行install_packages("rjags")library(rjags)我也会收到错误(只是文件路径不同):

\n
Loading required package: coda\nError: …
Run Code Online (Sandbox Code Playgroud)

r environment-variables jags rjags renv

3
推荐指数
1
解决办法
1万
查看次数

在geom_sf(R ggplot)中的颜色栏图例中添加逗号

我正在使用ggplot和geom_sf制作地图,并且想在图例中的值上添加逗号。我发现编辑颜色条的唯一方法是通过“指南”,但+ guides(fill = guide_colorbar(labels="comma"))似乎什么也没做(也许是因为“标签”不是“指南”中包含的规范?)如何获得相同的图例包括数量超过1000个的逗号?

获取所有数据的代码有点长,所以我希望有人知道这个答案而无需重新提出建议,但是我可以根据需要对其进行编辑。谢谢!

landingsmap <- ggplot() + 
  scale_x_continuous(limits=c(-126, -116), expand=c(0,0)) + 
  scale_y_continuous(limits=c(32, 42), expand=c(0,0)) + 
  geom_sf(data=simpleblocks, aes(colour=number_fish, fill=number_fish)) + 
  scale_colour_gradient(low="lightcoral", high="darkred", name="Number of Fish") + 
  scale_fill_gradient(low="lightcoral", high="darkred", name="Number of Fish") +
  geom_sf(data=camap, colour="black") + 
  theme(
    panel.background = element_rect(fill="skyblue4", size=0.5, linetype="solid"),
    legend.position = c(0.78, 0.5)
    ) + 
  NULL
landingsmap 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

r ggplot2 r-sf

2
推荐指数
1
解决办法
290
查看次数

将每年的时间点与 R 中过去 365 天的数据进行匹配

我正在尝试合并两个数据集。调查数据集由不同地区的生物多样性调查组成,每1-5年在某个月份进行一次(该月份在地区内固定,但地区之间不固定)。温度数据集由每个调查区域的每日温度读数组成。

对于具有不同开始月份和时间范围的多项调查,我想将每个调查*年份组合与其之前的十二个月的温度数据配对。换句话说,我想将 1983 年 5 月的调查与其之前 12 个月(或 365 天——我不在乎是哪一天)的每日温度记录(截至 1983 年 4 月 30 日)配对。同时,其他地方于 8 月进行的另一项调查1983 年需要与截至 1983 年 7 月 31 日的 365 天温度数据配对。

(至少)有两种方法可以做到这一点——一种是将调查数据与(较长的)温度数据结合起来,然后以某种方式子集化或识别哪些日期属于调查日期之前的 12 个月。另一个方法是从调查数据开始,尝试将温度数据与矩阵列的每一行配对——我尝试使用 和 的时间序列工具执行此操作,tsibbletsModel无法让它“滞后”正确的值按地区分组。

我能够创建一个标识符来加入数据集,以便温度数据中的每个日期都与后续调查及时匹配。然而,并非所有这些都在 365 天内(例如,在下面创建的数据集中,日期1983-06-03与 ref_year 匹配aleutian_islands-5-1986,因为调查每 3-5 年才进行一次)。

以下是我想要的单个区域行为的一些示例(来自下面的示例数据集),尽管我愿意接受实现相同目标但看起来并不完全像这样的解决方案:

对于这一行,我想要生成的新列 ( ref_match) 中的值应该是 NA;该日期早于 365 天ref_year

  region           date        year month month_year ref_year                temperature     
  <chr>            <date>     <dbl> <dbl> <chr>      <chr>                         <dbl>
1 aleutian_islands 1982-06-09  1982     6 6-1982     aleutian_islands-5-1983           0 …
Run Code Online (Sandbox Code Playgroud)

r date time-series lubridate tidyverse

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