R 如何得到加权数据的威布尔分布
目前,我只能传递一列,即“col”,作为dweibull函数的输入,但我想绘制加权数据的威布尔分布,即传递两列,一列用于数据,另一列用于权重。 我查看了此链接:。但这没有帮助,给了我一个扭曲的情节R 如何得到加权数据的威布尔分布,r,ggplot2,weibull,R,Ggplot2,Weibull,目前,我只能传递一列,即“col”,作为dweibull函数的输入,但我想绘制加权数据的威布尔分布,即传递两列,一列用于数据,另一列用于权重。 我查看了此链接:。但这没有帮助,给了我一个扭曲的情节 #libraries loaded library(tidyverse) library(janitor) library(tibble) library(stats) library(fitdistrplus) library(ggplot2) library(ggtext) 请简化您的帖子,没有
#libraries loaded
library(tidyverse)
library(janitor)
library(tibble)
library(stats)
library(fitdistrplus)
library(ggplot2)
library(ggtext)
请简化您的帖子,没有人将加载/安装100个软件包。我已经简化了代码,现在只添加了所需的库。如果有人对此查询有任何解决方案,请务必回答。谢谢。请简化您的帖子,没有人将加载/安装100个软件包。我已经简化了代码,现在只添加了所需的库。如果有人对此查询有任何解决方案,请联系我。谢谢。
#functions required
f <- function(k) {
step <- k
function(y) seq(0, ceiling(max(y)) + 2, by = step)
}
f1 <- function(k) {
step <- k
function(y) seq(0, max(y) + 0.02, by = step)
}
l <- function(y) c(0, ceiling(max(y)) + 2)
l1 <- function(y) c(0, max(y) + 0.02)
```
```
#dataset
freq_data1<-tibble( "col"= c( 3.9815,3.1105,3.3755,3.0600,3.2765,3.0065,2.7275,2.2970,2.5580,3.2320),
"weight"=c(0.1428571,0.1428571,0.1428571,0.1428571,0.1428571,0.1428571,0.1428571,0.1428571,0.1428571,
0.1428571))
```
# **current code**
#pulling column named "col"
freq_data1 %>%
pull(col) ->freq_data
freq_data[!freq_data %in% 0] -> freq_data
freq_data %>%
fitdist(distr = "weibull", method = "mle") -> fit_w # fits weibull distribution on raw data
fit_w$estimate[1] %>% round(12) -> w_1 # shape or gamma factor
fit_w$estimate[2] %>% round(1) -> w_2 # scale or alpha factor
dweibull(x = freq_data, shape = w_1, scale = w_2) -> fwei # density, distribtn, quantile fn (n: number of observations, )
tibble(raw_data = freq_data, freq = fwei) -> df
min(df$raw_data, na.rm = T) %>% floor() -> min_sel
max(df$raw_data, na.rm = T) %>% floor() -> max_sel
ggplot(data = df) +
geom_histogram(data = df, aes(x = raw_data, (y=stat(width*density))), fill = "#361752", color = "#ffbf00", breaks = seq(0,max_sel,0.5)) +
geom_line(aes(x=raw_data, y=stat(fwei*0.5)), size = 2) +
labs(x="ws", y = "Frequency [%]") +
ggtitle(paste0("**System: ", "dummy", "**"),
subtitle = bquote(atop(bold(.("Frequency Histogram and Weibull Distribution")),
atop(.(paste0("Best-fit Weibull distribution(k = ", round(w_1, 2), ",", " A = ", round(w_2, 2), " m/s)")))))) +
scale_color_manual(name = "", values = c("brown", "black"), labels = c("Actual Data", "Weibull Distribution")) +
#theme_minimal() +
coord_cartesian(clip = "off") +
theme(legend.position = "none",
axis.text = element_text(face = "bold", size = 20),
plot.title = element_markdown(size = 33),
axis.title = element_text(face = "bold", size = 25),
legend.text = element_text(face = "bold", size = 20),
legend.title = element_text(face = "bold", size = 25),
panel.grid.major = element_line(color = "#F1F1F1", size = 0.3),
panel.grid.minor = element_line(color = "#F1F1F1", size = 0.05),
plot.subtitle = element_text(face = "bold", size = 25),
legend.key = element_rect(fill = "white", color = "white"),
panel.background = element_rect(fill = "white",
color = "white",
size = 1),
panel.border = element_blank(),
axis.line = element_line(colour = "black", size = 0.5, linetype = "solid")) +
scale_x_continuous(expand = c(0,0), breaks = f(1)) +
scale_y_continuous(expand = c(0,0),labels = scales::percent_format(accuracy = 1,suffix = ""), limits = l1, breaks = f(0.01)) #-> freq_hist_wei_plot
```
Above is my code.With dweibull I cannot pass the "weight" column.I want to get the weibull distribution for weighted data.