Bootstrap modal não carrega Google Maps v3 corretamente

Question:
Pessoal estou com o seguinte problema, ao carregar Google Maps v3 em um Modal com o Framework Bootstrap o mapa não é exibido corretamente.
Segue o código:
    <!DOCTYPE html>

        <meta charset="UTF-8">
        <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />

        Open modal

                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

Peggy Guggenheim Collection - Venice

                        <button type="button" class="close" data-dismiss="modal">Fechar</button>

JSFiddle com exemplo do erro.
Comment: Montei um JSFiddle pra você que reproduz o comportamento: http://jsfiddle.net/h4d5006f/.
Comment: Wakim, no JSFiddle que você montou não tem o Modal.
Comment: Atualizei, vê denovo, acabei esquecendo de dar `fork` antes.
Comment: Wakim, agora está exatamente como na imagem a cima que postei, ou seja, com o erro, o mapa não renderiza no model. Sabe alguma possível solução ?
Comment: Editei sua resposta com alguns acertos, se me permite. Inclui o link do JSFiddle para que não precisem procurar nos comentários.
Comment: Sem problema. No exemplo que você montou no JSFiddle se selecionar um lugar pelo Street View ele renderiza e fica tudo certo, faz o teste.
Comment: Eu acho que o `resize` vai funcionar, mas o evento de `shown.bs.modal` não esta sendo chamado.
Comment: Vamos [continuar esta discussão no bate-papo](http://chat.stackexchange.com/rooms/17417/discussion-between-pedro-a-and-wakim).
Answer: O problema é que estava executando o bind antes do DOM ter sido construído por completo. Logo o seletor $('#mapmodals') não estava trazendo nenhum elemento e por isso não chamava a função definida para o evento, que redimensionava o #map-canvas.
Para isso, englobei o código de inicialização dos eventos dentro de um ready do jQuery.
O código ficou:
// Pode usar o document.ready também:
//$(document).ready(function() { ... });

$(function () {
    // Esse trecho de codigo sera executado quando a construção do DOM for terminada. Possibilitando a localização dos elementos nos seletores.
    $("#mapmodals").on('shown.bs.modal', function () {
        google.maps.event.trigger(map, 'resize');
    });
});

var map;

function initialize() {
    var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(-34.397, 150.644)
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);

Exemplo no JSFiddle com a correção.
Comment: Copiei igual o seu exemplo que resolveu, mas não sei o porque agora não está exibindo nada.
Comment: Já descobri o possível erro. Está executando o `$('#mapmodals").on(...);` antes do documento ser carregado, logo o seletor estava vazio. Vou atualizar minha resposta. Agora faz sentido não funcionar.
Comment: Fiz igual o JSFiddle e nada ainda.
Comment: Se a solução não deu certo é preciso mais informações de onde esta aplicando. Se puder coloque o restante do html. Esta gerando algum erro no console?
Comment: Editei a pergunta inserindo a página toda.
Comment: Qual versão do `Bootstrap` esta usando? Fiz o `JSFiddle` com a versão 3.1.1. Não falta importar o arquivo javascript do `Bootstrap`?
Comment: Eu importei o Boostrap, olha lá no código.


Associate user to a post in Django

Question: hi everyone i'm trying to make a blog and i want to associate a user to post .. and to comment , it's a multi-user blog and i don't know how to do it , any help guys ? !
here is the model file :
from django.db import models
from django.utils import timezone
from django.conf import settings
from django.utils.text import slugify

# Create your models here.
#this is for categories
class Category(models.Model):
    title=models.CharField(max_length=100,default='')
    def __str__(self):
        return self.title
        #this is where a user can create his own gigs
class Gigposter(models.Model):

    title=models.CharField(default='',max_length=100,blank=False)
    user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=False)
    categories=models.OneToOneField(Category,on_delete=models.PROTECT,default='',null=False)
    published_at=models.DateTimeField(auto_now_add=True)
    description=models.TextField(default='',max_length=None,blank=False)
    mainphoto=models.ImageField(default='')
    photo=models.ImageField()
    def __str__(self):
        return self.title

#this is where a user can comment and say what he thinks about others work
class Comment_area(models.Model):

    user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=False)
    comment=models.TextField(max_length=None,default='')
    commented_at=models.DateTimeField(auto_now_add=True)

and the views file is empty as you can see :
from django.shortcuts import render

# Create your views here.

Comment: So where exactly is your problem? As I metioned in your previous question, dont use OneToOneFields here! It means that one user is associated with ONE comment or post. Use a Foreignkey to the user and you should be fine.
Comment: @errorinpersona thank so much for answering me , so my problem is when i go to the admin panel , it's up to me to chose the user i want , and i want to the user to be selected automatically .. for exemple here your comment is associated with your name
Comment: Ah okay - so have a look at @Ivan K. s answer then
Answer: Wouldn't recommend using a OneToOneField here, as it tells Django the user is tied to exactly one comment/post (when, a user is likely to post more than once).
You could use models.ForeignKey:
from django.contrib.auth.models import User

class Gigposter(models.Model):
   # Other properties...
   user = models.ForeignKey(
                    'User'
                     on_delete=models.CASCADE
                 )

Comment: thanks for answering me, i'll change it now , but even if i change it , the problem is not solved  , i want  the user to be associated automatically to my model
Answer: If you want to automatically associate a user to a post when working on the admin page, you should redefine save_model method of your model. This method describes everything what should be done when you save your model. In your case you should add something like
class GigposterAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
    obj.user = `request.user`
    super().save_model(request, obj, form, change)

admin.site.register(Gigposter, GigposterAdmin)

to your admin.py. You should also exclude the user field from fieldset in GigposterAdmin. See this for reference.
If you need to identify user in your views, you can always use request.user. In particular, you can pass it as a part of the context for generating a view. Hope this helps.
Answer: from django.contrib.auth.models import User
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)

Associate user to a post in Django


Tips for implementation of the spring-oauth RandomValueProviderTokenServices class

Question: I need to implement the oauth spring persistence for RandomValueProviderTokenServices class.
The class that needs to be stored according to the abstract protected methods of the RandomValueProviderTokenServices is OAuthProviderTokenImpl. however, this class contains a reference to Authentication interface which has various implementations.
I assume that implementations of these methods were done by any who used the spring-oauth library for their projects.
Is there a common practice to achieve that? ( without using the Java built it serialization mechanism).
Answer: I am working on this now and I am just storing a reference to the user in the database using their id. Then I generate the Authentication object based on that user object using this:
public Authentication getAuthentication(User user) {

 Object credentials = user.getUsername(); // the username of your user
    GrantedAuthority[] authorities = {new GrantedAuthorityImpl(user.getRole().getName())}; // an array of their role names

    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, credentials, authorities);
    auth.setDetails(user);

    return auth;
}

Comment: Unfortunately no, I used the built in java serialization mechanism.


What properties does a rank-$1$ matrix have?

Question: I have seen a lot of papers mentioning that a certain matrix is rank-$1$. What properties does a rank-$1$ matrix have?
I know that if a matrix is rank-$1$ then there are no independent columns or rows in that matrix.
Comment: The section "Properties" at (http://en.wikipedia.org/wiki/Rank_(linear_algebra) gives a good account of the properties (e.g., Sylvester's rank inequality).
Answer: Any rank one matrix $A$ can always be written $A=xy^\intercal$ for vectors $x$ and $y$. More precisely...

Proposition: A matrix in $\mathbb{C}^{n\times n}$ has rank one if and only if it can be written as the outer product of two nonzero
  vectors in $\mathbb{C}^{n}$ (i.e., $A=xy^{\intercal}$).

Proof. This follows from the observation
$$
\begin{pmatrix}x_{1}y^{\intercal}\\
x_{2}y^{\intercal}\\
\vdots\\
x_{n}y^{\intercal}
\end{pmatrix}=xy^{\intercal}=\begin{pmatrix}y_{1}x & y_{2}x & \cdots & y_{n}x\end{pmatrix}.
$$

Corollary: The eigenspace of a rank one matrix in $\mathbb{C}^{n\times n}$ is one dimensional.

Proof. If $A$ is a rank one matrix in $\mathbb{C}^{n\times n}$, the previous result tells us that it can be written in the form $A=xy^{\intercal}$. Next, note that for any vector $w$ in $\mathbb{C}^{n}$,
$$
Aw=(xy^{\intercal})w=(y^{\intercal}w)x.
$$
Since $(y^{\intercal}w)$ is a scalar, it follows that if $w$ is
an eigenvector of $A$, it must be a multiple of $x$. In other words,
the eigenspace of $A$ is
$$
\operatorname{span}(x)=\left\{ \alpha x\colon\alpha\in\mathbb{C}\right\} .
$$
Comment: Is there also a geometric interpretation of the linear transformation associated with $A = xy^T$?
Comment: @philmcole: according to the answer, $Aw$ is always a scalar multiple of the vector $x$. The scalar multiple depends on the length of $y$ and $w$ and the angle between them (see https://en.wikipedia.org/wiki/Dot_product).
Comment: Does the transpose change to conjugate transpose in case of C


Prove $(\tan^{-1}{x})' = \frac{1}{1+x^2}$

Question: Prove $(\tan^{-1}{x})' = \frac{1}{1+x^2}$
How should I proceed? I tried:

Then another which I think is wrong:

What should I be doing?
UPDATE
In my lecture notes ... maybe a typo:
Comment: It's always hard to prove a false statement. Don't you think that derivative of $\tan$ is sometimes big? Btw. the first is correct.
Comment: Is this a joke?
Comment: In fact $(\arctan x)'=(\tan^{-1} x)'=1/(1+x^2)$, not $\tan$.
Comment: Try to prove $\frac{d}{dy} \arctan y = \frac{1}{1+y^2}$ by letting $y = \tan x$ and using your first result.
Comment: Surely a typo in your lecture notes.
Answer: I guess that you want to prove that $(\tan^{-1} x)'=1/(1+x^2)$.
$$ (\tan^{-1} x)'=\frac{1}{\tan' y}$$
where $\tan y=x$. Therefore
$$ (\tan^{-1} x)'=\frac{1}{1+\tan^2 y}=\frac{1}{1+x^2}$$
Answer: The answer above is also correct, but for clarity here it goes
Let $$y=\tan^{-1} x \Rightarrow \tan(y) = x$$
Differentiate both sides:
Left side will be $\sec^2(y) \dfrac{dy}{dx}$, Right side will be $1$
But using the trigonometric identity $\sec^{2}(y) = 1 + \tan^{2}(y)$
$$\begin{align}
(1+\tan^{2}(y))&\frac{dy}{dx} = 1 \\
\Rightarrow& \frac{dy}{dx} = \frac{1}{1+\tan^{2}(y)}
\end{align}$$
Since we began with $y=\tan^{-1} x \Rightarrow \tan(y) = x$
$$\frac{dy}{dx} = \frac{1}{1+x^2}$$
But $\dfrac{dy}{dx} = (\tan^{-1} x)'$, Therefore $$(\tan^{-1}x)' = \frac{1}{1+x^2}$$
Comment: A minor quibble: you shouldn't use expressions such as "the answer above..." because what was above at the time of your writing may not be above at the time of our reading :)
Answer: It should be $(\tan^{-1}x)'$ (or arc$\tan x$). Then use $$y=\tan^{-1} x$$
$$\Rightarrow \tan y= x$$ Now differentiate both sides w.r.t. $x$ to get $sec^2 y. y'=1$, or $(1+\tan^2 y).y'=1$, or $(1+x^2).y'=1$
Answer: Has your class covered integration?  It may be easier to prove that $\int\frac{dx}{1+x^2}=\tan^{-1}x+C$.  To clear up the denominator, try the substitution
$x=\tan\theta,dx=\sec^2\theta d\theta$
$\int\frac{dx}{1+x^2}=\int\frac{\sec^2\theta d\theta}{1+\tan^2\theta}=\int\frac{\sec^2\theta d\theta}{\sec^2\theta}=\int d\theta=\theta+C$
Since $x=\tan\theta, \theta=\tan^{-1}x$


Comparing k-means results to original data: how to interpret the resulting plots?

Question: I'm running k-means on my dataset that can be found here that has 7 classes.
I plotted the ggpairs for the dataset and then took k-means and plotted ggpairs again in an effort to see what effect it has.
The two plots are below, but I'm not sure how to interpret them.
How should I compare the original plot to the one against k-means (7 clusters) plot? What sort of information can I extract out of them?
Plotting actual data (7 classes)

Plotting k-means with 7 clusters

Code for the plots
#original data plot
wine <- read.csv("wine_nocolor.csv")
library(GGally)
wine1 <- wine[2:13]
wine1$q <- factor(wine1$q)
ggpairs(wine1, columns=1:11,
        colour='q',lower=list(continuous='points'),
        axisLabels='none',
        upper=list(continuous='blank'), title="Actual Wine Quality")
#k-means with 7 clusters
set.seed(1234)
wine1$Cluster7 <- factor(kmeans(wine[1:11], 7)$cluster)
ggpairs(wine1, columns=1:11, colour='Cluster7', lower=list(continuous='points'), axisLabels='none', upper=list(continuous='blank'), title="k-means with 7 clusters")

Comment: Just to clarify @ttnphns' advice: he refers to the [external evaluation](http://en.wikipedia.org/wiki/Cluster_analysis#External_evaluation) of a cluster analysis. I also think that an _analytical approach_ makes sense here.
Comment: I think I see another issue. You are k-means clustering with columns 1:11. In the dataset you've provided, that includes the id column, and ignores the 'a' variable. Am I wrong?
Comment: @danno, Look at the fourth line of the provided code.
Comment: @ amoeba Yea, that clears part of it up - but what about where OP is creating the kmeans clusters? OP uses wine[1:11], not wine1[1:11]...
Comment: The question isn't clear so far. What purpose did you do cluster analysis for? Why do need to compare the pictures? Can you draw any interpretation from the first picture, what does it tell you?
Comment: @danno: Hmm, yes, this does look like an error.
Comment: @ttnphns **Purpose**: By clustering I want to see how effectively can I classify data as correct labels. **Need**: I want to see if k-means helped at all on the dataset or whether clustering the data was helpful. **Interpretation** I can deduce that the `a` (alcohol) attribute varies a lot for wine quality compared to other attributes. But it doesn't vary as much after k-means. Now, for example, I'm not sure whether that is good or bad.
Comment: `how effectively can I classify data as correct labels` For that, you don't need these plots at all. See Wikipedia (Cluster analysis: external validation).
Answer:
How should I compare the original plot to the one against k-means (7 clusters) plot? What sort of information can I extract out of them?

You can extract the information that the k-means results are garbage.
If you look at your second figure, you will see that the clustering is based almost exclusively on the tsd variable: your 7 clusters  are basically 7 ranges of tsd values.
The reason this happens, is that tsd has largest numerical values. K-means operates on Euclidean distances and is highly sensitive to normalization. Having one variable that is on a much larger scale than all the others, can completely bias the clustering.
I think we can safely conclude that this particular analysis is useless.

Let me add that as you have the quality of each wine (your 7 "classes", which are in fact levels of an ordinal variable) explicitly available in your dataset, you should probably be doing some sort of regression (such as e.g. ordinal logistic regression), and not clustering. I cannot see any possible value of trying to cluster this dataset.
Answer: The scale for each variable is quite different, and this affects the cluster analysis, eg
> summary(wine1[,1:5])

      fa               va               ca               rs               ch
 Min.   : 3.800   Min.   :0.0800   Min.   :0.0000   Min.   : 0.600   Min.   :0.00900
 1st Qu.: 6.400   1st Qu.:0.2300   1st Qu.:0.2500   1st Qu.: 1.800   1st Qu.:0.03800
 Median : 7.000   Median :0.2900   Median :0.3100   Median : 3.000   Median :0.04700
 Mean   : 7.215   Mean   :0.3397   Mean   :0.3186   Mean   : 5.445   Mean   :0.05603
 3rd Qu.: 7.700   3rd Qu.:0.4000   3rd Qu.:0.3900   3rd Qu.: 8.100   3rd Qu.:0.06500
 Max.   :15.900   Max.   :1.5800   Max.   :1.6600   Max.   :65.800   Max.   :0.61100

Also a couple of the variables have skewed distributions which makes it hard to separate using clustering. You should transform the skewed variables and standardize the variables, and then run k-means for a better clustering.
Here is the revised code:
wine <- read.csv("data/wine_nocolor.csv", stringsAsFactors=FALSE)
library(GGally)
library(reshape)
library(ggplot2)
library(dplyr)
wine1 <- wine[,2:13]
wine1$q <- factor(wine1$q)

wine1$va <- log10(wine1$va)
wine1$rs <- wine1$rs^(0.25)
wine1$ch <- log10(wine1$ch)
wine1$fsd <- wine1$fsd^(0.25)
wine1$s <- log10(wine1$s)
set.seed(1234)
wine1$Cluster7 <- kmeans(rescaler(wine1[1:11]), 7)$cluster
wine1$Cluster7 <- factor(wine1$Cluster7, levels=1:7,
                         labels=letters[1:7])
table(wine1$q, wine1$Cluster7)

wine1.m <- melt(wine1[,c(1:11,13)])
qplot(Cluster7, value, data=wine1.m, geom="boxplot", colour=Cluster7) +
  facet_wrap(~variable, scales="free_y") + theme(legend.position="bottom")

The results look much better now. You can see that the clustering has divided the data into reasonable chunks, eg cluster c has high values on fa, ... The data has no natural clusters, so k-means is simply breaking the data into roughly equal chunks. The clusters do not match the original classes, but the original classes don't separate the data at all.
Answer: I basically agree with amoeba's comment but it doesn't offer much in the way of a solution.
What I would do is first drop q from the inputs of the clustering.  Secondly, you should standardize your variables to mean zero and unit variance.  Now if you try your k-means again, it will likely not be as heavily dependent on tsd.  However, you may also want to try other algorithms such as Gaussian Mixture Models or Affinity Propagation.  Afterwards you can summarize your clusters by your ordinal variable.
But this plotting approach is not the best for clustering, in my arrogant opinion.  I usually plot the data on the first two or three PCA coordinates.  Doing the PCA can also tell you a lot about the variables.  For instance, if you had done it on this dataset ahead of time you would have seen that 1) the first PC dimension dominates the clustering and 2) tsd is very highly correlated with the first PC.
Clustering is never an exercise in perfection, but keep at it and see if you get a result that is useful.
Comment: Reasonable suggestions (+1), but note that `q` was not used for clustering in the code example and figures provided by OP.
Answer: Focus on the tsd column only for now. As you can see from the stripes only this attribute has an effect on your clustering. You simply cut your data set into slices based on tsd.
This is because you did not preprocess your data well. The attributes have different scale. And some appear to be nonlinear. k-means only works if your attributes are comparable and linear. I.e. a difference of 1 must be the same in every attribute and at every location (0 to 1 must bear the same significance as 1 million to 1 million + 1)
You could compute for example the ARI (adjusted Rand index) to compare the clustering result to the known classes. But it will score really badly, because you skipped the first step: preprocessing your data.
Answer: Since your question was "to see what effect it has" or interpret k-means vs. raw data plots, rather than whether this was the best way to analyze or visualize the data, here's my try: k-means clustering hints at stronger trends between e.g. fa and va, ca, p in the last-to-be-mapped pink cluster, which may benefit from further examination. If you make density plots for each cluster, other such trends may become evident, if the goal is visual trend scanning.
k-means clustering seems to be based almost exclusively on tsd. However, it seems almost too good to be true, so it is more likely to be an a priori known grouping.
Comment: I am not sure it is that different from what you are saying with "your 7 clusters are basically 7 ranges of tsd values". Also, I did not look at the data so "almost' because of data overlay/may not be seeing the complete picture. And I was trying to be nice:) But, your explanation is better - I did not see the data range issue - so no worries about downvote.
Comment: I removed the downvote and the above comments, but I am still confused by your last sentence (*"it is more likely to be an a priori known grouping"*): it does not make sense to me.
Answer: I'll first discuss comparison methods, but I would also like to make a point:
In order to compare the k-means to the actual data, you should compare the distributions, since k-means is ultimately giving you an estimate of the distribution (see below). To compare the distributions, you should use the notion of "divergence" between the actual and predicted distributions. In this case, you can use the KL-divergence (aka the relative entropy) between the distributions for each class.
Since your distributions are discrete, you can use the following equation.
Given distributions $P$ and $Q$, the KL-divergence $D_{KL}(P||Q)$ is defined as follows
$$
D_{KL}(P||Q) = \sum_i P(x_i)\log_2\frac{P(x_i)}{Q(x_i)}
$$
Intuitively, this can be thought of (loosely) as a metric of the information lost when using $Q$ to try and predict $P$. In this case, $P$ is the actual distributions and $Q$ is the k-means distribution. In other words, how much information is lost when you try to use k-means to predict the actual data. The remarkable thing about the KL-divergence is that it gives you an unbiased estimate of the divergence. It can also be extended to multiple dimensions with the Renyi Divergence, but I wouldn't worry about that for your data.
Note that the base of the log is arbitrary - I used base 2 since these ideas come form information theory.
You can compare these plots by plotting the KL-divergence for each class for each plot.

Honestly, I don't think k-means is appropriate here. k-means makes a few important assumptions, which helps explain why you cannot extract the correct distribution in this case:

k-means assumes that the variance of the distribution of each class is spherical (for example, gaussian)
all classes have the same variance
the prior probability of each cluster is equal (equal number of observations)

You can see clearly that the first and second statements do not hold, so k-means will fail. You could, on the other hand, try single linkage hierarchical clustering. Here is a brilliant article about what this means in practice: http://varianceexplained.org/r/kmeans-free-lunch/
Let me know if you have any questions and I can elaborate.
Answer: This is a little vague, because k-means is meant to cluster, rather than to compare clusters to known groups. You might want to look into another technique. If you are still hooked on k-means clustering, you could always perform some sort of ordination, such as nMDS or RDA, and plot points to visualize both a priori and a posteriori (k-means) groups, as a start.
Another approach, which might be valuable to you, is to use LDA to construct a statistical model of your groups. With this technique you can see how well LDA predicts your a priori groups, and what variables contribute most to the formation of those groups. Unlike k-means, it is not so important that you standardize your data before hand (in response to amoeba's interpretation to your k-means results).
Good luck!

As an example, see the following:
library(MASS)
wine.lda <- lda(as.factor(q)~fa+va+ca+rs+ch+fsd+tsd+d+p+s+a, wine_nocolor)

plot(wine.lda)

wine.predict <- predict(wine.lda, newdata = wine_nocolor[2:12])

table(wine.predict$class, wine_nocolor$q)
sum(diag(prop.table(table(wine.predict$class, wine_nocolor$q) )))

I got an accuracy of about 54%. You can look at the summary of the LDA results to pick out the major players. I believe in this case it is "d"... possibly due to very low variance in group 9. As opposed to what I said earlier, column scaling the data might give you nicer looking eigenvectors, however it won't change the model's accuracy.


LMM for repeated-measures

Question: I want to perform a repeated measures analysis on an unbalanced dataset. To keep it simple, assuming I have activity level (AL) as my response variable, time  as my repeated measures.
Additional factor don't matter for now.
My time variable is not continuous, but I measured for 3 different stages during an intervention. My goal is to draw conclusions about changes in AL from one stage to another. As far as I understand, the LMM would tell me if 'time' has an influence on the AL in general. But I need to know whether there is a significant change between any of my 3 single time points (e.g. between stage 1 and stage 2 etc..). What would be the correct approach to do so? Do I need to perform multiple-comparisons for each stage?
Comment: I don't have time to give a full answer, but I can at least point you in the right direction (or at least point you to what you might want to Google). If your interest is not in knowing whether there is a change over time, but rather whether there's a difference between 1 and 2, 2 and 3, and 1 and 3, then you'd want to treat time as a factor with 3 levels, and then use _contrasts_ to perform the multiple comparisons that you want.
Answer: Yes, you can use a linear mixed model and then perform multiple comparisons. One way to do this in R, with lmer from the lme4 package and glht from the lsmeans package, is:
> require(lme4)
> m0 <- lmer(effort ~ 1 + Type + (1|Subject), data=ergoStool, REML = 0)
> summary(m0)

Fixed Effects:
(Intercept)       TypeT2       TypeT3       TypeT4
     8.5556       3.8889       2.2222       0.6667

Here the estimates for each level of Type are contrasts with the baseline level (T1). To get pairwise comparisons between other levels we can use:
> require(lsmeans)
> summary(glht(m0, lsm(pairwise ~ Type)))

which outputs the desired contrasts:
Linear Hypotheses:
             Estimate Std. Error t value Pr(>|t|)
T1 - T2 == 0  -3.8889     0.4890  -7.952  < 0.001 ***
T1 - T3 == 0  -2.2222     0.4890  -4.544  < 0.001 ***
T1 - T4 == 0  -0.6667     0.4890  -1.363  0.53140
T2 - T3 == 0   1.6667     0.4890   3.408  0.00956 **
T2 - T4 == 0   3.2222     0.4890   6.589  < 0.001 ***
T3 - T4 == 0   1.5556     0.4890   3.181  0.01689 *

LMM for repeated-measures


How to set up news page view so that news/topic shows news pieces with topic (which is an entity reference field)?

Question: I have a news page set up as a page display, showing nodes of a specific Content Type. /news works as expected.
I now want to improve the view: When someone goes to /news/pancreas or /news/heart, for example, nodes are filtered for that particular topic.
I have an entity reference field (Groups audience) whose machine name is og_group_ref, which allows unlimited categories.
For my display mode, I am outputting that field as an ID (although I'm not sure this is correct). For results without a topic on the view page they look something like the following screenshot (with ID of category shown).

However if I add a category to the URL, such as /news/heart, I do not get any results.
I have a contextual filter and a relationship set up from trying to follow a guide elsewhere, but I do not think it is correct and I'm sadly pretty fuzzy on how contextual filters and relationships work. I am not really sure I'm even on the right track.

How can I set up categorization based on an entity reference field?
Comment: A contextual filter is a good way to do this.  How did you configure the contextual filter?
Comment: @hotwebmatter YES it is a migration attempt!!!  I didn't realize this made a difference.  I didn't do the migration but I understand we have one field that is migrated from organic groups and one field that is new under d9.  I think the migrated D7->D9 field is the preferred one we want to use.  Thank you so much for putting this on my radar that it makes a difference.  Any idea how to set up the context/relationship for the migrated info?
Comment: @PatrickKenny I actually attempted to replicate the D7 view configuration and found the d7 context relationships were not available in D9, meaning something significant has changed between d7 and d9.  So I tried a bunch of different options for contextual filters and relationships and could not find any that would successfully pull term-specific related content :/
Comment: Does your "Groups audience" entity reference point to a Group, or an Organic Group, or a taxonomy term? Details matter! It looks like your project may be a D7 -> D9 migration from Organic Groups to the newer Group module, but that isn't clear from your question.
Answer: Judging by the name of the "og group" field, the Drupal 7 site probably used Organic Groups and the Drupal 8 site probably uses Group.
These modules have completely different implementations, so it's important to know which one you're working with when you configure your View -- and not to mix the old way with the new way.
It seems likely that the "og group" field contains legacy data from Drupal 7 Organic Groups. (It's common in migrations to create fields to preserve important data from the old site for the historical record, even if it is not used anymore in the new site.)
The OG group reference for "heart" has ID 46, but this is likely just a taxonomy term reference rather than a full OG implementation. (OG for Drupal 9 is currently under heavy development; I would not recommend installing both OG and Group on the same site.)
The "Groups audience" field refers to the new Group implementation, where "Heart" has ID 13366. This is the Group which matters for your Views contextual filter. But your View also has a Relationship with og_group_ref so you may be mixing things here.
Your View may not need a Relationship at all, so to simplify debugging I would remove that and concentrate on your contextual filter.
You may find that it's relatively straightforward to set up a contextual filter on Groups Audience, as long as the URL contains the Group number rather than its label. (In other words, try news/13366 instead of news/Heart.)
If you are certain that all Groups will always have a corresponding og_group_ref, you could instead opt to use a contextual filter on that taxonomy term reference, as long as the URL contains the taxonomy term ID rather than its label. (In other words, try news/46 instead of news/heart.)
Once you get the contextual filter working, I recommend you post another question to see how you can make this filter work with the word heart in the URL segment rather than the number 13366. It's a separate issue.
Comment: Great!!  That helped a bunch, I was indeed able to add a contextual filter to field_group (from organic groups) and now can filter by organic group category.  I'll see if I can work out the aliasing (news/heart instead of news/46) and will post again if I can't figure it out.  Thanks again!

(PS. I was just recently brought on to this project and see they are indeed using both D9 groups and D9 organic groups).  Just curious why you recommend not mixing the two, just for the inherent confusion in using two overlapping modules?
Comment: For anyone else struggling with this, I found some valuable information here: https://www.drupal.org/project/drupal/issues/2915934
Specically: " But if you add the **Relationship** for the taxonomy reference field you are making all the term data available to this View!

So after you have added the Relationship for the taxonomy term into your Content Views, the **Name** Contextual filter becomes available it's in the Taxonomy term Category."

Long story short, after I added a relationship to my organic group some additional contextual filter options showed up.
Comment: PS. Bonus points if you can point me to an article documenting how to show the entity's text/alias instead of number. :)
Comment: @PatrickMan Glad you got it working! Feel free to post another question about using the entity label instead of the ID number, and I'll see if I can help with that as well (the answer is too long for a comment!)
Comment: @PatrickMan In case you did not figure out how to use `news/heart` instead of `news/46`, you might find that aliasing the numerical path gets your what you want. Anyway, that's one quick and dirty approach. Drupal hides this configuration because it might not work -- different ID numbers may be labeled with the same string.


Estimation of ARMA: state space vs. alternatives

Question: I am interested in estimation of ARMA models. I understand that a popular approach is to write the model down in the state space form and then maximize the likelihood of the model using some optimization routine.
Question: Why rewrite the model into its state space representation and maximize the corresponding likelihood -- instead of maximizing the "naive" or "direct" likelihood?
(I could imagine that a different parameterization can make the optimization easier -- is that the case here?)
Related questions:

"Comparison of estimation techniques for ARIMA model"
"What state-space representation of VARMA is commonly used for fitting"
"What is the requirement for Kalman filters"

I am also aware of some general advantages and disadvantages of the state space representation as mentioned in "What are disadvantages of state-space models and Kalman Filter for time-series modelling?".
Comment: Simple reparametrizations are used in state space models in order to simplify estimation procedures as described in Durbin Koopman's and A. Harvey's books. Also in comparison to ARIMA, state space parametrization is richer as ARIMA parametrization is the reduced form of state-space and doesn't take into account the convergence to steady state.
Comment: @Plissken ...which is what I am trying to say here *write the model down in the state space form and then maximize the likelihood of the model* [due to that form] *using some optimization routine*. So I understand that likelihood maximization is inevitable. The question is, is the direct likelihood and the likelihood of the state space representation the same or different? And if they are different, is that due to reparameterization which makes one of the likelihood functions easier to maximize?
Comment: @CagdasOzgenc, oh, so then the change (improvement?) in estimation due to the state space (SS) representation is actually coming from reparameterization? That would explain some of my confusion. Could you give a very simple example of how the parameterization changes from direct likelihood to SS? E.g. in a simple AR(1) model I thought the model parameters are directly put into the SS without any changes (e.g. the slope $\varphi_1$ remains as is, it does not become some, say, $2\theta+1$ or whatever). But maybe I overlooked something.
Comment: Have you looked at these lecture notes: http://www.kris-nimark.net/pdf/Handout_S4.pdf ? Even if you write the model in state space form you will need to filter through the data using (e.g.) the Kalman filter. The prediction error can then be used to construct a likelihood function which needs to be maximized. So you will need to maximize a likelihood function one way or the other.
Comment: Not a full answer but: 1) alternatives don't always optimize the same likelihood, it's usually a conditional likelihood on the first few observations, whereas in state-space exact diffuse initialization is available, and 2) Kalman filter provides recursion for the likelihood naturally but also a recursion for the gradient as a by-product, which is useful for optimisation.
Comment: @ChrisHaug, it is possible to obtain exact/full MLE by specifying marginal densities for the initial values and including these in the likelihood function although these two are asymptotically equivalent.
Comment: @ChrisHaug, as long as the likelihood is parameterized in the same way, treatment of the initial value seems of minor importance, unless in small samples. But is the likelihood function the same? And if not, it must be due to different parameterization (since the model is still the same, just the parameterization may differ). And that would make me revise my impression than the parameterization is the same in the direct vs. the state space likelihood function.
Comment: @RichardHardy, interesting question which I am gonna read up on. According to this article: http://www.lse.ac.uk/statistics/documents/researchreport40.pdf their parametrisation has computational advantages but these are related to the Kalman filter recursions. Note, that since the state space approach and the direct MLE give the same (or very similar) parameter estimates the likelihood function should be the same. If it wasn't we wouldn't get the same estimates.
Comment: @Plissken, thanks. I read the lecture note. It requires some effort, just like many other lecture notes on state space modelling that I have read earlier, and I am not getting all the insight I am wishing for. I was hoping someone here could give me the intuition instead. Now regarding direct likelihood vs. state space, of course the estimates should be the same -- because we are talking about two equivalent representations of a single model (the same model). Differences in estimates could results from poor optimization (getting stuck in local optima, etc.), but hopefully we can avoid that.
Answer: If you manage to use a Kalman filter, you can marginalize or optimize out the state at each time analytically. Thus the remaining likelihood is much simpler, having only the ARMA process variables, i.e., tens of parameters.
If you use the direct variables, you have one (or more) parameters per state, so if your time series has 1000 entries, you have a 1000-dimensional likelihood.
High-dimensional spaces are hard to explore.
Comment: But the direct likelihood only has p+q+2 parameters + some initial values. Why should it be 1000-dimensional?
Comment: what is a direct likelihood for you? At each time, something unknown/random happens, how would you treat that? I'd say it is a free parameter of the likelihood
Comment: Forget "direct", it is just the likelihood of ARMA. It is a function of the parameters and some initial values, that is all. The "something unknown/random" at each time is not a free parameter, as its value is determined by the AR and MA parameters, the initial conditions (treated as parameters) and the data. (Similarly, in a simple cross sectional regression, each observation has an associated error, but that error is not a free parameter in the likelihood.)
Comment: See the comments by mlofton near the bottom of this thread. I claim zero knowledge-understanding of Manfred's work but it sounds like using standard numerical optimization algorithms for ARIMA estimation is problematic. Even state space transformations are not the be-all end-all solution. Desitler's material is very difficult ( I don't recommend his 88 book with Hannan ) but he has a new text, "time series models", that looks approachable at a glance. It's definitely  more introductory than the 88 book but the term "introductory" is relative.
Comment: I forgot to include the thread I was referring to. https://stats.stackexchange.com/questions/617564/let-x-t-be-an-arima1-1-1-process-and-y-t-y-t-1-x-t-what-kind-of-pro
Comment: "The something unknown/random at each time is not a free parameter, as its value is determined by the AR and MA parameters" -- no, you do not know the randomness added at each time step. An ARMA prediction is not deterministic from the parameters and initial value.


How to minify code structure without using if else statement

Question: I would like to ask if there is a better way to minify the code below without using if else statement. I have tried using switch case but it does not seem to work:
function (inputValueTime){
if (inputValueTime === 'thirty') {
return 1800000;
}else if (inputValueTime === 'fortyFive') {
return 2700000;
}else if (inputValueTime === 'oneHour') {
 return 3600000;
}else if (inputValueTime === 'oneHalf') {
return 5400000;
}else if (inputValueTime === 'oneQuarter') {
return 6300000;
}else if (inputValueTime === 'twoHours') {
return 7200000;
}

Comment: What happens with the switch statement?
Comment: Actually, you don't have a name for your function. Should be something like function getInputValueTime(inputValuetime){... if else here}
Answer: I would use an object indexed by inputValueTime, whose values are the associated numbers:
const times = {
  thirty: 1800000,
  fourtyFive: 2700000,
  oneHour: 3600000,
  oneHalf: 5400000,
  oneQuarter: 6300000,
  twoHours: 7200000
};

function foo(inputValueTime) {
  return times[inputValueTime];
}

This will return undefined for inputValueTimes which are not in the object, just like your original code, for the most part - if inputValueTime can have Object.prototype properties (such as 'isPrototypeOf'), then use a hasOwnProperty check as well.
Comment: This is a cool solution but it does require the introduction of another variable
Answer: Why do you say a switch clause does not work ? Look at the code

function sample(inputValueTime) {
  switch (inputValueTime) {
    case 'thirty':
      return 1800000;
    case 'fortyFive':
      return 2700000;
    case 'oneHour':
      return 3600000;
    case 'oneHalf':
      return 5400000;
    case 'oneQuarter':
      return 6300000;
    case 'twoHours':
      return 7200000;
    default:
      return -1;
  }
}

console.log(sample("thirty"));
console.log(sample("fortyFive"));
console.log(sample("oneHour"));
console.log(sample("oneHalf"));
console.log(sample("oneQuarter"));
console.log(sample("twoHours"));
Answer: You should be able to use a switch statement.
function (inputValueTime) {
  switch (inputValueTime) {
    case 'thirty':
      return 1800000
    case 'fortyFive':
      return `270000
    case 'oneHour':
      return 3600000
    case 'oneHalf':
      return 5400000
    case 'oneQuarter'
      return 6300000
    case 'twoHours':
      return 7200000
  }
}


measures to take against a dns amplification attack

Question: I recently discovered that my server was being used as part of a DNS DDOS. Basically, my BIND setup allowed recursion, and it was used to attack a certain IP address using IP spoofing.
I took the necessary measures to stop this, and disabled recursion. I'm no longer an amplifier, which I guess solves the large problem, but I'm still receiving the queries en mass, and BIND is replying with "denied" to all of them.
I'm just curious to know whether there's anything more I can do. I thought I might configure fail2ban to block them, doing something similar to the Debian recommendations, but according to other websites, and reasonable logic, this is not ideal, as an attacker could easily have me block any IP from accessing my server.
So what else can be done? Or should I just wait for the attackers to give up? Or hope they might rescan and delist me as an amplifier?
Comment: If, your attack traffic targets a number of known IPs, then it's ok to blacklist them - either in iptables or inside the bind, don't need even fail2ban. Also see this question - http://serverfault.com/questions/438515/bind-blackhole-for-invalid-recursive-queries Also there's experimental rate-limiting patch for bind - http://www.redbarn.org/dns/ratelimits .
Answer: Basically, the fail2ban setup described in the linked article modifies the firewall to DROP (for a limited time) incoming DNS queries from source IPs that are not allowed to query your DNS server. Not a bad idea, but if you're not providing authoritative DNS for one or more domains to the internet, then why not forget about fail2ban, and just DROP all incoming DNS queries from the internet altogether?
If you are running an authoritative DNS server, then, unfortunately, you don't have the luxury of ignoring DNS queries. In that case, I don't think you have much choice but to leave things as you have them (recursion off), and patiently wait for a gradual drop-off in the incoming spoofed traffic. It sure would be nice to be able to configure bind itself to silently ignore queries that it's configured not to answer, but I don't think it has that feature. (Such behaviour would technically violate DNS protocol, after all.) Fail2ban does provide something of an alternative, but as you noted, it's not ideal.
Comment: Thanks a lot. Yes, I am running an authoritative DNS server, and as you summed it, i guess I'll just have to wait.


SagePay responsive card selection page

Question: I have been given a task to update our current SagePay card selection page to responsive design.
In their integration and protocol guidelines document there are screenshots of responsive card selection page but I can't seem to find any information on how to set it.
I have also tried their PHP integration kit that is located here but the card selection page doesn't seem to be responsive.
Has anyone come across this?
Comment: Please share your code what you've tried so far.
Answer: You will need to be sending transactions over at a protocol level of 3.00 for this functionality.
Assuming you are, log into My Sage Pay, then click the Settings Tab (at the top r/hand side, assuming the user account has privileges to adjust account settings). Then click 'Settings' on the l/hand panel (confusingly!). In the bottom l/hand corner, you have the 'Account Settings' pane.
If you click Edit here, you can then change the page template. Annoyingly, the 'Edit' button might require you to scroll the page down, but it is there.

SagePay responsive card selection page


How to select table name dynamically in Linq or Entity Framework

Question: I want to call controller method for data in that there can be multiple data output from different table so it depends on user selection from which table they wanna to take output and so i was thinking to get output using single linq just by passed name of table according to user selection.
           query = (from Ledger in fen.TblLedgers
                     join acc in fen.Accounts
                     on Ledger.Code equals acc.AccId
                     where acc.DrCr == Fin.DRCR && Ledger.Grp == Fin.LedGrp  && acc.Vdate <= DateTime.Today && Ledger.isActive == "Y" group acc by new { acc.AccId } into g select g.Sum(acc => acc.Amt)).Sum() ;

Comment: You can't. Not out of the box, its techically possible, but you need to mess with `System.Linq.Expressions.Expression`s. Not easy. However it sounds like a code smell to me, wanting to dynamically select the table.
Answer: If you want to do this, one approach would be to use lambdas and implement something like the below.
Long and short of it - you'd need to leverage the DbContext.Set < T > method DbContext.Set method (MSDN) or the generic one (MSDN)
Vary T (or the type parameter) by the users choice, and then lambda from there.
However, in order to do this, all T's would need to implement the same interface and you'd need to cast the elements to that interface before using them. But, you'd have to do this if you want any of this stuff to work at all...)
(Code below written freehand, don't complain if there's the odd mistake!)
So - with classes:
public interface IAnimal
{
    ....
}
public class Dog :IAnimal
{
    ....
}
public class Cat :IAnimal
{
    ....
}
public class Cow :IAnimal
{
    ...
}

You could use something like:
var userSelection = "Dog";
Type chosenType = chooseTypeAccordingToUserSelection(userSelection);
var animalDBSet = GetOpenDBcontext().Set(chosenType).Cast<IAnimal>();
var insects = animalDBSet.Where(a=>a.NumberOfLegs>4);

HTH!


how can I match all the key value pair in python which running too long

Question: User-item affinity and recommendations :
I am creating a table which suggests "customers who bought this item also bought algorithm "
Input dataset
productId   userId
Prod1        a
Prod1        b
Prod1        c
Prod1        d
prod2        b
prod2        c
prod2        a
prod2        b
prod3        c
prod3        a
prod3        d
prod3        c
prod4        a
prod4        b
prod4        d
prod4        a
prod5        d
prod5        a

Output required
Product1    Product2    score
Prod1       prod3
Prod1       prod4
Prod1       prod5
prod2       Prod1
prod2       prod3
prod2       prod4
prod2       prod5
prod3       Prod1
prod3       prod2

Using code :
#Get list of unique items
itemList=list(set(main["productId"].tolist()))

#Get count of users
userCount=len(set(main["productId"].tolist()))

#Create an empty data frame to store item affinity scores for items.
itemAffinity= pd.DataFrame(columns=('item1', 'item2', 'score'))
rowCount=0

#For each item in the list, compare with other items.
for ind1 in range(len(itemList)):

    #Get list of users who bought this item 1.
    item1Users = main[main.productId==itemList[ind1]]["userId"].tolist()
    #print("Item 1 ", item1Users)

    #Get item 2 - items that are not item 1 or those that are not analyzed already.
    for ind2 in range(ind1, len(itemList)):

        if ( ind1 == ind2):
            continue

        #Get list of users who bought item 2
        item2Users=main[main.productId==itemList[ind2]]["userId"].tolist()
        #print("Item 2",item2Users)

        #Find score. Find the common list of users and divide it by the total users.
        commonUsers= len(set(item1Users).intersection(set(item2Users)))
        score=commonUsers / userCount

        #Add a score for item 1, item 2
        itemAffinity.loc[rowCount] = [itemList[ind1],itemList[ind2],score]
        rowCount +=1
        #Add a score for item2, item 1. The same score would apply irrespective of the sequence.
        itemAffinity.loc[rowCount] = [itemList[ind2],itemList[ind1],score]
        rowCount +=1

#Check final result
itemAffinity

the code is running perfectly fine on a sample dataset but
The code is taking too long to run in dataset containing 100,000 rows. Please help me optimize the code.
Comment: I guess `userCount=len(set(main["productId"].tolist()))` should be `userCount=len(set(main["userId"].tolist()))`
Comment: Second method provided.
Answer: Yes, algorithm could be improved. You are recalculating user list for items in inside loop multiple times. You can just get a dictionary of item and their users outside loops.
# get unique items
items = set(main.productId)

n_users = len(set(main.userId))

# make a dictionary of item and users who bought that item
item_users = main.groupby('productId')['userId'].apply(set).to_dict()

# iterate over combinations of item1 and item2 and store scores
result = []
for item1, item2 in itertools.combinations(items, 2):

  score = len(item_users[item1] & item_users[item2]) / n_users
  item_tuples = [(item1, item2), (item2, item1)]
  result.append((item1, item2, score))
  result.append((item2, item1, score)) # store score for reverse order as well

# convert results to a dataframe
result = pd.DataFrame(result, columns=["item1", "item2", "score"])

Timing differences:
Original implementation from question
# 3 loops, best of 3: 41.8 ms per loop
Mark's Method 2
# 3 loops, best of 3: 19.9 ms per loop
Implementation in this answer
# 3 loops, best of 3: 3.01 ms per loop
Comment: you are right! i mistakenly divided by total items instead of total users. I have corrected the solution.
Comment: Some of your items seems to be nan. You should convert `productId` dtype to `str`. Add this line at top  `main['productId'] = main['productId'].astype(str)`
Comment: I don't think that the score is correct. I tried your solution, with the data set in this link (https://stackoverflow.com/questions/57373669/alternative-to-make-a-nested-for-loop-faster-product-recommendation?noredirect=1#comment101232501_57373669). The formula should be: score = CommonUsers / userCount.  (Shaver & Babyfoon = 0.1111, Shaver & CoffeeMachine = 0.3333, Shaver & Blender = 0.2222 etc.).
Comment: Thanks! Works fine on my dummy data. But on my real data, I receive this error:

  File "", line 3, in
    score = len(item_users[item1] & item_users[item2]) / n_users

KeyError: nan

Maybe any ideas how to fix that :-)
Answer: The key here is to create a cartesian product of productId. See code below,
Method 1(works with smaller dataset)
result=(main.drop_duplicates(['productId','userId'])
            .assign(cartesian_key=1)
            .pipe(lambda x:x.merge(x,on='cartesian_key'))
            .drop('cartesian_key',axis=1)
            .loc[lambda x:(x.productId_x!=x.productId_y) & (x.userId_x==x.userId_y)]
            .groupby(['productId_x','productId_y']).size()
            .div(data['userId'].nunique()))

result

Prod1   prod2   0.75
Prod1   prod3   0.75
Prod1   prod4   0.75
Prod1   prod5   0.5
prod2   Prod1   0.75
prod2   prod3   0.5
prod2   prod4   0.5
prod2   prod5   0.25
prod3   Prod1   0.75
prod3   prod2   0.5
prod3   prod4   0.5
prod3   prod5   0.5
prod4   Prod1   0.75
prod4   prod2   0.5
prod4   prod3   0.5
prod4   prod5   0.5
prod5   Prod1   0.5
prod5   prod2   0.25
prod5   prod3   0.5
prod5   prod4   0.5

Method 2
result = (df.groupby(['productId','userId']).size()
            .clip(upper=1)
            .unstack()
            .assign(key=1)
            .reset_index()
            .pipe(lambda x:x.merge(x,on='key'))
            .drop('key',axis=1)
            .loc[lambda x:(x.productId_x!=x.productId_y)]
            .set_index(['productId_x','productId_y'])
            .pipe(lambda x:x.set_axis(x.columns.str.split('_',expand=True),axis=1,inplace=False))
            .swaplevel(axis=1)
            .pipe(lambda x:(x['x']+x['y']))
            .fillna(0)
            .div(2)
            .mean(axis=1))

Comment: Thanks for the response Mark. Really appreciate it. getting a perfect output for a sample dataset. but still getting memory error for the main data comprising of 2 columns and 40k rows. Any advice would be highly valuable
Comment: @pranjaljain99 It is expected as 40k*40k rows is no joke! I'll have a look later to see if it's possible to arrange into matrix format.
Comment: Can you please explain me the part in method 1 where you calculated the score. Actually, I need a score something like ```        commonUsers= len(set(item1Users).intersection(set(item2Users)))
        score=commonUsers / userCount```
Comment: @pranjaljain99 Last two steps. groupby size divided by number of users. Please codes line by line to see what happens


form submit hook handler not getting called in drupal

Question: I have Drupal 7 site. It has custom module named say myproducts. This module has following hook
function myproducts_form_alter(&$form, &$form_state, $form_id) {

if(isset($form_id) && $form_id=='product_node_form')
{
   $form['#submit'][] = 'myform_form_submit';
}

}

function myform_form_submit(){
 echo 'test submit handler'; exit();
 }

But `myform_form_submit` is not getting called.
Any help highly appreciated.
Answer: The submit for the node form is attached to a specific button, so something like this should work:
$form['actions']['submit']['#submit'][] = 'myform_form_submit';

Comment: do I need to replace #submit with my submit button id ??
Comment: Nope, the structure is already there it should just be a matter of adding another function name to that array
Comment: Nopes, still not getting called
Comment: Be sure to use `dpm($form);` within the form_alter hook to find out exactly where the submit element is located within the $form array. It may not necessarily be in `$form['actions']`. (You need to install the Devel module to run `dpm();`)


Regex to Select All Math Operator not Hyphen

Question: I am trying to implement a regex which splits the string on all math operators but no hyphen in the string:
dummy_string= "I Dont_Know The-Meaning_2018-You Know_Meaning_2017+You Know_Meaning_2017"

string_list = re.split("[0-9][+/*\-][A-Za-z]", dummy_string)

print(string_list)

>>['I Dont_Know The-Meaning_201', 'ou Know_Meaning_201', 'ou Know_Meaning_2017']

Expected Output:
>>['I Dont_Know The-Meaning_2018', 'You Know_Meaning_201', 'You Know_Meaning_2017']

I am using regex (re) package for this.
Answer: You may use (?<=[0-9]) and (?=[A-Za-z]) lookarounds instead of consuming patterns:
import re
dummy_string= "I Dont_Know The-Meaning_2018-You Know_Meaning_2017+You Know_Meaning_2017"
string_list = re.split("(?<=[0-9])[+/*-](?=[A-Za-z])", dummy_string)
print(string_list)
# => ['I Dont_Know The-Meaning_2018', 'You Know_Meaning_2017', 'You Know_Meaning_2017']

See the Python demo
When you use [0-9][+/*\-][A-Za-z] to split a string, the digit before a non-word delimiter and a letter after it are consumed, i.e. added to the match value, and re.split removes this text from the resulting output. When using lookarounds, the matched texts remain "unconsumed", they are not added to the match value and thus remain in the re.split output.
Note that you do not have to escape - when it is at the end of the character class, [+/*-] = [+/*\-]. If you plan to add more chars into the class, you  may keep - escaped to avoid further issues.


Salvaging inaccessible MySQL db without InnoDB log files

Question: In the past few weeks, I have been playing with the configuration of our test MySQL server, without first making a mysqldump of applications running on it. Aim was to increase performance and enable lowercase_names. I have tried to tweak InnoDB log file size, for which I had to delete (say, rename) InnoDB log files in order for them to be recreated. Eventually, I restored old log files and commented all InnoDB log file options, leaving the lowercase_names=1 option in Linux.
Today I discovered that the oldest application's DB is inaccessible. While SHOW TABLES; reports all tables, but doing SELECT * FROM TABLE says ERROR 1146 (42S02): Table 'TABLE' doesn't exist.

I still have the original ibdata1 file, which I have never backed up
I have the backup of ib_logfile[01] files
I have tried setting innodb_force_recovery up to 6 but I still get the same error

I already dumped all other working DBs.
How can I salvage the last one with the given files?
Answer: It was not a problem with InnoDB logfiles or whatever.
When I tweaked the DB configuration my goal was to enable lowercase_names because one of our applications had hardcoded queries. This ultimately broke querying the DB I'm trying to salvage.
You can never recover a DB created and populated with different name casing. So the solution was to temporarily comment lowercase_names, dump and resume recovery from that.
Comment: This may fall in the "cannot validate new answers" (see https://meta.superuser.com/questions/4192/should-i-delete-my-own-question-if-no-new-answer-can-be-validated) but since this explains a problem that may occur to others in the future, I'm in doubt if to delete or not


Add number to sorted list recursively

Question: I want to add my_num to where it fits in the given list and I want to solve it by using this structure, however I'm not getting the right results. What can I do?
my_lst   = [1, 3, 5]
my_num   = 4

def new_number(lst):
    return lst

def plus_number(n, lst):

    def add_number(n2, lst2):
        for i in range(len(lst)):
            if lst2[0] > n2:
                return [n2] + lst2
            else:
                return [lst[0]] + add_number(n2, lst2[1:])

    return new_number(add_number(n, lst))

result = plus_number(my_num, my_lst)

print(result)

>>> [1, 1, 4, 5]

Edit: changed [lst[0]] to [lst2[0]] and it works. Thanks!
Comment: You realize that `new_number` is not creating a new list, right?
Comment: Typo.  In `add_number` you have `return [lst[0]] +`... where you meant to use `lst2`.
Comment: Your `new_number` function doesn't *do anything*, btw, simply returning whatever object you pass to it.
Comment: Thank you @TimRoberts that typo fixed it. It should be sorted yes. Thanks again!
Comment: as an aside, `for i in range(len(lst)):` doesn't make sense, the body of your loop always returns on the first iteration
Comment: @juanpa.arrivillaga what should I have instead?
Comment: You should not have a loop at all. Think about it, consider `for i in range(1_000_000_000): if n + 1 == 1000: return 'foo'; else return 'bar'` is equivalent to just `if n + 1 == 1000: return 'foo'; else return 'bar'`
Comment: And also, the *recursion* is doing your looping here
Answer: There is already a stdlib method for this, no need to reinvent it:
>>> from bisect import insort
>>> my_lst = [1, 3, 5]
>>> insort(my_lst, 4)
>>> my_lst
[1, 3, 4, 5]

Comment: Thank you I'm learning python and this is an exercise without stdlib
Comment: Part of learning Python is learning to use the right tools. Copying the list every step with `lst2[1:]` is really inefficient. I recommend you use `insort` here, it's much better and there is rarely any good reason to avoid stdlib.
Comment: It's learning for school, it's about grasping concepts, not writing production code
Answer: I've two solutions:

Using a sentinel, enumerate and insert.

my_lst = [1, 3, 5]
my_num = 4
my_lst.append(my_num) # sentinel
for i, value in enumerate(my_lst):
    if my_num <= value:
        my_lst.insert(i, my_num)
        break
my_lst.pop() # remove sentinel
print(my_lst)

Using enumerate, insert and else:

my_lst = [1, 3, 5]
my_num = 4
for i, value in enumerate(my_lst):
    if my_num <= value:
        my_lst.insert(i, my_num)
        break
else:
    my_lst.append(my_num)
print(my_lst)

If you can't use break, you can circumvent that with a function. Note that this will be trickier for the first function.
Comment: This is suboptimal _O(n)_, using `insort` is _O(log n)_.
Comment: Well, this is the best I can do without stdlib.

Add number to sorted list recursively


Espaço entre título e tabela

Question: O título da minha tabela está muito distante da tabela em si, gostaria de saber algum modo de diminuir esse espaçamento.
Imagem:

Código:
<code>\begin{table}[!htb]
    \renewcommand{\arraystretch}{1.5}
    \caption{Cronograma de pesquisa}
    \label{cronograma}
    \begin{center}
        \begin{tabular}{c|c c c c c c c c c c c c}
            \hline
            ATV & SET & OUT & NOV & DEZ & JAN & FEV & MAR & ABR & MAI & JUN & JUL & AGO \\
            \hline
            1 &  &  &  &  &  &  &  &  &  &  &  & \\
            2 &  &  &  &  &  &  &  &  &  &  &  & \\
            3 &  &  &  &  &  &  &  &  &  &  &  & \\
            4 &  &  &  &  &  &  &  &  &  &  &  & \\
            5 &  &  &  &  &  &  &  &  &  &  &  & \\
            6 &  &  &  &  &  &  &  &  &  &  &  & \\
            7 &  &  &  &  &  &  &  &  &  &  &  & \\
            8 &  &  &  &  &  &  &  &  &  &  &  & \\
            \hline
        \end{tabular}
    \end{center}
\end{table}
</code>
Answer: O comando <code>\vspace</code> serve para adicionar ou retirar espaçamentos verticais no LaTeX. Se for usado um número positivo, ele adiciona espaço. Se for usado um número negativo, ele retira.
No caso da tua tabela, eu retirei 5 milímetros entre a caption e a tabela em si. Veja o código abaixo e, logo em seguida, o resultado que obtive.
<code>\documentclass{article}

\begin{document}

\begin{table}[!htb]
    \renewcommand{\arraystretch}{1.5}
    \caption{Cronograma de pesquisa}
    \vspace{-5mm}
    \label{cronograma}
    \begin{center}
        \begin{tabular}{c|c c c c c c c c c c c c}
            \hline
            ATV & SET & OUT & NOV & DEZ & JAN & FEV & MAR & ABR & MAI & JUN & JUL & AGO \\
            \hline
            1 &  &  &  &  &  &  &  &  &  &  &  & \\
            2 &  &  &  &  &  &  &  &  &  &  &  & \\
            3 &  &  &  &  &  &  &  &  &  &  &  & \\
            4 &  &  &  &  &  &  &  &  &  &  &  & \\
            5 &  &  &  &  &  &  &  &  &  &  &  & \\
            6 &  &  &  &  &  &  &  &  &  &  &  & \\
            7 &  &  &  &  &  &  &  &  &  &  &  & \\
            8 &  &  &  &  &  &  &  &  &  &  &  & \\
            \hline
        \end{tabular}
    \end{center}
\end{table}

\end{document}
</code>
Comment: Obrigado, Marcus. Outro método que encontrei foi colocar o caption após \begin{center} e antes do \begin{tabular}. Não havia visto que tinha coloco fora.
Comment: Bom, ao menos fica aí um outro recurso caso tu precise alterar espaçamentos dentro do teu texto no LaTeX. O `\vspace` serve para as mais variadas situações, não apenas tabelas.

Espaço entre título e tabela


Looking for clarification on this definition: $K_1K_2 = K_1(K_2)$.

Question: In my algebra book they define for field extensions
$L/K_1/F$ and $L/K_2/F$ the field $K_1K_2 = K_1(K_2)$.
The formal definition I have for this is
$$ K_1(K_2) = \bigcap_{K_2 < E < L} E \quad \text{where } E/K_1. $$
So the smallest field extension of $K_1$ containing $K_2$.
For extensions such as $F(\alpha)$ though sometimes it is written,
$$ F(\alpha) = \{ a_0 + a_1\alpha + a_2\alpha^2 + \cdots + a_{n - 1}\alpha^{n - 1} : a_i \in F\} $$
Can I extend this definition for $K_1(K_2)$ and write the following?
$$ K_1(K_2) = \left\{ \sum_{i = 1}^n \sum_{j = 1}^{m_i} a_{ij}b_i^j : a_{ij} \in K_1, b_i \in K_2, n, m_i \in \mathbb{N} \right\}$$
EDIT: $K_1, K_2$ are algebraic.
Comment: That expression for $F(\alpha)$ only works for $\alpha$ algebraic over $F$ right?
Comment: That is right, It does happen that the $K_1, K_2$ are algebraic in my situation.
Comment: You can probably ask Elman. He is in his office a lot of times, and he always welcomes anyone who walks by it.
Answer: There are several different-looking definitions of the compositum $K_1K_2$ of two fields, and it's a very useful exercise to sit down and prove them equivalent. Here's my suggestion: Let's consider only subfields and subsets of some larger field $\Omega$. Then for a field $K$ and a set $S$, take as definition of $K(S)$ something close to what you wrote, namely
$$
K(S)=\bigcap_{L\supset K\cup S}L\,,
$$
where the index $L$ runs through fields only. The construct is a field, of course.
Now on the other hand, consider the set $K^{\mathrm{r}}(S)$ whose elements are the finite rational expressions (with coefficients in $K$) of the form $f({\mathbf{s}})=n({\mathbf{s}})/d({\mathbf{s}})$, where in each case, $\mathbf{s}$ represents some finite set of elements of $S$, and the numerator $n$ and the denominator $d$ are multivariable polynomials over $K$. Clearly $K^{\mathrm{r}}(S)$ is also a field. Now you can easily check that $K(S)=K^{\mathrm{r}}(S)$. It follows immediately that for two fields, $K_1(K_2)=K_2(K_1)$, and you also see that in your case, where the $K_i$ are algebraic over some smaller field, you can dispense with rational expressions, and use just polynomial ones.


Scala syntax for iteration? Or something else?

Question: I saw some code like this.
From REPL test, it looks like "/:" iterates through chars and call incr repeatedly. But I can't find any document for this syntax.
What does /: syntax mean here?
<code>val chars = List('a','b')
def incr(acc:Map[Char, Int], c:Char) = {
   val count = (acc get c).getOrElse(0) + 1
    acc + ((c, count))
}

(Map[Char,Int]() /: chars)(incr)
</code>
Comment: check the scaladoc for Map ;) http://www.scala-lang.org/api/current/index.html#scala.collection.Map@/:[B](z:B)(op:(B,A)=>B):B
Comment: Looks like it is foldLeft right?
Comment: It's just a method call like any other method call. It's *not* special syntax. Very few things in Scala are, in fact.
Answer: From the documentation:
<code>   *  Note: `/:` is alternate syntax for `foldLeft`; `z /: xs` is the same as
   *  `xs foldLeft z`
</code>


Inequalities of high level

Question: For positive reals $a,b,c$ prove
$$\frac{a+b-2c}{b+c} +\frac{b+c-2a}{c+a} +
\frac{c+a-2b}{a+b}\geq 0$$
I proved this if these $a,b,c$ were sides of a triangle but could not proceed further
Comment: @G-man The substitution you mention would have been useful to prove if $a, b, c$ where sides of a triangle.  For all positive reals, it is not at all obvious how that would work.
Comment: What is Ravi's substitution?pls clarify it and then I hope I can solve this question
Comment: can u write some important points of this inequality. I have 2g internet connection which is opening this link very slow
Answer: Hint:  Note that
$$\prod_{cyc}(b+c) \sum_{cyc}\frac{a+b-2c}{b+c} = \sum_{cyc} a(a-c)^2$$

Inequalities of high level


Woocoomerce Generate API Key button not responding at all

Question: I am trying to generate a REST API Key to connect an app with my site but after many trials, I could not get able to generate key.
I tried different names, Read-Write-Read/Write…but every time the Generate API Key did not work. After clicking nothing happens. I have tried different browsers, Cleared Browser and WordPress Cache along with my cpanel cache, But nothing changed.
The Generate Key button is not responding at all.
I mean It is not even loading, nothing is happening.
I will add a link to a screen recoding for help.
Video Link
Comment: No one to answer???
Answer: It's default woocomerce functionality. If REST API is not generating means there is an issue with any other plugin or js error.
so first deactivate all other plugins and try again if working, then again active other plugins one by one and try it to generate key, in this way, you can easily found which plugin has an issue.
Comment: Hello, Thank yuo for your response. I actually tried this way...I have also asked the same question on wordpress.org here is the link, maybe the convesation there along with screenshots would give you some lead???     https://wordpress.org/support/topic/rest-api-generate-key-not-responding/#post-14784257


DIfference between hazard ratio and partial hazard in survival analysis

Question: The hazard function in survival analysis is represented as (https://in.mathworks.com/help/stats/cox-proportional-hazard-regression.html):

Here the exponential term is termed as hazard ratio as mentioned here:(https://in.mathworks.com/help/stats/cox-proportional-hazard-regression.html):

However, in some other documentation, the exponential term is denoted as a partial hazard as mentioned here (https://lifelines.readthedocs.io/en/latest/Survival%20Regression.html#model-selection-in-survival-regression):

I have below queries:

Why is it important to subtract the dataset by mean ( ̅ )? By doing this does the value of the regression coefficient (b) and hence the hazard function (h) change? If not, then is there any documentation/research paper/article where people could have explained this criterion by given datasets?

What is the difference between hazard ratio and partial hazard?

Why is the term 'partial' being used? Or what is the significance of the term 'partial'

Can somebody please clarify my doubts?
Comment: A partial hazard is just the multiplier for the baseline hazard function to calculate the actual hazard function. As you know, the Cox model doesn't estimate the baseline hazard function (it's an unknown function of time). But for the purposes of risk prediction, you can classify subjects as "higher risk" or "lower risk" than each other based on the partial hazard alone.
Comment: That's not to say that a baseline hazard function can't be estimated, the Breslow step estimator is pretty widely used when going for "full bore prediction". The interepretation is straightforward - it's the hazard as a function of time for a subject with all variables = 0. If you center them in this fashion, then BSE uses "prediction at the means" to calculate the population incidence curve.
Answer: Subtracting the mean from the covariate values can help in fitting a Cox model, as otherwise the exponentiations can lead to overflow. I recall that the R coxph() function internally mean-centers and standardizes (to unit standard deviation) all continuous covariates for that reason, even though it reports coefficients appropriate to the original scales of the covariates.
In the formula with the mean subtracted, you can factor out the constant terms associated with the mean covariate values into the baseline hazard:
$$ h(t | x) = b_0(t) \exp \left(\sum_{i=1}^n b_i (x_i - \overline{x_i})\right)\\
=\left(\frac{b_0(t)} {\exp \sum_{i=1}^n b_i ( \overline{x_i})}\right)\exp \left(\sum_{i=1}^n b_i (x_i)\right).$$
Thus there's no change in the modeled coefficients, just in the definition of the baseline hazard.
The important "partial" terminology has to do with the "partial likelihood" that a Cox model optimizes to estimate coefficient values. Technically, a likelihood is (proportional to) the probability of observing the data given a set of parameter values. In a Cox model the actual observation times aren't modeled directly, so you don't model the probability of the data per se. The contribution of Cox was to recognize that, if you were willing to make a proportional-hazards assumption, you don't need to model the actual observation times and you can factor out the baseline hazard to start. What's left is then called the "partial likelihood" of the data given the Cox regression coefficients.
The "partial hazard" and "log-partial hazard" terminology isn't uniformly used in books on survival analysis; at least, it didn't show up in a quick search of a few electronic texts that I have on hand, including the classic text by Therneau and Grambsch on Cox models. It might be intended to emphasize the partial-likelihood basis of the coefficient estimates. I wouldn't worry too much about that terminology.
The hazard ratio is simply the ratio of two hazards. It's often represented for an individual having a set of covariate values with respect to the baseline hazard, as in your first examples, but in general you can calculate a hazard ratio between any two sets of covariate values that are included in a Cox model.
Comment: Thanks a lot@EdM for your valuable comments.


OSS Router firmwares

Question: DD-WRT, Open WRT , Tomato or Third-party firmware projects ?
What are the compelling reasons to choose between these?
I used to be a great DD-WRT fan until I realized that the author was deceiving users by publishing it as a OSS, but made it very cumbersome to download the source and change it (requires you to download GB's of source files) .Also their bandwidth monitoring feature was part of the paid version, which IMHO is a killer. Having said that, DD-WRT just worked. And I think that's great..
Answer: I was a DD-WRT user until I saw the interface of Tomato, and then I switched.  I made the decision based on management ease-of-use.  In particular, I thought Tomato's QoS setup was a lot easier than DD-WRT's.  Other reasons to choose might be the number of services/apps that have been ported to each distribution if you want to do more than just routing/wireless access.
Comment: Second the tomato recommendation
Answer: Here's a review of Tomato (a bit dated do) to help you make your decision:

Turn Your $60 Router into a User-Friendly Super-Router with Tomato

There's a mention of DD-WRT at the end. Also make sure to read the comments.
Answer: You may be easily swayed towards dd-wrt after seeing tomato's supported device list.
Contrast that with dd-wrt's list.
Personnaly, for the above reason, I never tried or plan to try tomato and have been using dd-wrt for a while. Some negative points you should be aware with dd-wrt:

Its open source nature is questionable (as you said);
Tends to have a few quirks even on stable releases;
The options available may overwhelm you;
Where's IPv6? :(
There's no reliable way to save/restore settings that'll work across versions.

On the plus side, if you're a tinkerer (and you are if you're playing with firmware) there are very frequent updates posted on the forum that may fix glitches you're having, improve performance dramatically, or even BRICK your router without warning.
Answer: I have tried using both, and found that WDS seemed to be more reliable on my hardware with dd-wrt when I compared the two.  I didn't do any investigation to figure out why, at the time I just wanted something that worked.
Comment: I've used and installed Linksys WRT54GL's for at least the last 4 years. I use the same 'out of the box' Tomato WDS setup and have never had any issues. Just my experience...

OSS Router firmwares


OCFS2 updating procedures

Question: We plan to use OCFS2 in a project running Red Hat Enterprise Linux (RHEL) 5.6 because we found OCFS2 to be much simpler to set up than GFS.
I am, however, worried about the upgrade process since I understand OCFS2 depends on the kernel version number. If I perform yum update on my RHEL host I might receive a newer kernel version. What would be the correct way to keep OCFS2 updated and working?
What is the best practice to have an OCFS2 system that runs a long time and also gets kernel updates?
Comment: I just saw that the guys are Oracle have only made OCFS2 available for their own kernel. I cannot see the source to it or patches for the vanilla kernel anywhere on their website. Am I missing something?
Comment: Hi, you can grab OCFS2 off their project website. Just make sure you get the right kernel version then you can use it with RHEL/CentOS, etc.
http://oss.oracle.com/projects/ocfs2/
Comment: Yeah, I should have said "the 1.6 version of OCFS2". The older version (1.2 and 1.4) are available as source downloads and (as I see it) have been merged upstread. If anyone can point to a source download of the 1.6 version of ocfs2, that would be grand.
Answer: We have exclude = kernel* in our yum.conf file to avoid this exact problem. I should add that we only kernel update when there is a urgent need to do so and when oracle blesses the kernel
Answer:
If I perform yum update on my RHEL host I might receive a newer kernel version

Are you sure?  AFAIK, the whole point of RHEL is not to change anything on the same system release.  You just get bugfixes, not upgrades.
Comment: Unfortunately I am sure. Also RHEL bounces you from 5.5 to 5.6 without you really noticing. I have had several kernel updates during the last few months. You can imagine my surprise when I found out.

OCFS2 updating procedures


Is $\{x + y^2 | (x,y) \in B_r(a,b) \}$ an open set?

Question: Let $ f : \mathbb{R}^2 \rightarrow \mathbb{R} : (x,y) \mapsto x + y^2 $

Does $f$ map open sets to open sets with respect to the standard topology on $\mathbb{R}$?

I welcome any answers to this problem, below is my attempt.

Any open set in $\mathbb{R}^2$ can be written as a union of open balls in $\mathbb{R}^2$, this is because the collection of open balls form a topological basis. Also the image of a union of sets is the union of the images of each set and a union of open sets is open. Thus the problem reduces to:
Is $\{x + y^2 |  (x,y) \in B_r(a,b) \}$ an open set?
Special case:
$\{x + y^2 |  (x,y) \in B_1 (0,0) \} = (-1,1.25)$
This was obtained by noting
$\sup \{x + y^2 |  (x,y) \in B_1 (0,0) \} = \\ \max  \{x + y^2 |  x^2 + y^2 =1 \} = \max \{\cos (\theta) + \sin^2(\theta) | \theta \in [0,2\pi] \}$
And the last expression can be solved by Weierstrass' Extreme Value theorem.
Comment: The notation $(x,y) \in B_r (x,y)$ is not very fortunate. It's like writing $\int_0^t f(t)\, dt$.
Answer: Let $(u,v) \in U$, with $U \subset \Bbb{R}^2$ open. We need to show that there is $\epsilon >0$ with $B_\epsilon (f(u,v))\subset f(U)$.
For this, simply note that there is $\epsilon >0$ with $B_\epsilon (u) \times\{ v \}\subset U$.
Together with the special form of $f$, this proves the claim (why?).
Answer: Let $\Omega\subset{\mathbb R}^2$ be an open set, and consider a point $c_0\in f(\Omega)$. There is a point $(x_0,y_0)\in\Omega$ with $f(x_0,y_0)=c_0$. As $\Omega$ is open the is an $h>$ such that the segment $\sigma_h$ with endpoints $(x_0-h,y_0)$ and $(x_0+h,y_0)$ is contained in $\Omega$. It follows that
$$[c_0-h,c_0+h]=f(\sigma_h)\subset f(\Omega)\ .$$
Since $c_0\in f(\Omega)$ was arbitrary, this proves that $f(\Omega)$ is open.
Note that we did use the exact definition of $f$; continuity would not be sufficient.


Why are all real inflection points on a cubic projective algebraic curve on 1 line?

Question: Say we have $C\subset \mathbb{CP}^2$, a smooth curve of degree 3. I am aware of the group structure on cubics, what I don't get, is why are all inflection points with only real coordinates lie on a single line in $\mathbb{CP}^2$.
I think I see that if two real inflection points are on a line, then the third point (which will also be an inflection point) will also only have real coordinates. And so if there are 4 real inflection points then all inflection points will be real.
Comment: The answer [here](https://math.stackexchange.com/questions/4030730/geometric-interpretation-of-the-9-order-3-points-of-elliptic-curves) seems to be applicable.
Comment: 1. There's no need to use abbreviations like "infpoints" here - you have time to write out all of your statements in detail :) 2. Do you know that an inflection point is a 3-torsion point for the group law? Do you know what the structure of $n$-torsion points on an elliptic curve is?
Comment: 1. ok thanks :) $\\$ 2. You mean that P+P+P=0 iff P is an inflection point? I don't learn maths in english, and I can't always find what the Hungarian version of the terminology is.
Comment: @KReiser Thanks for this reference. Another reference that I just discovered [here](https://math.berkeley.edu/~monks/papers/DualityV3.pdf) , close to my approach.
Comment: As @KReiser mentions my answer, I'll comment a few things here. Firstly, you have discovered a non-trivial fact about elliptic curves, the field generated by the $N$-torsion points must contain an $N^{\text{th}}$-root of unity. Since inflection points are $3$-torsion points on an elliptic curve (once one is chosen to be the identity) the field generated by all the inflection points must be $\mathbb{C}$ since primitive 3rd roots of unity are not rational. In general this is due to the existence of the Weil pairing, but for 3-torsion it is somewhat simpler as my answer indictates.
Comment: Secondly, if you want to prove this yourself as an exercise, I would recommend starting with the Hessian description of the inflection points. First show that one inflection point must be over $\mathbb{R}$. Then move it to infinity to get a Weierstrass equation. Now compute the intersection with your cubic curve and project onto the $x$-coordinate (this will give you the $3$-division polynomial). Finally, compute its discriminant and see that its splitting field must contain a 3rd root of unity. I think this approach is quite illustrative and touches on some nice things.
Answer: This answer attempts to situate the issue in a dual view that historicaly has been capital, especially to Plücker for the discovery of his relationship that you surely know well. It is also how 19th century mathematicians have perceived/understood the power of duality because it could be explained through geometrical figures (and not with our present-day abstract algebraic notations), the only way to "convey" at that time what duality was about.
Disclosure : this isn't a general proof ; this answer, based on a final example (Fig. 4) is meant as a way to (more or less) follow the steps of Plücker, inserting the alignment of the three inflection points of a cubic curve into a more general context that brought him to a vast generalization.
The objective is to understand Fig. 4.
In this respect, we need first a recall about the duality "poles $\leftrightarrow$ polar lines" (Fig. 1 and 2) ; it will allow us, in a second step  (Fig. 3), to define the so-called "Polar Transform".
Let us consider the Euclidean plane with center $O$ and its unit circle. A point $M$ has a conjugate line called its polar line defined geometricaly in the following way ; the polar line of $M$ passes through $M'$ and is orthogonal to line $OM$ where $M'$ is the point of line $OM$ such that dot-product $\vec{OM}.\vec{OM'}=1$. The three different cases (point $M$ inside, outside and on unit circle are featured on figure $1$). $M$ is called the "pole" attached to its polar line.
A fundamental property of duality is the connection between alignment of points and intersection of lines, illustrated on Fig. 2.
The concept of "polar transform" is explained using an example in Fig. 3.

Fig. 1 : When $P$ is on the unit circle, its polar line is the tangent line at $P$. One can consider the concept of polar line as a generalization of the concept of tangent.

Fig. 2 : Duality between points' alignment and (polar) lines intersection.

Fig. 3 : Duality between verbs "to describe" and "to envelope" : when points describe a curve (C) (here a parabola), their polar lines envelope a certain curve (C') (here an ellipse, partially materialized). In a dual way, when points describe (C'), their polar lines envelope (C) ! (C) and (C') are called reciprocal polar curves ; transform (C) $\leftrightarrow$ (C') is called "polar transform".
We have now the tools to understand Fig. 4 featuring the example of cubic curve $(C)$ with parametric equation :
$$(1+y)(x^2+y^2)-xy^2=0 \tag{1}$$
with 3 inflection points at finite distance and an asymptote $y=-1$.
Curve $(C)$ possesses an isolated point at the origin, that we will neglect. Please note that (1) can be converted into the following polar equation :
$$r=\frac{1}{\sin \theta (\tfrac12 \sin(2\theta)-1)}\tag{2}$$
Now turn to the legend of Fig. 4 which explains (almost) everything. The fact that inflection points are in correspondence with cusps is one of the most interesting observations that can be made.
Remarks :

All the concepts introduced here (polar line, polar transform, etc...) can be given an algebraic form. See here. For example, if  curve $(C)$ with current point $(x,y)$ decribed by parametric equations, one obtains the parametric equations of the polar transform under the form :

$$\begin{cases}x&=&\alpha(t)\\y&=&\beta(t)\end{cases} \ \ \to \ \ \begin{cases}X&=&-\beta'(t)/\delta(t)\\Y&=& \ \ \ \alpha'(t)/\delta(t)\end{cases}$$ $$\text{with} \ \delta(t)=\alpha(t)\beta'(t)-\alpha'(t)\beta(t).$$
In particular, the cubic and the deltoid can be represented resp. by the following parametric equations :
$$\begin{cases}x&=&\frac{t(1+t^2)}{(-1+t-t^2)}\\y&=&\frac{(1+t^2)}{(-1+t-t^2)}\end{cases} \implies \begin{cases}X&=&\frac{1-t^2}{(1+t^2)^2}\\Y&=&\frac{2t^3}{(1+t^2)^2}-1\end{cases}$$

See this very interesting article, close to my approach.

The thin red curve passing through the origin that can be seen on Fig. 4 is the inverse curve of $(C)$, which has as well a certain number of properties that would be too long to expand here.

Fig. 4 : A cubic curve $(C)$ (in blue), with some of its points and their polar lines ; the polar transform $(C')$ of $(C)$ (envelope of its polar lines) is a hypocycloid, more precisely a deltoid. The tangents at the 3 cusp/cuspidal points of $(C')$ (magenta color) are the polar lines of the 3 inflection points $I,J,K$ ; their common point $M$ is the pole of the line on which the inflection points are aligned.
Comment: Any comment ?...


Besides Impact, what JavaScript game engines exist?

Question: Pls don't include subjective thoughts, or moderators will close this question like they closed our last one (http://stackoverflow.com/questions/7668076/impact-vs-other-javascript-game-engines).
We need game engines that can run on iOS devices and ideally Android devices as well.
Thanks!
Comment: If you don't need any subjectivity, wouldn't Google work just as well?
Comment: Frankly, we prefer subjectivity, but the last question was closed because it fostered too much debate. This is better than Google because we can see which game engines have greater consciousness in the SO community, which is a crude proxy for penetration, which itself is a crude proxy for utility.
Comment: the reason i prefer asking question like this here as opposed to google is that people can speak of their personal experiences/shortcomings of the project, etc.
Comment: "or moderators will close this question like they closed our last one" Ah, I see: "This question was voluntarily removed by its author."
Answer: Here's a site which contains a big list of JS game engines:
http://www.reddit.com/r/javascript/comments/f094j/list_of_js_game_engines_community_effort/
Also, if you're interested in this sort of thing, you might want to subscribe to the Javascript Weekly newsletter (or at least read the back-issues from their site). This newsletter is a great way of keeping up-to-date with the latest news in the Javascript world, including news about new JS libraries that come out. Game libraries have features quite often.
Hope that helps.
Comment: How does impact and javascript know if the game is to be displayed on a mobile device or a regular monitor.
Comment: @DougHauf - That's a really big topic; you probably want to ask it as a separate question, there's too much to explain to pic it in a comment here.
Answer: Crafty javascript game engine also exists.
Comment: which one do you think is the best. I have heard good things about impact but I want the games to run equally on the computer as they wou would a phone or tablet. I even think that they can be downloaded on the WII U. Not certain on the last part.


Subset a Data Frame based on Multiple Conditions

Question: I have the following data frame (my real data frame has more rows and columns but is ostensibly structured as such):
Root_R1 = c(1,2,3,4,5)
Root_R2 = c(1,0,3,0,0)
Root_R3 = c(1,0,3,0,0)
Shoot_R1 = c(1,0,3,4,5)
Shoot_R2 = c(0,0,31,4,5)
Shoot_R3 = c(0,0,0,0,0)
data.frame(Root_R1, Root_R2, Root_R3, Shoot_R1, Shoot_R2, Shoot_R3)

Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
1    Root    Root    Root    Shoot    Shoot    Shoot
2       1       1       1        1        0        0
3       2       0       0        0        0        0
4       3       3       3        3       31        0
5       4       0       0        4        4        0
6       5       0       0        5        5        0

What I would like to do is filter this data frame and find all of the rows where at least two of the columns associated with a tissue type (Root, Shoot, etc.) have values greater than 0. So, for the columns associated with "Roots" (Column 1,2,3), Rows 1 - 3 should be returned while the ones associated with "Shoots"will have Rows 4 - 6 returned. I thought a ifelse code would work but that seems inefficient. Would filter from dplyr be more appropriate?
Comment: It's a bad idea to mix character strings and numbers in a vector, they all become character.
Comment: Understood. If I made the same data frame but heeded your advice (dropping the "Root" and "Shoot" from my df and using "Root_R1" as a column title, would your suggested solution below change?
Comment: Yes,it would the first instruction (`df1 <- df1[-1,]`) would no longer be needed. Nor to coerce to integer in the `df1[]<-lapply` loop. This loop wouldn't hurt but it wouldn't be needed.
Comment: Gotcha. And assuming I want to change the numerical value of the cut-off, I would change this part of the code ```(sum(x > 0L))``` and if I wanted to change the number of rows that meet the cut-off, I would change this: ```>= 2L```?
Comment: Yes, that's it. Or write a function. I will edit my answer predicting those cases.
Answer: Here is a base R solution.
grep tells the "Root" columns from the "Shoot" ones. Then apply loops returning logical (row) indices and which take care of sub-setting the data.frame.
Root_R1 = c("Root",1,2,3,4,5)
Root_R2 = c("Root",1,0,3,0,0)
Root_R3 = c("Root",1,0,3,0,0)
Shoot_R1 = c("Shoot",1,0,3,4,5)
Shoot_R2 = c("Shoot",0,0,31,4,5)
Shoot_R3 = c("Shoot",0,0,0,0,0)
df1 <- data.frame(Root_R1, Root_R2, Root_R3, Shoot_R1, Shoot_R2, Shoot_R3)

df1 <- df1[-1,]
df1[] <- lapply(df1, as.integer)

root <- grep("Root", names(df1))
shoot <- grep("Shoot", names(df1))
ok_root <- which(apply(df1[root], 1, \(x) sum(x > 0L) >= 2L))
ok_shoot <- which(apply(df1[shoot], 1, \(x) sum(x > 0L) >= 2L))

df1[ok_root, ]
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 2       1       1       1        1        0        0
#> 4       3       3       3        3       31        0
df1[ok_shoot, ]
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 4       3       3       3        3       31        0
#> 5       4       0       0        4        4        0
#> 6       5       0       0        5        5        0

Created on 2022-06-09 by the reprex package (v2.0.1)

Edit
Following a question in comments

And assuming I want to change the numerical value of the cut-off, I would change this part of the code (sum(x > 0L)) and if I wanted to change the number of rows that meet the cut-off, I would change this: >= 2L?

here is a function to solve the problem.
special_subset <- function(x, colpattern, cutoff = 0L, numrows = 2L) {
  i_cols <- grep(colpattern, names(x))
  ok <- which(apply(x[i_cols], 1, \(y) sum(y > cutoff) >= numrows))
  x[ok, ]
}

special_subset(df1, "Root")
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 2       1       1       1        1        0        0
#> 4       3       3       3        3       31        0

special_subset(df1, "Shoot", cutoff = 1)
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 4       3       3       3        3       31        0
#> 5       4       0       0        4        4        0
#> 6       5       0       0        5        5        0

Created on 2022-06-09 by the reprex package (v2.0.1)

Edit 2
To pass more than one colpattern to the function above, use a lapply loop.
In the two exaples below, first I use the new pipe operator introduced in R 4.2.0 and in the second a standard lapply.
tissue_type <- c("Root", "Shoot")

tissue_type |>
  lapply(\(pat, data) special_subset(data, pat), data = df1)
#> [[1]]
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 2       1       1       1        1        0        0
#> 4       3       3       3        3       31        0
#>
#> [[2]]
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 4       3       3       3        3       31        0
#> 5       4       0       0        4        4        0
#> 6       5       0       0        5        5        0

lapply(tissue_type, \(pat, data) special_subset(data, pat), data = df1)
#> [[1]]
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 2       1       1       1        1        0        0
#> 4       3       3       3        3       31        0
#>
#> [[2]]
#>   Root_R1 Root_R2 Root_R3 Shoot_R1 Shoot_R2 Shoot_R3
#> 4       3       3       3        3       31        0
#> 5       4       0       0        4        4        0
#> 6       5       0       0        5        5        0

Created on 2022-06-17 by the reprex package (v2.0.1)
Comment: Thank you. I assume a ```lapply``` function doing the same thing to each data frame in the list look like this: ```ok_list <- which(lapply(list, 1, \(x) sum(x > 0L) >= 2L))```?
Comment: @PatrickThomas No, `lapply` doesn't have a `MARGIN` argument, it must be `apply`. Try it and see the `1` giving an error.
Comment: Understood. I want to run this function on a data frame with several groups and I'm trying to think of the most efficient way to do so. Would a ```for``` loop be feasible if I placed all of my tissue types in a table labeled ```colpattern``` with a column titled ```colpattern$tissue_type``` and ran the following: ```special_subset <- function(x, colpattern, cutoff = 0L, numrows = 2L) {
  for (i in seq_along(colpattern$tissue_type)) {
  i_cols <- grep(colpattern, names(x))
  ok <- which(apply(x[i_cols], 1, \(y) sum(y > cutoff) >= numrows))
  x[ok, ]
} ```?
Comment: I tried placing all of my categories for my large df in a vector called ```tissuetype <- c("Roor", "Shoot")``` and drafted the following code to run on multiple columns at once ```special_subset <- {function(x,z, cutoff = 0L, numrows = 2L) {
  for (i in seq_along(z)) {
    i_cols <- grep(colpattern, names(x))
    ok <- which(apply(x[i_cols], 1, \(y) sum(y > cutoff) >= numrows))
    x[ok, ]}
  }
}
``` but got the following error:  ```Warning messages: 1: In grep(colpattern, names(x)) : argument 'pattern' has length > 1 and only the first element will be used```.
Comment: @PatrickThomas Try `tissue_type<- paste(colpattern$tissue_type, collapse="|")` before `grep(tissue_type, names(x))`.
Comment: The vector with my tissue types is called ```tissuetype```. I tried running this as you suggested: ```special_subset <- {function(x,z, cutoff = 0L, numrows = 2L) {
  for (i in seq_along(z)) {
    colpattern<- paste(tissuetype, collapse="|")
    i_cols <- grep(colpattern, names(x))
    ok <- which(apply(x[i_cols], 1, \(y) sum(y > cutoff) >= numrows))
    x[ok, ]}
  }
}``` but got this error: ```Error in paste(colpattern, collapse = "|") :
  argument "colpattern" is missing, with no default```
Comment: @PatrickThomas I have just updated the function, see Edit 2.
Comment: Thanks, but this isn't quite doing what I want. If I have a vector with ```c("Roots", "Shoots")```, I want to loop through each element of the vector to get the results of the function for each element. I thought looping the items in the vector in a ```for``` loop might work but I can't get it to work, it keeps returning ```NULL```.
Comment: @PatrickThomas OK, With the original function, use a `lapply` loop, see Edit 2, revised. The new function was deleted.