asardaes / dtwclust

R Package for Time Series Clustering Along with Optimizations for DTW
https://cran.r-project.org/package=dtwclust
GNU General Public License v3.0
252 stars 29 forks source link

Why does my "dtwclust" package download and load, but whenever it runs it says "could not find function "dtwclust"? #63

Closed ZZx121318 closed 7 months ago

ZZx121318 commented 1 year ago

I'm using R 4.3.0 run by Rstudio, In this process I tried to run Rstudio by giving it administrator status and by forcing (re) installing and updating the latest version of the dtwclust package (install.packages("dtwclust", repos =" http://cran.r-project.org", type="source", INSTALL_opts = '--force-reinstall')), The result is "could not find function "dtwclust", I do not understand why this is so, I urgently want someone to help me, here is my complete code, thank you!

设置数据路径

setwd("D:\Rwenjian")

安装和加载需要用到的包

install.packages("dtwclust", repos = "http://cran.rstudio.com/") trying URL 'http://cran.rstudio.com/bin/windows/contrib/4.3/dtwclust_5.5.12.zip' Content type 'application/zip' length 3411926 bytes (3.3 MB) downloaded 3.3 MB

程序包‘dtwclust’打开成功,MD5和检查也通过

下载的二进制程序包在 C:\Users\周小星\AppData\Local\Temp\Rtmpcb9YmW\downloaded_packages里

install.packages(c("ggplot2", "dplyr", "tidyr"), dependencies = TRUE) trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.3/ggplot2_3.4.2.zip' Content type 'application/zip' length 4294288 bytes (4.1 MB) downloaded 4.1 MB

trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.3/dplyr_1.1.2.zip' Content type 'application/zip' length 1551893 bytes (1.5 MB) downloaded 1.5 MB

trying URL 'https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/4.3/tidyr_1.3.0.zip' Content type 'application/zip' length 1292092 bytes (1.2 MB) downloaded 1.2 MB

程序包‘ggplot2’打开成功,MD5和检查也通过 程序包‘dplyr’打开成功,MD5和检查也通过 程序包‘tidyr’打开成功,MD5和检查也通过

下载的二进制程序包在 C:\Users\周小星\AppData\Local\Temp\Rtmpcb9YmW\downloaded_packages里

library(dtwclust) 载入需要的程辑包:proxy

载入程辑包:‘proxy’

The following objects are masked from ‘package:stats’:

as.dist, dist

The following object is masked from ‘package:base’:

as.matrix

载入需要的程辑包:dtw Loaded dtw v1.23-1. See ?dtw for help, citation("dtw") for use in publication.

dtwclust: Setting random number generator to L'Ecuyer-CMRG (see RNGkind()). To read the included vignettes type: browseVignettes("dtwclust"). See news(package = "dtwclust") after package updates. Warning message: 程辑包‘dtwclust’是用R版本4.3.1 来建造的

library(dplyr)

载入程辑包:‘dplyr’

The following objects are masked from ‘package:stats’:

filter, lag

The following objects are masked from ‘package:base’:

intersect, setdiff, setequal, union

Warning message: 程辑包‘dplyr’是用R版本4.3.1 来建造的

library(tidyr) Warning message: 程辑包‘tidyr’是用R版本4.3.1 来建造的 library(ggplot2) Warning message: 程辑包‘ggplot2’是用R版本4.3.1 来建造的

读取数据

data <- read.csv("pig3.csv")

将长格式转化为宽格式

data_wide <- data %>%

  • pivot_wider(names_from = pig_id, values_from = weight)

    定义距离函数

    dist_fun <- function(x, y) dtw(x, y)$distance

    进行聚类分析

    res <- dtwclust(data_wide,

  • dist_method = dist_fun,
  • k = 2:10,
  • bootstrap = TRUE,
  • seed = 123) Error in dtwclust(data_wide, dist_method = dist_fun, k = 2:10, bootstrap = TRUE, : could not find function "dtwclust"

    进行聚类数评估

    fviz_nbclust(res$boot_summary,

  • method = "silhouette") Error in fviz_nbclust(res$boot_summary, method = "silhouette") : could not find function "fviz_nbclust"

    将聚类结果可视化

    ggplot(data_wide, aes(x = data, y = weight, color = as.factor(res$clusters))) +

  • geom_line(size = 0.5) +
  • facet_wrap(~pig_id, nrow = 4) +
  • labs(title = "Clustering of pig weight measurements") Error in combine_vars(): ! At least one layer must contain all faceting variables: pig_id ✖ Plot is missing pig_id ✖ Layer 1 is missing pig_id Run rlang::last_trace() to see where the error occurred. Warning message: Using size aesthetic for lines was deprecated in ggplot2 3.4.0. ℹ Please use linewidth instead. This warning is displayed once every 8 hours. Call lifecycle::last_lifecycle_warnings() to see where this warning was generated.
ZZx121318 commented 1 year ago

When I upgrade the version of R to be the same as the package version, it still doesn't fix the problem. Why?

asardaes commented 1 year ago

Because the package doesn't include a function called dtwclust, the main function is called tsclust.

ZZx121318 commented 1 year ago

Because the package doesn't include a function called , the main function is called .dtwclust``tsclust

Thank you very much for your reply. Then how should I modify my code?

asardaes commented 10 months ago

Ah I completely forgot about this. You should update your code to use the new function instead.