Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用ggplot2叠加非对称t分布_R_Ggplot2_Statistics - Fatal编程技术网

R 用ggplot2叠加非对称t分布

R 用ggplot2叠加非对称t分布,r,ggplot2,statistics,R,Ggplot2,Statistics,我试图证明非对称t分布还是正态分布更适合某个数据集。在此过程中,我决定将拟合正态分布和拟合t分布叠加起来。对于正态分布,使用stat_fun没有问题: x <- data.frame(rnorm(500)) names(x) <- c("test.data") ggplot(x,aes(x=test.data)) + stat_function(fun = dnorm, args=list(mean=mean(x$test.data,na.

我试图证明非对称t分布还是正态分布更适合某个数据集。在此过程中,我决定将拟合正态分布和拟合t分布叠加起来。对于正态分布,使用stat_fun没有问题:

    x <- data.frame(rnorm(500))
    names(x) <- c("test.data")

    ggplot(x,aes(x=test.data))  +  
      stat_function(fun = dnorm, args=list(mean=mean(x$test.data,na.rm=TRUE),
      sd=sd(x$test.data,na.rm=TRUE)), aes(colour = 'Normal')) +
      geom_histogram(aes(y = ..density..), alpha = 0.4)
x试试这个:

library(ggplot2)
set.seed(1)
x <- data.frame(rt(5000,df=5,ncp=1)*10+7)
names(x) <- c("test.data")

# Define a Student t distribution with shape (nu) and location (mu)
dt2 <- function(x, mu, nu, df, ncp) {
  dt((x-mu)/nu,df,ncp)/nu
}

ggplot(x,aes(x=test.data))  +  
stat_function(fun = dt2, args=list(mu=7, nu=10, df=5, ncp=1), 
              aes(colour = 'Student t'), lwd=1) +
geom_histogram(aes(y = ..density..), bins=100, alpha = 0.4)
库(ggplot2)
种子(1)
x