kernel-density

R: Kernel Density Plots (Bandwidth Must be Strictly Positive)

烂漫一生 提交于 2021-02-11 12:38:03
问题 I am using the R programming language. I am following this tutorial over here for making 3d kernel density plots in R: https://plotly.com/r/3d-surface-plots/: library(MASS) library(plotly) kd <- with(MASS::geyser, MASS::kde2d(duration, waiting, n = 50)) fig <- plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface() fig I decided to try this on my own data : #generate data a = rnorm(100,10,10) b = rnorm(100,5,5) c = rnorm(100,5,10) d = data.frame(a,b,c) #make 3d plot (I think n = 50 refers to

Crating Kernel density estimate for polygon in R

不想你离开。 提交于 2021-02-11 12:29:30
问题 I Have a shapefile of polygons and another one of points that are distributed over the polygons. I would like to create a kernel density estimate for each polygon based on the points it contains. unfortunately I was only able to create squared KDEs with the kde2d function from the MASS package. I would like the KDEs to be shaped as the polygons. Any suggestions? kde1 <- kde2d(poly$X, poly$Y, n=100,) enter image description here 回答1: You can use the spatstat package for this. Here is an

Python: Overlap between two functions (PDF of kde and normal)

别说谁变了你拦得住时间么 提交于 2021-02-10 09:44:35
问题 Short summary: Im trying to figure out how to calculate overlap between two functions. One is a gaussian, the other is a kernel density, based on data. Then, I would like to make a small algorithm that choose the mean and variance for the gaussian, which maximises overlap Firstly, needed imports: import numpy as np import matplotlib.pyplot as plt import seaborn as sns from scipy.stats.kde import gaussian_kde import scipy I have some data which is approximately normal (somewhat heavy right

rpy2 passing python reserved keyword arguments

自作多情 提交于 2021-02-07 20:25:57
问题 I am trying to use r's density function through python, and I have to pass the 'from', 'to' arguments to the density functions. However, since the word 'from' is a reserved ketyword in python, how can I achieve this? Thank you. Here is the code so far. r_density=robjects.r('density') f_a = robject.FloatVector(a) r_a = r_density(f_a, bw='SJ', n=1024) ## Here I need to add 'from' and 'to' arguments 回答1: You can use dict argument-unpacking to pass reserved words as parameter names: r_a = r

R ggplot2 - Simple plot- cannot specify log axis limits

时间秒杀一切 提交于 2021-02-07 14:20:35
问题 I'm trying to create a simple densityplot in R in ggplot2. Here's my code which works great. d <- ggplot(result, aes(x=result$baseMeanA)) d + geom_density(colour="darkgreen", size=2, fill="darkgreen") + scale_x_log10() + scale_y_continuous(limits = c(0, 0.45)) The problem is that I cannot adjust the x-axis as I would like, into negative numbers. scale_x_log10(limits= c(1, 10000)) works great, but scale_x_log10(limits= c(-1, 10000)) does not work at all! It gives me this error: Error in if

ggplot2 density of circular data

橙三吉。 提交于 2021-02-07 14:13:14
问题 I have a data set where x represents day of year (say birthdays) and I want to create a density graph of this. Further, since I have some grouping information (say boys or girls), I want to use the capabilities of ggplot2 to make a density plot. Easy enough at first: require(ggplot2); require(dplyr) bdays <- data.frame(gender = sample(c('M', 'F'), 100, replace = T), bday = sample(1:365, 100, replace = T)) bdays %>% ggplot(aes(x = bday)) + geom_density(aes(color = factor(gender))) However,

ggplot2 density of circular data

时光怂恿深爱的人放手 提交于 2021-02-07 14:11:44
问题 I have a data set where x represents day of year (say birthdays) and I want to create a density graph of this. Further, since I have some grouping information (say boys or girls), I want to use the capabilities of ggplot2 to make a density plot. Easy enough at first: require(ggplot2); require(dplyr) bdays <- data.frame(gender = sample(c('M', 'F'), 100, replace = T), bday = sample(1:365, 100, replace = T)) bdays %>% ggplot(aes(x = bday)) + geom_density(aes(color = factor(gender))) However,

Limit the range of x in seaborn distplot KDE estimation

﹥>﹥吖頭↗ 提交于 2021-02-06 10:45:44
问题 Suppose we have an array with numbers between 0 and 1: arr=np.array([ 0. , 0. , 0. , 0. , 0.6934264 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.6934264 , 0. , 0.6934264 , 0. , 0. , 0. , 0. , 0.251463 , 0. , 0. , 0. , 0.87104906, 0.251463 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.48419626, 0. , 0. , 0. , 0. , 0. , 0.87104906, 0. , 0. , 0.251463 , 0.48419626, 0. , 0.251463 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.251463 , 0. , 0.35524532, 0. , 0. , 0. , 0. , 0. , 0.251463 , 0.251463 ,

Simulate from kernel density estimator with variable underlying grid

亡梦爱人 提交于 2021-01-28 05:51:02
问题 I have a dataset that I'm using to create an empirical probability distribution by estimating a kernel density. Right now I'm using R's kde2d from the MASS package. After estimating the probability distribution, I use sample to sample from slices of the 2D distribution along the x-axis. I use sample much like described here. Example code would look like this library(MASS) set.seed(123) x = rnorm(100, 1, 0.1) set.seed(456) y = rnorm(100, 1, 0.5) den <- kde2d(x, y, n = 50, lims = c(-2, 2, -2, 2

Unnormalized histogram plots in Seaborn are not centered on X-axis

守給你的承諾、 提交于 2021-01-07 01:36:13
问题 I am graphing the number of occurrences that a value occurs in two different datasets. One plot (plot 1) graphs perfectly, the bars are right above the numbers on the x-axis. On the second plot (plot 2), there should be two bars, one above the 1 x-axis value and the other above the 2 x-axis value, but both bars are thick and squashed between 1 and 2on the x-axis. How do I get the second graph to look like the first graph? This is the code that I used in Jupyter notebook to generate both plots