我如何转换我的时间;垃圾箱;在R中连续进行时间序列分析?

我如何转换我的时间;垃圾箱;在R中连续进行时间序列分析?,r,graph,time-series,modeling,R,Graph,Time Series,Modeling,使用以下数据: #demo data: set.seed(1234) library(tidyverse) library(fs) n = 100 time= c(15, 30, 60, 90, 120, 180, 240, 300) treat = factor(c("trt1", "trt2", "tr1+2", "trt1+2+3")) intensity = c(sample(1:400, n, repla

使用以下数据:

#demo data:
set.seed(1234)

library(tidyverse)
library(fs)
n = 100
time= c(15, 30, 60, 90, 120, 180, 240, 300)
treat = factor(c("trt1", "trt2", "tr1+2", "trt1+2+3"))
intensity = c(sample(1:400, n, replace=TRUE))
              df <- expand.grid(time= time, treat = treat, intensity=intensity)
df <- data.frame(
                time= rep(df$time, each = 100),
                intensity = rep(df$intensity),
                treat = rep(df$treat, each = 100)
)
演示数据: 种子集(1234) 图书馆(tidyverse) 图书馆(财政司) n=100 时间=c(15,30,60,90,120,180,240,300) treat=因子(c(“trt1”、“trt2”、“tr1+2”、“trt1+2+3”)) 强度=c(样品(1:400,n,替换=真实))
df这是假数据的密度图:

df %>% 
  ggplot(aes(x=intensity, fill=treat)) + 
  geom_density(color="transparent", alpha=.25) + 
  facet_wrap(~as.factor(time), nrow=1) + 
  theme_bw() + 
  theme(panel.grid=element_blank())

现在,如果要将其绘制成线形图,可以计算每个治疗时间对的最高密度值,然后绘制它:

df %>% group_by(treat, time) %>% 
  summarise(d = max(density(intensity)$y)) %>% 
  ggplot(aes(x=time, y=d, colour=treat)) + 
  geom_point() + 
  geom_line() + 
  theme_classic() + 
  labs(x="Time", y="Intensity")

任务的最后一部分是随时间生成密度数据。如果我知道你想做什么,你可以按如下方式做:

out <- df %>% group_by(treat, time) %>% 
  summarise(as.data.frame(density(df$intensity)[c("x", "y")]))

head(out)
# # A tibble: 6 x 4
# # Groups:   treat, time [1]
#   treat  time     x          y
#   <fct> <dbl> <dbl>      <dbl>
# 1 tr1+2    15 -20.5 0.00000608
# 2 tr1+2    15 -19.6 0.00000838
# 3 tr1+2    15 -18.7 0.0000114 
# 4 tr1+2    15 -17.8 0.0000153 
# 5 tr1+2    15 -17.0 0.0000203 
# 6 tr1+2    15 -16.1 0.0000267 
out%分组依据(治疗,时间)%>%
总结(如数据帧(密度(df$强度)[c(“x”,“y”)]))
头(外)
##tibble:6 x 4
##小组:请客,时间[1]
#治疗时间xy
#           
#1 tr1+2 15-20.5 0.00000608
#2 tr1+2 15-19.6 0.00000838
#3 tr1+2 15-18.7 0.0000114
#4 tr1+215-17.80.0000153
#5 tr1+2 15-17.0 0.0000203
#6 tr1+2 15-16.1 0.0000267

x
变量是强度的评估点,
y
变量是该值
x

的密度曲线高度,以下是假数据的密度图:

df %>% 
  ggplot(aes(x=intensity, fill=treat)) + 
  geom_density(color="transparent", alpha=.25) + 
  facet_wrap(~as.factor(time), nrow=1) + 
  theme_bw() + 
  theme(panel.grid=element_blank())

现在,如果要将其绘制成线形图,可以计算每个治疗时间对的最高密度值,然后绘制它:

df %>% group_by(treat, time) %>% 
  summarise(d = max(density(intensity)$y)) %>% 
  ggplot(aes(x=time, y=d, colour=treat)) + 
  geom_point() + 
  geom_line() + 
  theme_classic() + 
  labs(x="Time", y="Intensity")

任务的最后一部分是随时间生成密度数据。如果我知道你想做什么,你可以按如下方式做:

out <- df %>% group_by(treat, time) %>% 
  summarise(as.data.frame(density(df$intensity)[c("x", "y")]))

head(out)
# # A tibble: 6 x 4
# # Groups:   treat, time [1]
#   treat  time     x          y
#   <fct> <dbl> <dbl>      <dbl>
# 1 tr1+2    15 -20.5 0.00000608
# 2 tr1+2    15 -19.6 0.00000838
# 3 tr1+2    15 -18.7 0.0000114 
# 4 tr1+2    15 -17.8 0.0000153 
# 5 tr1+2    15 -17.0 0.0000203 
# 6 tr1+2    15 -16.1 0.0000267 
out%分组依据(治疗,时间)%>%
总结(如数据帧(密度(df$强度)[c(“x”,“y”)]))
头(外)
##tibble:6 x 4
##小组:请客,时间[1]
#治疗时间xy
#           
#1 tr1+2 15-20.5 0.00000608
#2 tr1+2 15-19.6 0.00000838
#3 tr1+2 15-18.7 0.0000114
#4 tr1+215-17.80.0000153
#5 tr1+2 15-17.0 0.0000203
#6 tr1+2 15-16.1 0.0000267

x
变量是强度的评估点,
y
变量是该值的密度曲线高度
x

第二个图中的点与第一个图中的分布有何关系?我知道这两种颜色对应于这两种处理方式,但这些点是平均值、中值还是每个分布值的其他汇总?这些点只是测量的时间点。分布不应该是相同的,因为要匹配的分布的图片有太多的标识信息。但它们的处理方式类似。所以让我们选择一个点来解释:在左边的第一个点,即时间=0的点,但测量强度(y)=高#该水平的强度计数。不过,这是我的问题。在您的示例数据中,
df
只有时间和处理(x值和颜色),我们应该使用什么来计算y值?另外,
silence
没有在演示数据中定义,我假设在调用
expand.grid()
时它应该是
silence=time
。我在生成虚拟数据时犯了错误-我需要添加y值,现在应该是。另外,请注意,仅因为强度是随机抽样的,虚拟数据不会产生类似的分布。第二幅图中的点与第一幅图中的分布有何关系?我知道这两种颜色对应于这两种处理方式,但这些点是平均值、中值还是每个分布值的其他汇总?这些点只是测量的时间点。分布不应该是相同的,因为要匹配的分布的图片有太多的标识信息。但它们的处理方式类似。所以让我们选择一个点来解释:在左边的第一个点,即时间=0的点,但测量强度(y)=高#该水平的强度计数。不过,这是我的问题。在您的示例数据中,
df
只有时间和处理(x值和颜色),我们应该使用什么来计算y值?另外,
silence
没有在演示数据中定义,我假设在调用
expand.grid()
时它应该是
silence=time
。我在生成虚拟数据时犯了错误-我需要添加y值,现在应该是。另外,请注意,仅仅因为强度是随机抽样的,虚拟数据不会产生类似的分布。我添加了一个平滑层,只是为了让我的老板更容易看到(他们讨厌线图,所以我的缺点是不具体),但除此之外,这是完美的。做得很漂亮。我添加了一个平滑层,只是为了更容易地向我的老板展示(他们讨厌线图,所以我的缺点是不具体),但除此之外,这是完美的。