简体   繁体   English

R中的循环密度曲线

[英]loop density curve in R

nummonty<- c(09,10,11,12)
monty <- c('Sep','Oct','Nov','Dec')
daa <- c(rlnorm(1000), rlnorm(1000), rlnorm(1000),rlnorm(1000))
dat <- data.frame(nummonty,monty,daa)

dfun <- function(x, a, b) 1/(sqrt(2*pi*b(x-1)))*exp(-0.5*((log(x-a)/b)^2))

#Fit the density curves for each "month"
#dens <- density(dat$daa, n = nrow(daa))
#df_dens <- data.frame(x = dens$x, y = dens$y)

#Fit the data for all months 
fit <- nls(y ~ dfun(x, a, b), data = df_dens, start = list(a = mean(dat$daa[i]), b = sd(dat$daa[i])))

How do I include for each montyg type the density curve and extract the fitted data including the nls coeff a and b in a separate csv file?如何为每个montyg类型包含密度曲线并在单独的csv文件中提取拟合数据,包括nls coeff ab For a single case, the solution is here but I'd like to loop it.对于单个案例,解决方案在这里,但我想循环它。 Any help is appreciated.任何帮助表示赞赏。

What you want is possible using patchwork and avoiding the use of loop with a function.你想要的是使用patchwork ,避免使用带有函数的循环。 You can split your data in a list by month and the fit the models.您可以按月拆分列表中的数据并拟合模型。 Then, you can arrange the plots as desired.然后,您可以根据需要排列图。 Just few thoughts in your code.您的代码中只有一些想法。 You literally took the function from the other post and changed from normal to lognormal.您确实从另一篇文章中获取了该功能,并将其从正常更改为对数正态。 Be careful about that because setting logs in dfun() without knowing what will happen is not a good practice.请注意这一点,因为在不知道会发生什么的情况下在dfun()设置日志不是一个好习惯。 In most of cases, NA will be produced and you will get errors in stat_function() .在大多数情况下,将产生NA并且您将在stat_function()得到错误。 For that reason and to show you how to do, I will use the same function as in the cited post.出于这个原因并向您展示如何做,我将使用与引用帖子中相同的功能。 After that you can adjust to your real data:之后,您可以调整到您的真实数据:

library(tidyverse)
library(patchwork)
#Data
nummonty<- c(09,10,11,12)
monty <- c('Sep','Oct','Nov','Dec')
daa <- c(rnorm(1000), rnorm(1000), rnorm(1000),rnorm(1000))
dat <- data.frame(nummonty,monty,daa)
gdat <- data.frame(montyg=unique(dat$monty),nummontyg=unique(dat$nummonty))
#Function
dfun <- function(x, a, b) 1/(sqrt(2*pi)*b)*exp(-0.5*((x-a)^2/(2*b^2))) 
#Create a data list
List <- split(dat,dat$monty)
#Function
myfun <- function(x)
{
  #Fit the density curves for each "month"
  dens <- density(x$daa, n = nrow(x))
  df_dens <- data.frame(x = dens$x, y = dens$y)
  #Fit the model
  fit <- nls(y ~ dfun(x, a, b),data = df_dens,start = list(a = mean(x$daa), b = sd(x$daa)))
  #Plot
  G1 <- ggplot(x, aes(x = daa)) + 
    geom_histogram(aes(y = ..density..), binwidth = 0.5)+
    stat_function(fun = dfun,
                  args = list(a = coef(fit)[1], b = coef(fit)[2]))+
    ggtitle(unique(x$monty))
  return(G1)
}
#Apply
List2 <- lapply(List,myfun)
#Wrap plots
G <- wrap_plots(List2,ncol = 2)

Output:输出:

在此处输入图片说明

Update: To extract coefs try this:更新:要提取 coefs 试试这个:

#Function
myfun2 <- function(x)
{
  #Fit the density curves for each "month"
  dens <- density(x$daa, n = nrow(x))
  df_dens <- data.frame(x = dens$x, y = dens$y)
  #Fit the model
  fit <- nls(y ~ dfun(x, a, b),data = df_dens,start = list(a = mean(x$daa), b = sd(x$daa)))
  #Plot
  C1 <- coef(fit)
  return(C1)
}
#Apply
List2 <- lapply(List,myfun2)

Output:输出:

List2
$Dec
        a         b 
0.0426051 0.9513849 

$Nov
         a          b 
0.02477635 0.96920246 

$Oct
         a          b 
0.08146357 0.98437906 

$Sep
          a           b 
-0.02620755  0.93557576 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM