首页 \ 问答 \ Html 5 Drag n Drop Ajax(Html 5 Drag n Drop Ajax)

Html 5 Drag n Drop Ajax(Html 5 Drag n Drop Ajax)

我试图处理Ajax Dom Elements Loaded上的dragstart事件,但不起作用。 以下是我如何处理Dragstart事件以获取要移动的元素的ID:

  $(document).on(".dragItem", "dragstart", function (ev) {

        ev.originalEvent.dataTransfer.setData("text", ev.target.id);
    });

对于页面上加载的任何元素,Dragstart由上面的代码处理。

但是,当我稍后在代码中使用JS创建Html div时,事件不会响应。

<div class="dragItem" draggable='true'>My Drag Item </div>

实际上我通过jquery Ajax接收JSON数据,然后使用HTML Created更新DOM。


I m trying to handle dragstart event on Ajax Dom Elements Loaded but doesn't work. Here is how I handle the Dragstart Event to get the ID of the Element being Moved:

  $(document).on(".dragItem", "dragstart", function (ev) {

        ev.originalEvent.dataTransfer.setData("text", ev.target.id);
    });

For any element loaded on the Page the Dragstart is handled by the code above.

However, when I create an Html div using JS later in the Code, the Event doesn't respond.

<div class="dragItem" draggable='true'>My Drag Item </div>

In fact I'm receiving JSON Data through jquery Ajax, then update The DOM with the HTML Created.


原文:https://stackoverflow.com/questions/34299381
更新时间:2021-06-22 22:06

最满意答案

为了可视化概率回归模型的拟合优度,“标准”残差(例如,泊松或偏差)通常不那么具有信息性,因为它们主要捕获均值的建模而不是整个分布的建模。 有时使用的一种替代方案是(随机化的)分位数残差。 没有随机化,它们被定义为qnorm(pdist(y)) ,其中pdist()是拟合分布函数(这里是ZIP模型), y是观察值, qnorm()是标准正态分布的分位数函数。 如果模型拟合,残差的分布应该是标准的,并且可以在QQ图中检查。 在离散分布的情况下(如此处),需要随机化来打破数据的离散性质。 有关更多详细信息,请参见Dunn&Smyth(1996, Journal of Computational and Graphical Statistics ,5,236-244)。 在R中你可以使用R-Forge的countreg包(希望很快也会在CRAN上)。

另一种检查数据边际分布的替代方案是所谓的根图。 它在视觉上比较了计数0,1,......的观察和拟合频率。通常比显示随机分位数残差的QQ图更好地显示过量零和/或过度离散的问题。 有关详细信息,请参阅我们的论文Kleiber&Zeileis(2016, The American Statistician70 (3),296-303, doi:10.1080 / 00031305.2016.1173590 )。

将这些应用于回归模型可以快速显示零膨胀泊松不能解释响应中的过度离散。 (计数高达100以上,基于泊松的分布几乎从不适合。)此外,零膨胀模型并不适合,因为对于assnage = 1和= 2,零很少,并且不需要零膨胀。 这导致零膨胀部分中的相应系数朝向-Inf具有非常大的标准误差(如在二元回归中的准分离中)。 因此,由两部分组成的障碍模型更适合,并且可能更容易解释。 最后,由于两个assnage组不同,我会将assnage编码为一个因素(我不清楚你是否已经这样做了)。

因此,为了分析您的数据,我使用您帖子中提供的yc并确保:

yc$assnage <- factor(yc$assnage)

为了第一次探索assnage的影响我绘制count是否为正(左:零障碍)和对count刻度上的正count (右:计数)。

plot(factor(count > 0, levels = c(FALSE, TRUE), labels = c("=0", ">0")) ~ assnage,
  data = yc, ylab = "count", main = "Zero hurdle")
plot(count ~ assnage, data = yc, subset = count > 0,
  log = "y", main = "Count (positive)")

探索性的情节

然后,我使用R-Forge的countreg包装适合ZIP,ZINB和障碍NB模型。 这还包含zeroinfl()hurdle()函数的更新版本。

install.packages("countreg", repos = "http://R-Forge.R-project.org")
library("countreg")
zip <- zeroinfl(count ~ assnage * spawncob, offset = logoffset,
  data = yc, dist = "poisson")
zinb <- zeroinfl(count ~ assnage * spawncob, offset = logoffset,
  data = yc, dist = "negbin")
hnb <- hurdle(count ~ assnage * spawncob, offset = logoffset, data = yc,
  dist = "negbin")

ZIP明显不合适,障碍NB略胜ZINB。

BIC(zip, zinb, hnb)
##      df      BIC
## zip  20 7700.085
## zinb 21 3574.720
## hnb  21 3556.693

如果检查summary(zinb)您还会看到零膨胀部分中的某些系数大约为10(对于虚拟变量),标准误差summary(zinb)到两个数量级。 这实质上意味着相应组中的零膨胀概率变为零,因为负二项分布已经具有足够的零响应概率权重(分组1和2)。

为了在HNB正确捕获响应时可视化ZIP模型不适合,我们现在可以使用根图。

rootogram(zip, main = "ZIP", ylim = c(-5, 15), max = 50)
rootogram(hnb, main = "HNB", ylim = c(-5, 15), max = 50)

rootograms

ZIP的波浪状图案清楚地显示了模型中未适当捕获的数据中的过度离散。 相比之下,障碍相当合适。

作为最后的检查,我们还可以查看障碍模型中分位数残差的QQ图。 这些看起来相当正常,并且没有显示模型的可疑偏离。

qqrplot(hnb, main = "HNB")

Q-Q情节

由于残差是随机的,您可以重新运行代码几次以获得变化的印象。 qqrplot()也有一些参数让你在一个图中探索这种变化。


For visualizing the goodness of fit of probabilistic regression models, "standard" residuals (e.g., Poisson or deviance) are often not so informative because they mostly capture the modeling of the mean but not of the entire distribution. One alternative that is somtimes used are (randomized) quantile residuals. Without randomization they are defined as qnorm(pdist(y)) where pdist() is the fitted distribution function (here a ZIP model), y are the observations, and qnorm() is the quantile function of the standard normal distribution. If the model fits, the distribution of the residuals should be standard normal and can be checked in a Q-Q plot. In case of discrete distributions (as here), randomization is needed to break up the discrete nature of the data. See Dunn & Smyth (1996, Journal of Computational and Graphical Statistics, 5, 236-244) for more details. In R you can use the countreg package from R-Forge (hopefully soon also on CRAN) for these.

Another alternative which checks the marginal distribution of the data is the so-called rootogram. It visually compares the observed and fitted frequencies for the counts, 0, 1, ... It is often better at displaying the problems of excess zeros and/or overdispersion than Q-Q plots of randomized quantile residuals. See our paper Kleiber & Zeileis (2016, The American Statistician, 70(3), 296–303, doi:10.1080/00031305.2016.1173590) for more details.

Applying these to your regression model quickly shows that a zero-inflated Poisson is not accounting for the overdispersion in the response. (With counts up to and beyond 100 a Poisson-based distribution almost never fits well.) Furthermore, a zero inflation model does not fit very well because for assnage = 1 and = 2 there are very few zeros and no zero inflation is needed. This leads to corresponding coefficients in the zero inflation part that go towards -Inf with very large standard errors (like in quasi-separation in binary regression). Therefore, a two-part hurdle model fits somewhat better and is likely easier to interpret. Finally, due to the two assnage groups being different I would code assnage as a factor (it is not clear to me whether you did that already).

Thus, for analyzing your data I use yc as provided in your post and ensure:

yc$assnage <- factor(yc$assnage)

For a first explorative look at the influence of assnage I plot whether or not the count is positive (left: zero hurdle) and the positive count on a log-scale (right: count).

plot(factor(count > 0, levels = c(FALSE, TRUE), labels = c("=0", ">0")) ~ assnage,
  data = yc, ylab = "count", main = "Zero hurdle")
plot(count ~ assnage, data = yc, subset = count > 0,
  log = "y", main = "Count (positive)")

exploratory plots

Then, I fit the ZIP, ZINB, and hurdle NB models using the countreg package from R-Forge. This also contains updated versions of the zeroinfl() and hurdle() functions.

install.packages("countreg", repos = "http://R-Forge.R-project.org")
library("countreg")
zip <- zeroinfl(count ~ assnage * spawncob, offset = logoffset,
  data = yc, dist = "poisson")
zinb <- zeroinfl(count ~ assnage * spawncob, offset = logoffset,
  data = yc, dist = "negbin")
hnb <- hurdle(count ~ assnage * spawncob, offset = logoffset, data = yc,
  dist = "negbin")

The ZIP is clearly inappropriate and the hurdle NB is slightly better than the ZINB.

BIC(zip, zinb, hnb)
##      df      BIC
## zip  20 7700.085
## zinb 21 3574.720
## hnb  21 3556.693

If you check summary(zinb) you will also see that some coefficients in the zero-inflation part are around 10 (for a dummy variable) with standard errors one or two orders of magnitude larger. This essentially means that the zero-inflation probability in the corresponding groups goes to zero because the negative binomial distribution already has more than enough probability weight for the zero responses (assnage groups 1 and 2).

To visualize that the ZIP model does not fit while the HNB appropriately captures the response, we can now use the rootograms.

rootogram(zip, main = "ZIP", ylim = c(-5, 15), max = 50)
rootogram(hnb, main = "HNB", ylim = c(-5, 15), max = 50)

rootograms

The wave-like pattern for the ZIP clearly shows the overdispersion in the data that is not appropriately captured by the model. In contrast, the hurdle fits reasonably well.

As a final check we can also look at the Q-Q plot of the quantile residuals in the hurdle model. These look fairly normal and show no suspicious departures from the model.

qqrplot(hnb, main = "HNB")

Q-Q plot

As the residuals are randomized, you can re-run the code a couple of times to get an impression of the variation. qqrplot() has also some arguments that let's you explore this variation in a single plot.

相关问答

更多
  • 问:可以在R中安装有效的,零膨胀的准泊松模型吗? 答:是的,在R中拟合有效的,零膨胀的准泊松模型。 留出pscl::zeroinfl()并专注于glmmADMB::glmmadmb() 。 glmmADMB::glmmadmb() : family="nbinom1"仍然是具有有效似然性的负二项模型 - 帮助文件只是声明参数化对应于quasipoisson。 如果你深入了解glmmADMB::glmmadmb()你会发现“nbinom”,“nbinom1”和“nbinom2”都改为family="nbino ...
  • 参见,例如, ar(AirPassengers, aid = FALSE, order.max = 1, method = "ols")$resid 。 也就是说,您应该检查返回的元素residuals ,而不是应用函数residuals或residuals 。 See, e.g., ar(AirPassengers, aid = FALSE, order.max = 1, method = "ols")$resid. That is, instead of applying functions resid ...
  • 所以我发现哪些不适合大回归的是简单地建立一个累积回归结果的数据框,我可以稍后进行修改。 out <- data.frame() for(i in 2000:2012){ dtSub <- subset(dt, Year <= i) fit <- lm(pctEarn ~ y2 + Company, data = dtSub) fits[[i - 1999]] <- fit dtSub$fitted <- fitted(fit) dtSub$resid <- residuals(fit) ...
  • 使用aov() ,您将获得顶级$residuals属性,适用于某些适合,但不适用于其他属性。 例如,使用如下的简单模型,您可以直接访问残差(使用str()来查看对象的结构,包括可以访问的属性): fit1 <- aov(Sepal.Length ~ Sepal.Width, data=iris) str(fit1$residuals) ## Named num [1:150] -0.644 -0.956 -1.111 -1.234 -0.722 ... ## - attr(*, "names")= chr ...
  • 我想解释残差,但是以num_encounters的比例返回。 你可以轻松地计算出它们: mod <- lm(log(num_encounters) ~ log(distance)*sampling_effort, data=df) res <- df$num_encounters - exp(predict(mod)) I want to interpret the residuals but get them back on the scale of num_encounters. You can ea ...
  • 你必须分别计算它们。 这是一个简短的模块,定义了这些残差的函数。 它们采用观察到的频率和预期频率(由chi2_contingency返回)。 请注意,虽然chi2_contingency和以下residuals函数适用于n维数组, stdres此处实现的stdres仅适用于2D数组。 from __future__ import division import numpy as np from scipy.stats.contingency import margins def residuals(o ...
  • 要获得相同的结果,您应该将type设置为response (默认为pearson) all.equal(fm_zip$residuals,residuals(fm_zip,'response')) [1] TRUE 从?residuals.zeroinfl : 残差方法可以计算原始残差(观测拟合)和皮尔逊残差(由方差函数的平方根缩放的原始残差)。 perason方差定义为: mu <- predict(fm_zip, type = "count") phi <- predict(fm_zip, type ...
  • 为了可视化概率回归模型的拟合优度,“标准”残差(例如,泊松或偏差)通常不那么具有信息性,因为它们主要捕获均值的建模而不是整个分布的建模。 有时使用的一种替代方案是(随机化的)分位数残差。 没有随机化,它们被定义为qnorm(pdist(y)) ,其中pdist()是拟合分布函数(这里是ZIP模型), y是观察值, qnorm()是标准正态分布的分位数函数。 如果模型拟合,残差的分布应该是标准的,并且可以在QQ图中检查。 在离散分布的情况下(如此处),需要随机化来打破数据的离散性质。 有关更多详细信息,请参见 ...
  • 你需要确保time是一个因素。 time <- as.factor(c("pm", "am", "am", "pm", "am", "pm", "pm", "am")) 然后你的情节就会奏效。 当你传递一个角色向量时,看起来aov会有点困惑。 You need to make sure time is a factor. time <- as.factor(c("pm", "am", "am", "pm", "am", "pm", "pm", "am")) Then your plot will wor ...
  • 没有可重复的例子,很难回答这个问题,但我会提供一些观察结果(评论时间太长): pscl的默认行为是对模型的零膨胀和计数(条件)部分使用相同的公式。 除非您拥有非常大的数据集,否则您很可能无法在数据的计数和零通胀方面拟合10参数模型(拦截+9个协变量)。 (一个合理的经验法则是你的观测值应该是参数的20倍,因此至少有400次观测 - 而这个规则对于估算零通货膨胀可能是保守的。) 您的一个参数估计值( x6 )近似为零,表明您的数据中没有足够的变化来估计该参数(或者此协变量存在其他问题,例如,此维度中存在极端异 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)