Distribution of sum of squares error for linear regression?

Question: I know that distribution of sample variance
$$
\sum\frac{(X_i-\bar{X})^2}{\sigma^2}\sim \chi^2_{(n-1)}
$$
$$
\sum\frac{(X_i-\bar{X})^2}{n-1}\sim \frac{\sigma^2}{n-1}\chi^2_{(n-1)}
$$
It's from the fact that $(X-\bar{X})^2$ can be expressed in matrix form,
$xAx'$ (where A: symmetric), and it could be again be expressed in: $x'QDQ'x$ (where Q: orthonormal, D:diagonal matrix).
What about $\sum(Y_i-\hat{\beta}_0-\hat{\beta}_1X_i)^2$, given the assumption $(Y - \beta_0 - \beta_1X)\sim \mathcal{N}(0, \sigma^2)$ ?
I figure $$\sum\frac{(Y_i-\hat{\beta}_0-\hat{\beta}_1X_i)^2}{\sigma^2}\sim \chi^2_{(n-2)}.$$
But I have no clue how to prove it or show it.
Is it distributed exactly as $\chi^2_{(n-2)}$?
Comment: Is this homework? If so, please use the Homework tag.
Comment: No, it's not. I think it is true bcoz after all, sum of squares is a square of linear combination of Y's given constant X's. But is it? Simple proof like this would be appreciated!http://math.stackexchange.com/questions/47009/proof-of-fracn-1s2-sigma2-backsim-chi2-n-1
Comment: The descriptions you give in both the question and your comment are a little bit muddled. Have you written out what your matrix $A$ must be for the sample variance? Does that help you see how to generalize?
Comment: Corrected for D. I think the critical point is that diagonal element of D should be something like (1,1,1,...,1,0,0). Is there any way to prove it? or Is there anyway to show that $\chi^2(n)=\chi^2(n-2)+\chi^2(1)+\chi^2(1)$ where sse/$\sigma^2 \sim \chi^2(n-2)$, $\sum{e_i^2}/\sigma^2 \sim \chi^2(n)$
Comment: https://stats.stackexchange.com/q/20227/119261
Answer: We can prove this for more general case of $p$ variables by using the "hat matrix" and some of its useful properties.  These results are usually much harder to state in non matrix terms because of the use of the spectral decomposition.
Now in matrix version of least squares, the hat matrix is $H=X(X^TX)^{-1}X^T$ where $X$ has $n$ rows and $p+1$ columns (column of ones for $\beta_0$).  Assume full column rank for convenience - else you could replace $p+1$ by the column rank of $X$ in the following.  We can write the fitted values as $\hat{Y}_i=\sum_{j=1}^nH_{ij}Y_j$ or in matrix notation $\hat{Y}=HY$.  Using this, we can write the sum of squares as:
$$\frac{\sum_{i=1}(Y-\hat{Y_i})^2}{\sigma^2}=\frac{(Y-\hat{Y})^T(Y-\hat{Y})}{\sigma^2}=\frac{(Y-HY)^T(Y-HY)}{\sigma^2}$$
$$=\frac{Y^T(I_n-H)Y}{\sigma^2}$$
Where $I_n$ is an identity matrix of order $n$.  The last step follows from the fact that $H$ is an idepotent matrix, as $$H^2=[X(X^TX)^{-1}X^T][X(X^TX)^{-1}X^T]=X(X^TX)^{-1}X^T=H=HH^T=H^TH$$
Now a neat property of idepotent matrices is that all of their eigenvalues must be equal to zero or one.  Letting $e$ denote a normalised eigenvector of $H$ with eigenvalue $l$, we can prove this as follows:
$$He=le\implies H(He)=H(le)$$
$$LHS=H^2e=He=le\;\;\; RHS=lHe=l^2e$$
$$\implies le=l^2e\implies l=0\text{ or }1$$
(note that $e$ cannot be zero as it must satisfy $e^Te=1$) Now because $H$ is idepotent, $I_n-H$ also is, because
$$(I_n-H)(I_n-H)=I-IH-HI+H^2=I_n-H$$
We also have the property that the sum of the eigenvalues equals the trace of the matrix, and
$$tr(I_n-H)=tr(I_n)-tr(H)=n-tr(X(X^TX)^{-1}X^T)=n-tr((X^TX)^{-1}X^TX)$$
$$=n-tr(I_{p+1})=n-p-1$$
Hence $I-H$ must have $n-p-1$ eigenvalues equal to $1$ and $p+1$ eigenvalues equal to $0$.
Now we can use the spectral decomposition of $I-H=ADA^T$ where $D=\begin{pmatrix}I_{n-p-1} & 0_{[n-p-1]\times[p+1]}\\0_{[p+1]\times [n-p-1]} & 0_{[p+1]\times [p+1]}\end{pmatrix}$ and $A$ is orthogonal (because $I-H$ is symmetric) .  A further property which is useful is that $HX=X$.  This helps narrow down the $A$ matrix
$$HX=X\implies(I-H)X=0\implies ADA^TX=0\implies DA^TX=0$$
$$\implies (A^TX)_{ij}=0\;\;\;i=1,\dots,n-p-1\;\;\; j=1,\dots,p+1$$
and we get:
$$\frac{\sum_{i=1}(Y-\hat{Y_i})^2}{\sigma^2}=\frac{Y^TADA^TY}{\sigma^2}=\frac{\sum_{i=1}^{n-p-1}(A^TY)_i^2}{\sigma^2}$$
Now, under the model we have $Y\sim N(X\beta,\sigma^2I)$ and using standard normal theory we have $A^TY\sim N(A^TX\beta,\sigma^2A^TA)\sim N(A^TX\beta,\sigma^2I)$ showing that the components of $A^TY$ are independent.  Now using the useful result, we have that $(A^TY)_i\sim N(0,\sigma^2)$ for $i=1,\dots,n-p-1$.  The chi-square distribution with $n-p-1$ degrees of freedom for the sum of squared errors follows immediately.
Comment: Wow, Thank you very much. It really is magnificent! Matrix form really pays off! In summary, SSE/$\sigma^2 = Y^T(I-H)Y$ and $I-H$ is idempotent. Idempotent matrices have eigenvalues either 0 or 1. So sum of eigenvalues is the number of eigenvalue 1. and $tr(I_n-H)= tr(I_n)-tr(H)=tr(I_n)-tr(X(X^T X)^-1 X^T)=tr(I_n)-tr((X^T X)^-1 X^T X)$ since $tr(AB)=tr(BA)$, and $tr(I_n-H)$ becomes n-p+1. and sum of eigenvalues of a matrix is sum of traces of the matrix! and $I-H$ can be expressed as $ADA^T$. So the first $Y^T(I-H)Y$ becomes $Y^TADA^TY$ with D with only n-p-1 diagonal 1's.
Comment: Great answer!! Just to present another approach, we can instead choose to define a transformed multivariate normal variable $v := A'Y$ and it will still follow the same distribution $\mathcal{N}\left(0, \sigma^{2}I\right)$ if we use the affine property. Then the last fraction $\frac{Y'ADA'Y}{\sigma^{2}} = \frac{v'Dv}{\sigma^{2}} = \frac{v'\begin{bmatrix} I & 0\\0 & 0\end{bmatrix}v}{\sigma^{2}}= \sum_{i=1}^{\operatorname{tr}D} \left(\frac{v_{i}}{\sigma}\right)^{2} $.


O que é Multi-Tenancy?

Question:
O que é Multi-Tenancy?
Quais principais abordagens para implementá-lo?
Answer:
O que é Multi-Tenancy?

É a capacidade da aplicação suportar a execução de diversos usuários ou grupos de usuários que possuem alguma ligação e precisam do funcionamento dela de uma forma específica. Em geral cada um terá seus dados separados, configurações próprias, funcionam de forma independente, mas é uma aplicação só rodando. Em geral esse grupo de usuários, pode ser uma empresa que contratou um serviço de aplicação. Mas pode ser seu navegador que permite que vários usuários o usem de forma independente sem que o uso de um afete o outro.
Como o nome em inglês diz, você arrenda uma parte de uma infraestrutura para você. Outras partes da mesma infraestrutura ficam disponíveis para outras pessoas de forma isolada. O isolamento se dá pela forma como a aplicação foi desenvolvida.
O sistema operacional costuma funcionar assim, pelo menos em partes. Como nem tudo é perfeito como multitenancy existem soluções como o Docker. Bancos de dados funcionam assim.
Virtualização, nuvem, essas coisas não costumam ser consideradas multitenancy porque o isolamento é grande demais é quase como rodar em máquinas separadas. Mas principalmente há instâncias separadas, o isolamento não é feito pela aplicação em questão e sim por uma ferramenta externa.
Ela se contrapõe à aplicação rodar de forma totalmente isolada para vários usuários. Se você fizer instalações totalmente separadas, mesmo rodando na mesma máquina não é multitenancy.
Mas obviamente que nem precisa ser tudo na mesma máquina para ser multitenancy, desde que as diversas máquinas trabalhem com mais de um grupo de usuários isoladamente. O fato da aplicação ser distribuída nada tem a ver com o conceito de multi arrendamento.
Em geral existem bancos de dados diferentes (não importa o que seja esse DB, se é SQL, se são arquivos e diretórios, registro do Windows, etc.) para cada usuário/grupo e código sabe lidar com isso. Não pode ter algo que faça globalmente na máquina, porque a máquina não é de apenas um usuário.
Se a aplicação não for desenhada para trabalhar dessa forma provavelmente haverá confusão. Em geral isso exige um pouco mais de complexidade no desenvolvimento porque tudo precisa ser configurável de acordo com o "cliente" que está arrendando sem atrapalhar os demais, mas facilita a implantação e manutenção física do ambiente, embora atualizações podem ser mais problemáticas.
A manutenção do desenvolvimento precisa ser feita com um pouco mais de cuidado porque várias situações são possíveis, principalmente quando algo incompatível é feito. Piora quando os "clientes" possuem muita flexibilidade e podem personalizar demais algumas coisas.

Quais principais abordagens para implementá-lo?

Não sei se consigo responder isso, a não ser dizendo o óbvio, que todos os recursos precisam poder ser acessados conforme alguma informação que remete a qual cliente está operando naquele momento. Essa decisão de pegar os dados de qual lugar pode ser tomada baseada no login efetuando, uma configuração em algum lugar, uma chave, o local de onde vem o acesso, ou mesmo o domínio de internet que está acessando, só para citar algumas.
Se for usar um banco de dados tradicional, pode ser que todos os "clientes" estejam em uma mesma base de dados e tenha uma chave para diferenciar a quem pertence aquela linha. Ou pode ter bases de dados diferentes e completamente isoladas, o que pode dar mais possibilidades de personalização de cada um.
As abordagens são tantas e algumas tão específicas para o tipo de aplicação que ficaria difícil citar. Eu citei exemplos de aplicação que muita gente não percebe que é multitenancy, cada um tem sua abordagem, quem entende o funcionamento deles deve perceber como cada um funciona.
O sistema que usa isto é multi-tenant.
Answer: De forma resumida multi-tenancy é quando você tem um sistema que aceita varios usuário, onde cada usuário utiliza o sistema de forma isolada.
Um exemplo é o wordpress.com onde cada usuário tem o seu blog independe e isolado dos outros blogs. Diferente do Facebook, onde todos os usuários coexistem no mesmo ambiente.

O que é Multi-Tenancy?


Why are these not functions?

Question: The definition of a function given in my proofs class is:
$\Gamma f = \{(a,b)\in A\times\ B: b=f(a)\} = \{(a,\ f(a)): a\in A\} \subseteq A \times B$
And we are asked if the following are functions
$\{(2a,\ a^2): a\in \Bbb Z\} \subseteq \Bbb Z\ \times \Bbb Z$
$\{(a^2,\ a^3): a\in \Bbb Z\} \subseteq \Bbb Z\ \times \Bbb Z$
$\{(a^2,\ a^2): a\in \Bbb Z\} \subseteq \Bbb Z_{\ge0}\ \times \Bbb Z_{\ge0}$
Each answer is  no they are not and I do not understand why. My thinking process is(using first one for an example) that the coordinates would be something like $(-4,4), (-2,1), (0,0), (2,1), (4,4)$. Which would form a parabola which is a function.
Is there something I'm missing or am I misinterpreting the definition?
Comment: That definition you say you were given is a rather poor one as it depends completely on **what in the world** $\;f\;$ is ...and if you *already* defined $\;;$ to be *a function* then you have one ugly circular definition. That looks close to be the definition of "graph" of a function...
Answer: For the first set, the image of odd number are not defined but odd number is included in the domain. Hence it is not a function. In particular, $f(1)$ is not defined.
For the other two sets,  try to think of a number that is included in the domain but the image is not defined.

Why are these not functions?


Changing process priority in linux for jvm process

Question: Is it fine if I change the JVM process priority through nice at server machine [linux]. I wanted my JVM process to use maximum resources from OS.
What is the standard practice? Do people tune the linux OS specifically for a process to get  maximum performance?
Answer: The normal fair share approach work well for most work loads, and it is not common to tune priorities for performance.  Tuning process priorities can have undesirable side-effects.  There are cases, such as batch processes, where applications are specifically niced.
You will need root privileges to give the JVM higher than normal priority.  This might be acceptable if the process is I/O bound.  If the process is CPU bound, you may find it kills performance on ALL other processes.  This will include the process you used to up the priority.
It is common to nice batch processes, so they don't use resources which might be used by interactive processes.  The CPU speed monitors can be be set to ignore such processes when determining if CPU speed should be increased.  If batch processes are niced, this is typically done in the script running the process.
Modern kernels also allow you to set IO priorities for processes.  Check man ionice.  If your JVM is IO bound this might be appropriate.
I do run my boinc processs extremely niced.  This is because I only want to use CPU cycles which would otherwise be unused for these processes.  They typically uses over 90% of the available cycles.
Answer: Setting a favourable 'nice' level will give that process priority CPU time over other processes. You will only be able to burst out to a certain "performance" based on how powerful your CPU is.


How to change color of button with javascript/jquery when input (radio buttons) is selected

Question:

 <button id="id1" class="button">button1</button>
 <button id="id2" class="button">button2</button>
 <button id="id3" class="button">button3</button>

  <form action="" id="formId1">
   <input type="radio" name="gender" id="id1"  value="male"> Male

   <input type="radio" name="gender" id="id1"  value="male"> female

   <input type="radio" name="gender" id="id1"  value="male"> others

  </form>

  <form action="" id="formId2">
   <input type="radio" name="gender" id="id2"  value="male"> Male

   <input type="radio" name="gender" id="id2"  value="male"> female

   <input type="radio" name="gender" id="id2"  value="male"> others

  </form>

  <form action="" id="formId3">
   <input type="radio" name="gender" id="id3"  value="male"> Male

   <input type="radio" name="gender" id="id3"  value="male"> female

   <input type="radio" name="gender" id="id3"  value="male"> others

  </form>

If input with id id1 is selected in form with id formId1 then color of button with id id1 should be change and If input with id id2 is selected in form with id formId2 then color of button with id id2 should be change and same for third button also.
Comment: Can you please indicate anything you've tried.  If you haven't tried anything, what have you researched.
Comment: An Id attribute should always be unique. There are several elements in your code with same id. Take care of that.
Comment: @vapurrmaid Thank you, I will remember that so i don't do same mistake again in future
Comment: In the future, if asking for a javascript/jquery solution, you should post the javascript/jquery you've tried. Questions are better suited towards "why isn't this working" or "what am i doing wrong" or "ive tried this". When given just the HTML, it seems like you want SO to write code for you. Please see https://stackoverflow.com/help/how-to-ask
Answer: You can call function on click to set the button color,
   <input type="radio" name="gender" id="id1"  onclick="handleClick('id1')"  value="male"> Male

In this code we have added onclick="handleClick('id1')"

handleClick =function(id){

// uncomment if you want to reset other button
/*
for(var i=1 ;i<4;i++){
     var button = document.getElementById("id"+i);
      button.style.backgroundColor = "buttonface";
}*/
     var property = document.getElementById(id);
      property.style.backgroundColor = "red";
 }

 <button id="id1" class="button">button1</button>
 <button id="id2" class="button">button2</button>
 <button id="id3" class="button">button3</button>

  <form action="" id="formId1">
   <input type="radio" name="gender" id="id1"  onclick="handleClick('id1')"  value="male"> Male

   <input type="radio" name="gender" id="id1"onclick="handleClick('id1')"   value="male"> female

   <input type="radio" name="gender" id="id1"onclick="handleClick('id1')"  value="male"> others

  </form>

  <form action="" id="formId2">
   <input type="radio" name="gender" id="id2" onclick="handleClick('id2')"   value="male"> Male

   <input type="radio" name="gender" id="id2"  onclick="handleClick('id2')"  value="male"> female

   <input type="radio" name="gender" id="id2" onclick="handleClick('id2')"   value="male"> others

  </form>

  <form action="" id="formId3">
   <input type="radio" name="gender" id="id3" onclick="handleClick('id3')"   value="male"> Male

   <input type="radio" name="gender" id="id3" onclick="handleClick('id3')"   value="male"> female

   <input type="radio" name="gender" id="id3" onclick="handleClick('id3')"   value="male"> others

  </form>

Comment: You are always welcome @chiragprajapati, If your problem solved and you find this helpful, accept the answer and vote so that others can find it helpful
Answer:

     <button id="id1" class="button">button1</button>
     <button id="id2" class="button">button2</button>
     <button id="id3" class="button">button3</button>

      <form action="" id="formId1">
       <input type="radio" name="gender" value="male" onchange="setColor('id1')"> Male

       <input type="radio" name="gender" value="male" onchange="setColor('id1')"> female

       <input type="radio" name="gender" value="male" onchange="setColor('id1')"> others

      </form>

      <form action="" id="formId2">
       <input type="radio" name="gender"  value="male" onchange="setColor('id2')"> Male

       <input type="radio" name="gender"  value="male" onchange="setColor('id2')"> female

       <input type="radio" name="gender" value="male" onchange="setColor('id2')"> others

      </form>

      <form action="" id="formId3">
       <input type="radio" name="gender"  value="male" onchange="setColor('id3')"> Male

       <input type="radio" name="gender"  value="male" onchange="setColor('id3')"> female

       <input type="radio" name="gender"  value="male" onchange="setColor('id3')"> others

      </form>

Comment: You are always welcome. If your problem solved.. Mark the answer.


From Column to Array Scala Spark

Question: I am trying to apply a function on a Column in scala, but i am encountering some difficulties.
There is this error
found   : org.apache.spark.sql.Column
required: Array[Double]

Is there a way to convert a Column to an Array?
Thank you
Update:
Thank you very much for your answer, I think I am getting closer to what I am trying to achieve. I give you a little bit of more context:
Here the code:
object Targa_Indicators_Full {

  def get_quantile (variable: Array[Double], perc:Double) : Double = {
  val sorted_vec:Array[Double]=variable.sorted
  val pos:Double= Math.round(perc*variable.length)-1
  val quant:Double=sorted_vec(pos.toInt)
  quant
  }

def main(args: Array[String]): Unit = {

 val get_quantileUDF = udf(get_quantile _)

 val plate_speed =
 trips_df.groupBy($"plate").agg(sum($"time_elapsed").alias("time"),sum($"space").alias("distance"),
 stddev_samp($"distance"/$"time_elapsed").alias("sd_speed"),
 get_quantileUDF($"distance"/$"time_elapsed",.75).alias("Quant_speed")).
 withColumn("speed", $"distance" / $"time")

}

Now I get this error:
type mismatch;
[error]  found   : Double(0.75)
[error]  required: org.apache.spark.sql.Column
[error]  get_quantileUDF($"distanza"/$"tempo_intermedio",.75).alias("IQR_speed")
                                                         ^
[error] one error found

What can I do?
Thanks.
Comment: What do you expect to have in the array?
Comment: I am trying to find a specific element in the array after it has been sorted. The thing is that I am applying this function on a column, hence the error
Comment: Can you show the format of the column please?
Comment: You cannot directly use the literal. You have to convert it using lit eg: df.withColumn("new_col", lit(10))
Answer: You cannot directly apply a function on the Dataframe column. You have to convert your existing function to UDF. Spark provides user to define custom user defined functions(UDF).
eg:
   You have a dataframe with array column
scala> val df=sc.parallelize((1 to 100).toList.grouped(5).toList).toDF("value")
df: org.apache.spark.sql.DataFrame = [value: array<int>]

You have defined a function to apply on the array type column
def convert( arr:Seq[Int] ) : String = {
  arr.mkString(",")
}

You have to convert this to udf before applying on the column
val convertUDF = udf(convert _)

And then you can apply your function:
df.withColumn("new_col", convertUDF(col("value")))

From Column to Array Scala Spark


Is it possible to have multiple different bases of the same space?

Question: I am studying about spaces, basis, and span. I know that a basis $S$ is a set of vectors that span a space $V$. Therefore, we could represent any vector in $V$ as a linear combination of $S$.
So, I am wondering if for a space $V$, is it possible to have two distinct bases $S_1$ and $S_2$ such that $S_1 \neq S_2$? If all vectors can represented as the basis, they should be the same, right? Is a base unique for a space?
Comment: Related: http://math.stackexchange.com/questions/468339/necessary-condition-of-a-vector-space-having-only-one-basis
Comment: Bases are highly non-unique. Any two bases have the same cardinality. That's about all one can say.
Comment: Generally, a vector space has many different bases. For example, in $\mathbb{R}^2$, consider $\{(1,0),(0,1)\}; \{(1,0),(1,1)\}; \{(2,3),(17,2)\};\, \dotsc$
Comment: Ok guys, I got it, thanks.
Answer: Your definition of basis is not totally correct. A basis for $V$ is a linearly independent set $S\subset V$ such that $S$ spans $V$.
It is not hard to check that any vector space (over an infinite field) has infinitely many bases. In a trivial way, you could vary the length of the vectors to get a different basis, and of course you can do this in infinitely many ways.
But let me show with a simple example how to get infinitely (actually, uncountably many) different bases in $\mathbb R^2$, and all basis with vectors of length one. Fix first the canonical basis in $\mathbb R^2$, i.e. $\{(1,0),(0,1)\}$. And now take the two vectors and, preserving the square angle between them, rotate them. You will get again a basis (actually the new basis is also orthonormal), and a different one for every rotation angle $\theta$.
Explicitly, the basis after a rotation $\theta$ is
$$
\{(\cos\theta,\sin\theta),(\sin\theta,-\cos\theta)\}.
$$
Comment: Further nitpick: $\{0\}$ has only one basis.
Comment: (nitpicking) You can have a finite vector space.
Comment: Good point. I added "over an infinite field" to satisfy the nitpickers :)


Usar o include com Jquery Mobile?

Question: Eu gostaria de fazer uma página assim como é feito com Razor MVC (_Layout) com cabeçalho, e rodapé, e fazer a inclusão dele nas demais páginas, como eu poderia fazer isso?
Quero fazer o AJAX Load Content
Veja o exemplo que fiz:
_Layout.html

<!DOCTYPE html>

 <meta charset="utf-8">
 <title>Treinamento</title>
 <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1">
    <link rel="stylesheet" href="css/jquery.mobile-1.3.0.min.css">

Cardápio

            Home

adicionar o conteúdo de todas as páginas externas

Página de Teste

pagina_teste.html

esté é o conteúdo da página

controle.paginas.js

$(document).ready(function () {
    $('#conteudo').load('conteudo/pagina_teste.html');

    $('ul#nav li a').click(function () {
        var page = $(this).attr('href');
        $('#conteudo').load('conteudo/' + page + '.html');
        return false;
    });

});

Comment: Pelo que eu entendi, você quer customizar um Layout para web e outro para mobile, é isso?
Comment: Você incluiu a tag **asp.net-mvc**. Você está usando este framework? Se sim, por que não usa o Razor então?
Comment: Estou usando apenas Jquery puro, estou querendo fazer um  AJAX Load Content, eu alterei o conteúdo da minha pergunta, veja se pode me ajudar. agradeço
Answer: Um jeito de fazer o que você quer é usar o load do jQuery.
Digamos que você tem os arquivos:
header.html

Minha página

footer.html

     Conteúdo do rodapé

Então, no seu arquivo principal:

    <title></title>

Dessa forma, o conteúdo do arquivo header.html será incluído dentro da tag  e do arquivo footer.html na tag .
Se quiser usar links, como no seu código, terá que interromper o evento padrão de navegação, usando o método preventDefault():
$(document).ready(function () {
    $('#conteudo').load('conteudo/pagina_teste.html');

    $('ul#nav li a').click(function (event) {
        event.preventDefault();
        var page = $(this).attr('href');
        $('#conteudo').load('conteudo/' + page + '.html');
        return false;
    });

});

Comment: Marcus, eu estou querendo fazer um  AJAX Load Content, eu alterei o conteúdo da minha pergunta, veja se pode me ajudar. agradeço
Comment: Bem @MarcusVinicius, ficou muito interessante sua resposta, mas e se no arquivo principal eu quisesse me livrar daquelas tags padrões como `` e `` além de carregar scripts padrões para todas as páginas, como acontece no projetos que usam `Razor`, isso seria possível?
Comment: Não, nada como o Razor. Sem usar nenhuma tecnologia do lado do servidor fica difícil chegar nesse nível de reutilização. [Existe um rascunho da W3C para permitir inclusão de HTML apenas com tag ``](http://www.w3.org/TR/html-imports/), mas ainda é muito incompatível com os browsers atuais.

Usar o include com Jquery Mobile?


Como remover um caractere repetido em sequência?

Question: Como faço para remover os caracteres repetidos em sequencia com PHP?
Entrada:

Ellizabetth

Saída:

Elizabeth
Answer: Pode usar o preg_replace, um exemplo:
$var = 'Elliiiiiiiiiiizabetth';
$pattern = '/(.)\1+/';
$replace = '$1';
$resultado = preg_replace($pattern, $replace, $var);
echo $resultado;

Resultado:
Elizabeth

Outro exemplo, em uma única linha, com o mesmo resultado:
echo preg_replace('/(.)\1+/', '$1', 'Elliiiiiiiiiiizabetth');

Comment: Se está usando `'` ao invés de `"` não vejo motivo para usar `\\ `, pode usar `'/(.)\1+/'` ou `'/(.*)\1+/'`. Se estivesse usando `"` deveria usar `"/(.)\\1+/"`, com `\\ `. De qualquer forma, ambos funcionam normalmente.
Comment: Inkeliz... grato pelo comentário. Já ajustei na pergunta.
Answer: Uma outra alternativa é:
function unique($palavra){
    $p = str_split($palavra);
    return implode(array_map(function ($c) use ($p) {
                      return ($c > 0 && $p[$c] == $p[$c - 1] ? '': $p[$c]);
    }, array_keys($p)));
}

echo unique("Banana") . "\n";        // Banana
echo unique("Arara") . "\n";         // Arara
echo unique("assassinos") . "\n";    // asasinos
echo unique("Marreco") . "\n";       // Mareco
echo unique("Elllizabettth") . "\n"; // Elizabeth
echo unique("FooBaaar") . "\n";      // FoBar
echo unique("Woow") . "\n";          // Wow
echo unique("baazz") . "\n";         // baz

Ver demonstração
Uma segunda alternativa é percorrer a string num loop for e verificar se a letra atual é igual a letra anterior:
function unique2($palavra){
    $ret = "";

    for ($i = 0; $i < strlen($palavra); $i++){
        if ($i == 0 || $palavra[$i] != $palavra[$i - 1]) $ret .= $palavra[$i];
    }
    return $ret;
}

echo unique2("Banana") . "\n";        // Banana
echo unique2("Arara") . "\n";         // Arara
echo unique2("assassinos") . "\n";    // asasinos
echo unique2("Marreco") . "\n";       // Mareco
echo unique2("Elllizabettth") . "\n"; // Elizabeth
echo unique2("FooBaaar") . "\n";      // FoBar
echo unique2("Woow") . "\n";          // Wow
echo unique2("baazz") . "\n";         // baz

Ver demonstração
Nota
Ambas as funções são case-sensitive, ou seja, difere letras maiúsculas e minusculas, para resolver isso use a função strtolower em ambas as letras que serão comparadas, ou fazer a comparação usando a função strcasecmp.


Attaching a library to only one custom module

Question: I wrote a sample custom module (juniper) and created the corresponding juniper.libraries.yml file to link the CSS and JS. I am trying to load this library only on this module pages (/juniper). However, I have issues:

I tried to add the library via juniper.info.yml:

libraries:
  - juniper/june

but this doesn't work for some reason

I added the juniper_page_attachments hook to the juniper.module

function juniper_page_attachments(array &$page) {
  //$page['#attached']['library'][] = 'juniper/june';
}

but it added the library to the whole site, not just the module.
Could you please help me figure out how to limit the library loading only to the custom module page I created? Thanks!
Comment: What does this mean: _"but it added the library to the whole site, not just the module."_?
Answer: There's a few different ways of doing this, depending on how you're building /juniper.

If you have a controller that is returning a render array, you can include something like $build['#attached']['library'][] = 'juniper/june'; to just include it for that page.

If you're going to use this library on any /juniper/* page, then something like a hook_page_attachments could be used with logic similar to this:

/**
 * Implements hook_page_attachments().
 */
function juniper_page_attachments_alter(array &$attachments)
{
  $route_uri = \Drupal::request()->getRequestUri();

  if(str_starts_with($route_uri, '/juniper/')) {
    $attachments['#attached']['library'][] = 'juniper/june';
  }
}

Another solution could be to check out \Drupal::routeMatch() if you want to attach to particular routes, or \Drupal::currentUser() if you only want to attach for particular users.
Comment: Yes, the first option is the preferred way and has the desired result that the library is attached when the module is outputting HTML for the library. See https://drupal.stackexchange.com/questions/237336/attach-library-to-a-specific-page. The second option should be used only if the first is not possible. It works for the current route match, but not for the current user. Pages are not cached by user.
Comment: Thank you! The second option worked for me, I just had to slightly modify the route:
str_starts_with($route_uri, '/juniper') to match just '/juniper'
Answer: You can't attach libraries from module info files. That only works for themes. And yes, if you attach in the page attachments hook without any other condition on the URL or whatever it will be added on every site.
Preferably you only attach to the element where you need either from inside a template via {{ attach_library('your_module/library_name') }} or from a render array maybe from a preprocess hook like hook_preprocess_paragraph etc.
But this is already documented really well in the official docs: Adding assets (CSS, JS) to a Drupal module via *.libraries.yml.


how to approximate this expression $\frac{1}{8}x^2(1-\frac{1}{12}x^2)/(1-\frac{1}{4}x^2)$

Question: when x is small, for example <1, then the expression can be approximate by (from a book)
$$ g(x)= \frac{-x^2}{8}{\frac { \left( 1-1/12\,{x}^{2} \right) }{1-1/4\,{x}^{2}}
}= \frac{-x^2}{8} \frac{1}{1-1/6{x}^{2}} $$  (1.1)
let $$ f(x)= {\frac { \left( 1-1/12\,{x}^{2} \right) }{1-1/4\,{x}^{2}}
}$$
the  expression $g(x)$ should be dimensionless, so there should be keep a $ {x}^{2}$ in the denom part.
I have known that we can do this using the taylor series, like
$taylor(f(x),x)$ is $(1+{\frac {1}{6}}{x}^{2}+{\frac {1}{24}}{x}^{4}+O \left( {x}^{6}
 \right) )
 $
but this not in denom,
a trick is $taylor(1/f(x),x)$ ,which will be $(1-{\frac {1}{6}}{x}^{2}-{\frac {1}{72}}{x}^{4}+O \left( {x}^{6}
 \right) ) $
so we can get the approximate like the
$$ g(x)= \frac{-x^2}{8}{\frac { 1 }{1-1/6\,{x}^{2}}
}$$
could other person give another method more directly, because I think this is too tricky.
and the second problem is, why we can not just in the numer make $1-x^2/12=1$, and the denom part not changed.
howerver this will get a expression $$ g(x)= \frac{-x^2}{8}{\frac { 1 }{1-1/4\,{x}^{2}}
}$$  (1.2)
(1.2) is not good like (1.1), we can just take a taylor, then we can compare.
but from the first eye, $x^2/8*(1-x^2/12)$ have a term $x^4$ ,when x is small, this can be dropped.
but the fact is cannot, why? Does this means,if $x is not <<1$, but only $<1$, then we can not to drop a term, but have to do a taylor siries.
drop a term is only when the $x->0$, but taylor could get better approximation. even the term is two order high than other terms
comment, these approximate come frome a acoustic problem for a duct absorption problems, the impedance have a part like $$\left[ 1-\dfrac {2} {\sqrt {-i}k}\dfrac {J_{1}\left( \sqrt {-j}k\right) } {J_0\left( \sqrt {-jk}\right) }\right] ^{-1}$$
take the taylor series for $J_1$ and $J_0$, then get the polynom, the do another approximate, do this approximate, we can take the low frequency and high frequency easy.
Comment: It is hard to see the function. At least could you put parentheses or tell what the numerator and what is the denominator ?
Comment: This site uses MathJax to format formulars so the look like real math.  There a a tutorial here [MathJax basic tutorial and quick reference](http://meta.math.stackexchange.com/q/5020/86986).   As it stands I'm not 100% sure what your formulas are.
Comment: **1.** To convert an equation into MathJAX, just enclose it in dollar signs, so use \$x>8\$ to show $x>8$. **2.** For a fraction, use \frac{numerator}{denominator} to show $\frac{numerator}{denominator}$. **3.** For exponents use {base}^{exponent} to show ${base}^{exponent}$.
Answer: $$\frac{1}{8}x^2\frac{1-\frac{1}{12}x^2}{1-\frac{1}{4}x^2}=\frac18x^2\frac1{(1-(1/4)x^2)(1-(1/12)x^2)^{-1}}\approx\frac18x^2\frac1{(1+(1/12-1/4)x^2)}=\frac18x^2\frac1{(1-(1/6)x^2)}$$
As:
$$(1+x)^n=1+nx+n(n-1)x^2/2+...\approx 1+nx\tag{$x\ll1$}$$
$$(1+ax)(1+bx)=1+(a+b)x+abx^2\approx1+(a+b)x\tag{$x\ll1$}$$
Comment: a good answer, first change form, then taylor, is more natural than take taylor of the $1/f(x)$, thanks!


Extract function argument description from R package

Question: I know there are ways to extract all arguments to a function using, for example, rlang::fn_fmls. But is it possible to extract the description of one of these arguments from the package documentation?
For example, what if I wanted to extract the description of the na.rm argument to base::sum()?
I'm imagining something like:
get_argument_description(fn = 'base::sum', arg = 'na.rm')
Return:
"logical. Should missing values (including NaN) be removed?"
Answer: You could try to read the associated help file for that function, and grep the line where \item{argument}. However, multi-line help texts are allowed, if the next line does not start with a \ you would want to grab that too.
This answer shows a way to acess the file, then it is just a matter of grabbing the correct line(s). I also want to highlight a different function in tools,
tools:::.Rd_get_text()

Which almost gets you where you want, (if you find the correct line)
library(tools)
db = Rd_db("base")
tools:::.Rd_get_text(x = db[["sum.Rd"]])[21] # 21 is the line you want here

[1] "   na.rm: logical.  Should missing values (including 'NaN') be removed?"

Comment: This does indeed get me closer. Thank you!


Using field api to get key of select list option

Question: I need the safe output of a key from a select list field within a field collection item.
I'm theming a field collection item and one of the fields is a select list like:
icon-map|Map
icon-info|Info
icon-pencil|Pencil

I'm using the following to create some easy to use variables;
$fields = array_keys($content);
foreach ($fields as $field) {
  $field_items = field_get_items('field_collection_item', $content[$field]['#object'], $field);
  $$field = render(field_view_value('field_collection_item', $content[$field]['#object'], $field, $field_items[0]));
}

This works fine for all fields except for the select list.  I need the key of that select list.  For this select list field, $field_items is an array which looks like:
array(1) { [0]=> &array(1) { ["value"]=> string(13) "icon-map" } }

However, $field_icon (result of $$field = render(..) ) is Map, which makes sense.
I can get the key by using $field_items[0]['value'] but I'm not sure that's the best way.
Answer: I hope this may help
 $key = $form_state['values']['whatever'];
$options = list_allowed_values("field_whatever");
$value = $options[$key];

Comment: then in that case I suppose wat @Chris Rockwell has given  $field_items[0]['value']  is the right choice to get the key.
Comment: This is right, but it's possible for it not to work (if the allowed values have been altered in the form, `list_allowed_values` won't bring back the same as what was presented to the user, for example). In that case you could also check `$form['whatever']['#options']`, assuming you're in a validate/submit handler
Comment: else use this method ,
$items = field_get_items('node', $node, 'field_list');
$key = $items[0]['value'];

If you need to match those up again at any point you can get the full key/value list from the field metadata:

$info = field_info_field('field_list');
$values = $info['settings']['allowed_values'];
$label = $values[$key];
Comment: Same problem with that method @user3783391 - you get the settings for the field, not necessarily what was actually used in the form


How to update column in database in blade.php file Laravel

Question: Hello I have row in a database table have an id and name and etc.. and active = 1
I want in blade.php file when someone click a button it change into 0 in 1 click
What I tried is {{$referral_detail->update('active', 1)}}
And
{{$referral_detail->IDT->where('active', 0)->update('active', 1)}}

Function in Controller is
public function DriverReferAll()
{
    $query = DriverReferralDiscount::where([['referral_driver_id', '!=', 0],
        ['referral_sender_id', '!=', 0], ['active', '<>', 1]
    ]);

    $referral_details = $query->paginate(25);

    return view('merchant.driver.driver_referall', compact('referral_details', 'query'));
}

I got it thedit('{{$referral_detail->IDT->update(['active' => 1])}}')
and function
function thedit(a) {

    $("input:submit").val(a);
}

But when I do it , it change the first 2 column not only 1 tho I put the specific id any idea?
its in onClick""
IDT->update(['active' => 1])}}')
                                        " data-target="#addMoneyModel"
                                              data-toggle="modal" id="{{ $referral_detail->GetterDriver->id }}">GetterDriver->id }}" data-placement="top"
                                                    class="btn text-white btn-sm btn-success menu-icon btn_detail action_btn">

Answer: For that you must create a link in your blade template when that link is clicked It must have an href which point to the laravel route which will perform the update of your active state.
 $referral_detail->id]) }}>Change

the route can be define like this
Route::get('/route_path/{id}', 'ControllerName@action_name')->name('route_name');

Or If you want the browser to not refresh you can perform and Ajax
Comment: Oh thanks! but what should i put in the function? is there a function or just that?
Comment: Like I said you must create a controller which will receive the ID of the item you want to update and in that controller you retrieve that Item from the database  and perform the update
Comment: I did this its missed up ?

public function editUpdate($id)
    {
        DriverReferralDiscount::where([['id', $id]])->update(['active' => 1]);

        return redirect()->back()->with('History', 'Done');
    }
Comment: Thank you Worked!! can i put 2 actions for 1 button? like 2 routs? to do 2 things?


Uniform continuousness: are only the "extremes" interessant? (poles and infinity)

Question: Help me please understand the concept of uniform continuousness.
$f(x)=x$ is it, and I get that.
But what is with $g(x)=x^3/(x^2+1)$ (in reals)
Just arguementing unrigorously, i'd say this isn't very different than $f(x)=x$, the limit is "the same" (if you can even say that about different infinities).
And more importantly the $y$-difference will be almost the same with large enough $x$, $x_0$.
And if i had something like $h(x)=x^3/(1-x)$, would be the only interessant locations be $x \to 1$ and $x \to \pm \infty$ ?
Note: I just made up this, so please don't post the solution to: "is $g(x)$ uniform continuous", help me understanding the concept behind it.
Comment: Sorry, I made a mistake on my answer. I'll see if I can salvage something on it.
Comment: @Aloizo i have screenshotted my lazy browser since i though that way https://postimg.org/image/bam9adlcn/  if you need the latexcode for timesafing say
Comment: I updated the answer.
Answer: Uniform continuous means you can, given $\varepsilon$, in the $\varepsilon, \delta$ definintion of continuity, the value of $\delta$ uniformly for  all $x$.
In case you have a pole (a zero of a polynomial in the denominator without a zero in the nominator) this is never true near the pole, but these are not the only examples. (I don't prove this but only tell you it's true, leaving the $\varepsilon\delta $ stuff as an exercise to you -- in the end you will only learn this by doing it yourself)
One of the most instructive examples (in my opinion) is the exponential function, which is not uniformly continuos. To see this choose $\varepsilon =1$. If you choose any $\delta >0 $ you now have to find $x$ such that, say $f(x)-f(x-\delta/2) > \varepsilon$ -- this is rather easy (choose $x$ suffiently large) and I leave it to you as an exercise as well.
Edit: (in order to answer the question about the extremes) there is a theorem which says that a continuous function is uniformly continous on each bounded closed interval. So in a way, yes, it's mostly the extremes which are relevant.
Answer: What you really want is to understand the difference between continuity and uniform continuity, so let's start with the definition of continuity.  (I'll stick to functions of a single variable because for understanding this difference you don't have to introduce anything more complicated.)
A function $f(x)$ is continuous at point $x_0$ iff for any given $\epsilon$ there exists some $\delta$ which may depend on $\epsilon$ and on $x_0$ such that $|x-x_0| < \delta \implies |f(x) - f(x_0)| < \epsilon$.  And $f(x)$ is continuous in some range if it is continuous at every point in that range.
Now look at the definition of uniform continuity and see how it is different:
A function $f(x)$ is uniformly continuous at in some range $R$ iff for any given
$\epsilon$ and given $x_0$ in $R$, there exists some $\delta$ which may depend on $\epsilon$ but not on  $x_0$ such that $|x-x_0| < \delta \implies |f(x) - f(x_0)| < \epsilon$.
So if you look at your $g(x)$ in a range that includes all the reals, can you express a suitable $\delta$ based on a given $\epsilon$ using a formula that does not depend on the point $x_0$ you are testing at?
Answer: This is a nice example to try, stop, and make your intuition rigorous. What your intuition apparently materialized is the following, written rigorously:

Given $f,g: \mathbb{R}\to \mathbb{R}$ such that $f$ is continuous, $g$ is uniformly continuous and $\lim\limits_{x \to \infty}f(x)-g(x)=A \in \mathbb{R}$ and $\lim\limits_{x \to -  \infty}f(x)-g(x)=B \in \mathbb{R}$. Then $f$ is uniformly continuous.

And this is true.
Proof: Define $h(x):= f(x)-g(x).$ We have $h$ is continuous function on the real line. By hypothesis, $h$ has finite limits at infinity, and it is well-known  that a continuous function on the real line with finite limits at infinity is uniformly continuous. Now, we have that $f(x)=h(x)+g(x)$, and since $h$ and $g$ are uniformly continuous, so is $f$.
$\blacksquare$
Now apply your lemma to $f(x)=\frac{x^3}{x^2+1}$ and $g(x)=x$. We have that
$$f(x)-g(x)=\frac{x^3-x^3-x}{x^2+1}=\frac{-x}{x^2+1} \stackrel{x \to \pm \infty}\to 0.$$
Comment: Hi, but in my example, g(x) vanishes in the point x=0 or not? or do you mean "doesn't vanish at large values"? (since your limit is at large values we really don't care about x=0 or whatever there can be)
Comment: maybe the solution to the "problem" is: f and g have to be vanishing at the same point(s), if you understand what i mean, since then only the limit as f/g approaches V is relevant or?
Comment: why do we know that h in your proof is continuous on the real line?
Comment: is it because f(x)-g(x)=(-1)/(x^2+1) and this is continuous?
Comment: ah, i misunderstood, your proof hadnt anything to do with actual functions. but then again, why do we know that h will be continuous? is there a lemma that states conti+conti=conti?
Comment: @saturatedexpo Yes, sum of continuous functions is continuous.


Remove count from glossary view

Question: I have a situation where I need to remove (via client request) the letter count in the header of a view based on Glossary view. How might I remove the numbers (in parenthesis)

Previous question history: Paginate views by first letter of Node title
Answer:
Go to the Attachment details of the Glossary view
Under Contextual Filters (you may need to click Advanced to see it) click on "Content: Title"
Uncheck "Display record count with link"
Answer: Copy views-view-summary-unformatted.tpl.php to your theme and edit the file and remove line 17 (<?php print $row->count; ?>)
or wrap line 17 (<?php print $row->count; ?>) with a span and hide it using css
(<?php print $row->count; ?>)

Answer: Since I can't comment, I have to add an answer. I agree with Bill's answer. This also works for Drupal 8 and no code change is required. De-selecting "Display record count with link" will remove the parenthesis.

Remove count from glossary view


Dúvida em código em C!

Question: Pessoal alguém poderia me ajudar nessa questão da minha lista de exercicio:
Faça um programa que receba um número real, encontre e mostre:

a) a parte inteira desse número;
b) a parte fracionária desse número;
c) o arredondamento desse número.

Não sei como calculo isso, meu professor mandou utilizar o modf, mas não funcionou, dando uma busca na web encontrei o seguinte código que ao testá-lo rodou perfeitamente, mas eu não consigo entender a logica que foi utilizada.
<code>float numero,inteira,fracao,arred;
printf("Digite um numero real: ");
scanf("%f%*c",&numero);
printf("Parte Inteira : %d \n",(int) numero);
printf("Parte Decimal : %f", numero - ((int)numero));
</code>
Comment: C não é C# e não é C++.
Comment: a dúvida é sobre como fazer ou o que o algoritmo que você encontrou faz?
Comment: Como fazer,eu não entendi como o codigo  funciona
Answer: Colocar (int) antes de uma variável retorna somente a parte inteira do numero (sem arredonda-lo), assim no segundo print ele mostrou somente a parte inteira, dessa forma que expliquei, no terceiro printf subtraiu a somente a parte inteira do numero com ponto flutuante, sobrando somente a parte decimal, e para o quarto printf que seria relativo ao arredondamento você poderia utilizar a função round(variavelFloat), que arredonda a variável.
O código ficaria assim:
<code>{
    float numero,inteira,fracao,arred;
    printf("Digite um numero real: ");
    scanf("%f%*c",&numero);
    printf("Parte Inteira : %d \n",(int) numero);
    printf("Parte Decimal : %f", numero - ((int)numero));
    printf("Arredondado : %d", (int)round(numero));
}
</code>
Comment: @JeffersonQuesado há controvérsias :) Tem maneiras diferentes de fazer o  floor . Claro, sempre pegará o piso, mas a definição do que é o piso pode mudar.
Comment: Era isso que não tinha entendido,muito obrigado cara vc me salvou kk
Comment: `floor` não vai arredondar o número do jeito desejado. `floor` vai truncar o número. É a operação `piso` da matemática: vai arredondar para o inteiro mais baixo

Dúvida em código em C!


Documenting networking infrastructure

Question: I'm looking for Open source web interface for documenting the active and passive parts of a networking infrastructure, cabling.
Answer: I think Dia would help you. http://www.osalt.com/dia
Good luck!
Comment: I suspect that, unlike so many other graphics editors/drawing tools (OpenOffice Draw, etc...), Dia is your best bet. But I wouldn't be surprised if there's network templates for others, like Inkscape.
Comment: Thanks for bringing OSALT.com to my attention, it makes for a nice alternative to alternativeto.net :-)
Answer: Gliffy and Creately are online tools for making network topology drawings.
They are free to use, although perhaps limited in features.
However, I'm not either of these fit your open source requirement.
Google now also has Drawings as part of their Google docs offering, which is meant to compete with Microsoft's Visio.
Answer: Have you tried Lucid Chart? They have some great network diagrams examples.
Comment: I don't think that's an open source product.
Comment: This is not open source and does not address the question.

Documenting networking infrastructure


Simple self-hosted website monitoring

Question: I'm looking for a simple self hosted website monitoring tool. It should be somthing similar to watchmouse.com or pimgdom.com, with a nice UI, colorful charts and so on (Customers like that :)). At the moment we use Zabbix also for HTTP monitoring, but since now our hoster care about the hardware and software monitoring on the machine directly, we don't need Zabbix anymore. For pure http-monitoring zabbix or an other monitoring suite is really an overkill.
So what I'm not looking for is:
Zabbix
Nagios
Hyperic
....
Sadly but the truth, after some hours of researching I wasn't able to find a fitting application. My hope is now on you.
Thanks in advance.
Comment: Sorry, when you say self hosted, do you mean installed and run on your servers?
Comment: Yesm this is what I mean :)
Answer: Forget self hosting monitoring and use something like monitis - the big advantage is that it checks from multipel external (!) locations and is free / cheap, depending how you check.
This way you not only know that your HTTP server works well (it can cehck outout of specific URL's) but also that you are reachable from various international locations.
Comment: yup. self-monitoring is a contradiction in my opinion. if your network/host/etc is down, your monitor is likely afflicted by the same outage as the webserver....this almost necessarily must be done by a third-party
Answer: How about awstats? http://awstats.sourceforge.net/ This can run on the webserver itself, analyses the logfiles and generates graphs. There's a bit of a learning curve first time you set it up, but when you've done it once its fairly simply to install on a new system.
Answer: How about Circonus?
http://circonus.com/
I've used it before and it's the right mix of colourful graphs and monitoring. The have a hosted and non-hosted solution (Enterprise Agents) that may suit your needs.

Simple self-hosted website monitoring


How to check whether maximum likelihood estimation optimizer has converged in R?

Question: I got AIC values of all models to identify the best model using R language. As I heard, best model produce the smallest AIC value, but maximum likelihood estimation procedure optimizer should converge.
How can I check whether maximum likelihood estimation procedure optimizer has converged or not in R language?
Comment: What R function are you talking about ?
Comment: I have already fitted several models using R code; arima(rates,c(p,d,q))
Answer: If you're using the arima function as
mod <- arima(rates,c(p,d,q))

then the convergence status from the underlying optim routine is
mod$code

If this is 0 then you converged, at least as far as optim was concerned.  See ?optim for details.
This is all in the help page: ?arima
Answer: Assuming you have a globally convex likelihood and a regular parameter space, then the optimizer may have not converged when it reaches a boundary of the parameter space. This yeilds parameter estimates which explode and information matrices which are singular. Usually any graphical method is good at diagnosing that. Some estimation problems have unique solutions which are on the boundary of the parameter space, though, such as separation of outcomes in univariate logistic regression, giving odds ratios which are $\infty$ or 0.


Induction proof for sum $\sin(\theta)+\sin(3\theta)+\cdots$

Question: I would like to prove by induction that
$$\sin(\theta)+\sin(3\theta)+\dots+\sin((2n-1)\theta)=\frac{\sin^2(n\theta)}{\sin(\theta)}$$
for $n\in\mathbb{Z}$. (This is a question from the International Baccalaureate Mathematics HL textbook by Fannon et al.; I teach the course.)
I am aware that this answer deals with the category of problems, but the solutions provided there are not induction proofs and do not obviously translate to induction proofs. In the office we managed to 'brute force' it but the solution seemed to require identities beyond the apparent level of the question.
Comment: Please, if you are ok, you can accept the answer and set it as solved. Thanks!
Answer: you have to prove that
$$\frac{\sin^2(nx)}{\sin(x)}+\sin((2n+1)x)=\frac{\sin^2((n+1)x)}{\sin(x)}$$
and work backwards
it must be
$$\sin(x)\sin((2n+1)x)=\sin^2((n+1)x)-\sin^2(nx)$$
can you prove this?
and use $$\sin(x)-\sin(y)=2 \sin \left(\frac{x}{2}-\frac{y}{2}\right) \cos
   \left(\frac{x}{2}+\frac{y}{2}\right)$$
and
$$\sin(x)+\sin(y)=2 \sin \left(\frac{x}{2}+\frac{y}{2}\right) \cos
   \left(\frac{x}{2}-\frac{y}{2}\right)$$
Answer: Base case: n=1
$$\sin(\theta)=\frac{\sin^2(\theta)}{\sin(\theta)}=\sin(\theta)$$
Inductive step: n -> n+1
Let's assume that the statement is true for n:
$$\sum_{k=1}^{n}\sin[(2k-1)\theta]= \sin(\theta)+\sin(3\theta)+\dots+\sin[(2n-1)\theta]=\frac{\sin^2(n\theta)}{\sin(\theta)}$$
Let's verify that it holds also for $n+1$, that is:
$$\sum_{k=1}^{n+1}\sin[(2k-1)\theta]=\frac{\sin^2[(n+1)\theta]}{\sin(\theta)}$$
Since:
$$\sum_{k=1}^{n+1}\sin[(2k-1)\theta]=\sum_{k=1}^{n}\sin[(2k-1)\theta]+\sin[(2n+1)\theta]= \frac{\sin^2(n\theta)}{\sin(\theta)}+\sin[(2n+1)\theta]$$
Finally we have t prove that:
$$\frac{\sin^2[(n+1)\theta]}{\sin(\theta)}=\frac{\sin^2(n\theta)}{\sin(\theta)}+\sin[(2n+1)\theta]$$
Comment: My question was really about how to prove the final statement; as I wrote in the question I wasn't able to find a way that didn't use identities beyond the apparent level of the question.
Comment: Sorry I didn't catch that your question concerned with this. Thus you would want to prove the statement without use well-known identities?


Where do I go in Windows Server 2012 to observe the replication of my domain information?

Question: I am looking for and have not found where the user interface of Windows Server 2012's AD and Server Management tools, I can see the replication status of domain (Active Directory) data replication, similar to the stuff that you could query using dfsrdiag from the command line.
Is there something built in? I have found an installable tool ADREPLSTATUS that may offer some of this information, but I am wondering if there is any built in UI for this.
I have searched the Active Directory Administrative Center and the Server Manager to no effect.
Answer: No. The old Replmon went out of style a long time ago.  See the AskDS post "Getting over Replmon":
http://blogs.technet.com/b/askds/archive/2009/07/01/getting-over-replmon.aspx
The idea is that repadmin, a command-line tool, can do everything and more that the GUI Replmon could do, only faster and more efficiently.
Then, if you still just really can't stand not having a GUI, then you can install AD Replication Status Tool, which you have already found.
http://www.microsoft.com/en-us/download/details.aspx?id=30005
Here's the AskDS post about that tool when it released just about a year ago:
http://blogs.technet.com/b/askds/archive/2012/08/23/ad-replication-status-tool-is-live.aspx

Ned here again. The AD Replication Monitor utility (Replmon.exe) was
  introduced with the Windows Server 2000 Support Tools many years ago
  as a GUI mechanism for performing certain DC admin tasks. With the
  release of Window Server 2008 Replmon was not included and we stopped
  making add-on Support Tools. Every few weeks someone asks me 'where do
  I download the Windows Server 2008 version of Replmon? Nowhere. It's
  done. Buried. Gone. Kaput. If you want it, you must run the old
  Windows Server 2003 version. Today I will talk about moving on with
  its supported replacement, Repadmin.exe.

Edit: Just to clarify, the standard suite of Remote Server Administration Tools such ADUC, Sites and Services, ADSI Edit, etc., are of course still included with Server 2012. You'll get them automatically on a DC, or on any member server if you install the RSAT optional feature. But for monitoring replication status, it's the repadmin command line tool, or the downloadable AD Replication Status tool.
Comment: I'm glad they made a new AD Repl Status UI tool!
Comment: I downloaded it and played with it a little, but honestly, I didn't find any use for it beyond what repadmin already does. But to each his own! :)
Comment: If the stock UI included EVEN an indication of the last and next sync time, and the last and next sync result, I'd be happy. As is, the UI ignores the existence of replication.
Comment: I'm guessing that's because for the vast majority of cases, AD replication is never an issue. However, you still have all the tools you need to troubleshoot it and monitor it. You have repadmin, and you have a free downloadable tool in AD Replication Status Monitor, and you have SCOM management packs if you really want to get in to it...


Is it possible to define a hook_preprocess function based on entity type such as profile2?

Question: I walked through the possible hooks by using function hook_preprocess(&$vars, $hook) and only entity was available to use. Is it possible to do something like hook_preprocess_profile2_entity(), or do I have to write an if condition to check the entity type in hook_preprocess_entity()?
Answer: This adapts the Zen theme pattern for node preprocess functions to entities:
<?php

/**
 * Implements template_preprocess_entity().
 *
 * Runs a entity specific preprocess function, if it exists.
 */
function MYTHEME_preprocess_entity(&$variables, $hook) {
  $function = __FUNCTION__ . '_' . $variables['entity_type'];
  if (function_exists($function)) {
    $function($variables, $hook);
  }
}

/**
 * Profile2 specific implementation of template_preprocess_entity().
 */
function MYTHEME_preprocess_entity_profile2(&$variables, $hook) {
}

/**
 * Field Collection specific implementation of template_preprocess_entity().
 */
function MYTHEME_preprocess_entity_field_collection_item(&$variables, $hook) {
}

Comment: This is awesome.  I always forget that PHP lets you do stuff like this.
Comment: If you want to do it in a parent theme and inherit/override, then you'll have to dig deeper into hook_theme. Zen's hook_theme is a good example (although lengthy).
Comment: I'd be grateful if someone showed me how I can achieve something similar outside of the theme layer! Driving me nuts...


Vector analysis: Calculate the flux of the vector field S

Question: $\mathbf{A}(x,y,z)= (\frac{-6x}{x^2+y^2},{-6y}{x^2+y^2},z+1)$
$S: x^2+4y^2=4, 0 \leq z \leq 1$
The task is to calculate the flux away from the z-axis.
Solution:
The divergence theorem obviously doesn't apply in this case so we are left with using the formula:
$I= \iint_S \mathbf{A}(x,y,z) \cdot \hat{n} dS $
but we can also write it as:
$I= \int_z \int_{\theta} \mathbf{A}(\theta , z) \cdot \mathbf{r}_{\theta} \times \mathbf{r}_z d \theta dz$
If we let
$\mathbf{r}(\theta, z)= (2cos(\theta),sin(\theta),z)$
we get
$\mathbf{r}_\theta \times \mathbf{r}_z = (cos(\theta),2sin(\theta),0)$
We rewrite $\mathbf{A}$ in terms of $\theta$ and $z$:
$\mathbf{A}(\theta,z)= (\frac{-12cos(\theta)}{1+3cos^2(\theta)},\frac{-6sin(\theta)}{1+3cos^2(\theta)},z+1) $
We can now begin calculating the integral $I$. I simplify the integrand and finally get:
$I= -12 \cdot \int_0^1 \int_0^{2\pi} \frac{1}{1+3cos^2(\theta)} d\theta dz $
How do I solve this integral? I have done Calculus 1-3 but I don't remember doing this integral. Keep in mind Wolfram Alpha says the integral is $-12\pi$ which is the correct solution to this problem.
Is their an easier way of solving this flux problem?
Comment: See https://math.stackexchange.com/questions/2470822/how-to-calculate-this-definite-integral-int-0-pi-fracxa2-cos2x-b2/2470845 for an alternative method to @froggie's clever observation (his posted solution). BTW, my original (unposted) approach would have been to use https://en.wikipedia.org/wiki/Tangent_half-angle_substitution (e.g., second example) to transform the trigonometric integral into an integral of a rational function, but that was less neat than either approach (I would have residue theorem at that point),
Comment: ... look at the comments in the posted solutions to the above post for missing details.
Answer: I think there are probably lots of ways to see that $$\int_0^{2\pi}\frac{1}{1 + 3\cos^2\theta}\,d\theta = \pi.$$ Below I'm going to compute it using a double integral, but if you know some complex analysis you could also use the residue theorem to compute the integral. And while I don't see one off the top of my head, I wouldn't be surprised if there's some useful substitution you could make to evaluate it.
Let $R$ be the ellipse $4x^2 + y^2\leq 4$ in the $xy$-plane. The area of this ellipse is $2\pi$, so $$2\pi = \iint_R 1\,dA.$$ Let's see what happens when we write this double integral out in polar coordinates. The equation describing the boundary of the ellipse  in polar coordinates is $$4 = 4x^2 + y^2 = 4r^2\cos^2\theta + r^2\sin^2\theta.$$ Solving for $r$ in this equation gives $$r = \frac{2}{\sqrt{4\cos^2\theta + \sin^2\theta}} = \frac{2}{\sqrt{1+3\cos^2\theta}}.$$ Therefore, in polar coordinates our integral becomes $$2\pi = \iint_R 1\,dA = \int_0^{2\pi}\int_0^{2/\sqrt{1+3\cos^2\theta}}r\,dr\,d\theta = \int_0^{2\pi}\frac{2}{1+3\cos^2\theta}\,d\theta.$$ Dividing both sides by 2 gives the desired identity $$\pi =\int_0^{2\pi}\frac{1}{1 + 3\cos^2\theta}\,d\theta.$$
Finally, I should say that you could use the divergence theorem if you like (though I'm not sure it would necessarily make the integrals easier). For instance, you might use the divergence theorem on the region of space defined by the inequalities $$x^2 + y^2\geq 1,\,\, x^2+4y^2\leq 4,\,\, 0\leq z\leq 1.$$ Might be worth trying.


how can I solve for $x$?

Question: I am having a bit of trouble solving for x when trying to find $f^{-1}$. I have $$y=\frac{x+5}{x-4}$$ How can I get x on one side? I tried multiplying both sides by the denominator but I am still left with an $x$ on both sides...
Comment: That was a good start! After that move the terms containing an $x$ to the other side. Remember to swap signs when appropriate.
Answer: Note that $x+5 = 1\cdot(x-4) + 9$. Just as with numbers, this means that $x-4$ divides $x+5$ once with a remainder of 9. So you can write $(x+5)/(x-4)$ as $1+\frac{9}{x-4}$. It should be easy to get $x$ on one side now.
Comment: For doing $x+5=1.(x-4)+9$, assuming $x\neq 4$ is essential. :-)
Comment: Just as with numbers, one needs to be careful when dividing by zero. Simply by paying careful attention to the statement of the problem we can see that $x=4$ could be problematic.
Comment: Usually when we are working with rational **functions**; we delete the roots of its denominator of our domain. So, what you did here is someway legal.
Answer: You just need to expand out the brackets and re-factor:
$$
\begin{array}{rcl}
y & = & \frac{x+5}{x-4}\\
y(x-4) & = & x+5\\
yx-4y&=&x+5\\
yx-x &=&4y+5\\
x(y-1)&=&4y+5\\
x&=&\frac{4y+5}{y-1}
\end{array}
$$
Answer: First multiply out to get rid of the fraction: $y(x-4)=x+5$, or $yx-4y=x+5$. Now collect the $x$ terms on one side and everything else on the other: $yx-x=5+4y$. Factor the lefthand side: $(y-1)x=5+4y$. Now just divide both sides by $y-1$, and you'll have $x$ in terms of $y$.
Answer: Multiply with the denominator:
$$y(x-4)=x+5$$
$$xy -4y=x+5$$
Take all x and bring them to the left side, all other terms to the right side.
$$xy -x = 5+4y$$
Single out x:
$$x(y-1)=5+4y$$
Divide by the factor:
$$x=\frac{5+4y}{y-1}$$
Note: $x\ne4$, because of the first equation.

how can I solve for $x$?


Break dentro de método que retorna boolean

Question: Estou tentando fazer um loop for, que em determinada ocasião retorna true, e sai do laço.
Exemplo:
public boolean autentica (Usuario usuario) {
    for(i=0; i<listaUsuarios.size ; i++){
        if(usuario.getSenha().equals(listaUsuarios.get(i).getSenha()){
            return true;
            break;
        }
    }
}

Ou seja, quando acontecer determinada coisa, retorne true e saia do laço, porque se não vai continuar testando a senha nos outros usuários.
Resumindo, quero retornar true e sair do laço. Como faço? Pois assim dá erro.
Comment: Mas assim que você retorna, ele sai da função e do laço.
Answer: Se você quer dar break e o return dentro do if, não faz sentido. Não dá para colocar nada depois de um return. Se o problema é apenas o erro que está ocorrendo, basta tirar o break porque o único problema é ter um comando depois da saída do método. Quando você sai do método, certamente você saiu do laço.
Mas não basta retirar o break. Você ainda terá um caminho de execução que não retornará nada. Você precisa retornar um booleano sempre. O código precisa garantir que em todas as situações ele retorne alguma coisa do mesmo tipo.
Este método está indicando se achou algum usuário que autenticou, acho que você quer que informe se não autenticou também se não achar nenhum usuário com senha compatível, então basta colocar um return false no final do método se passou por todo o laço sem cair no if. Assim:
class Program {
    static String[] listaUsuarios = new String[] { "1", "12", "123" };
    public static void main (String[] args) throws java.lang.Exception {
        System.out.println(autentica("123"));
        System.out.println(autentica("456"));
    }
    public static boolean autentica (String usuario) {
        for(int i = 0; i < listaUsuarios.length; i++) {
            if(usuario.equals(listaUsuarios[i])) {
                return true;
            }
        }
        //opcionalmente faz alguma coisa aqui.
        return false;
    }
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Haviam outros erros que impediram o funcionamento. Claro que tive que fazer algumas adaptações para simplificar. O seu ficaria assim (acho, obviamente não testei):
public static boolean autentica (Usuario usuario) {
    for(int i = 0; i < listaUsuarios.length; i++) {
        if(usuario.getSenha().equals(listaUsuarios.get(i).getSenha()) {
            //opcionalmente faz alguma coisa aqui.
            return true;
        }
    }
    //opcionalmente faz alguma coisa aqui.
    return false;
}

Comment: Obrigado, dessa forma que queria o algoritmo, valeu.
Comment: porque vc editou? dessa forma não sai do laço, a forma correta era a que voce postou antes
Comment: Você tem certeza? Vou fazer um teste real então. Se for o caso eu volto ao que estava. Aguarde.
Comment: voce nao entendeu o contexto, essa forma q vc postou era como eu tava fazendo antes, porem ficava errado porque se o 1 usuario da lista tiver a senha igual a digitada, vai retornar true, porem o laço continua, e logo em seguida vai retornar false, ou seja precisa do break, por isso perguntei como usar ele, e voce solucionou daquela forma.
Comment: Não você não entendeu. Se ele retorna `true`, o laço termina. Leia o primeiro parágrafo da resposta. Você fez o teste desta forma? Não tem como ele retornar `true` e depois retornar `false`. Ou ele faz um OU faz outro.
Comment: então nao preciso colocar else? else return false
Comment: entendi agora, se nao retornar true por padrao vai ser false neh?
Comment: Se você usar o `else`, não fará o que você quer, por que aí na primeira verificação de senha o método vai terminar dizendo que não autenticou mas sem verificar os outros usuários. Aconselho você fortemente executar todas as versões do código *debugando* passo-a-passo tanto em situação que autentica, como não autentica. E ver o que acontece para você entender a lógica que acho que é o que está lhe faltando. Teste inclusive a versão com o `else` que você falou. Você entenderá melhor o problema.
Comment: o problema era exatamente esse então, eu usava o else, e dessa forma funcionou, obrigado
Comment: Você estava fazendo certo, o problema real é que não retornava nada quando passava por todo o laço sem achar um usuário com senha compatível. Quando não tem nenhum usuário com senha compatível significa que não autenticou. Você tem que informar isto e não esta fazendo. A única situação que não autentica é se ele terminar o laço. Se ele achar uma senha compatível, ele nunca terminará o laço. Porque se tem um usuário com senha compatível, não tem porque olhar para os outros. Você encerra o laço e o método também.
Comment: eu entendi a logica, so nao tava sabendo escrever o codigo.
Comment: A lógica que eu coloquei inicialmente era muito complicada e não serve para esta situação, ela serviria em outro caso que eu imaginei que era o seu mas depois vi que não era.
Answer: O return em qualquer lugar que esteja finaliza o método (e por consequência o loop). O erro que deves estar encontrando é que o break é código morto, nunca alcançado, pois o método sempre retornará antes de atingi-lo. Retire o break que deverá funcionar.
Comment: Na verdade só retirar o `break`continuará dando erro. Veja minha resposta.
Comment: se retirar o break vai rodar, porem eu quero usar o break
Comment: @WarLock não é possível colocar código depois do return. Se em algum momento o método atingir o return, ele retorna e a execução acaba. Coloque o `return true;` dentro do `if` e o `return false;` no final do método.
Comment: Se tu entendes inglês te sugiro esta leitura: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html


API Android usada para programa que corta músicas

Question: Qual API Android usar para editar e cortar música? segue um link de um app android que faz isso:
https://play.google.com/store/apps/details?id=com.herman.ringtone&hl=pt_BR
Aguardo.
Comment: O que o link tem a ver com a pergunta?
Comment: Acredito que a segunda foto mostra o requisito dele. Ele quer uma library/API que faça crop de um determinado arquivo de música.
Comment: O link serve para ter uma ideia do que o app faz e é exatamente isso que quero saber.
Answer: Acredito que terá duas opções que são as mais viáveis, recomendo analisar ambas e verificar qual é a mais recomendada para o seu projeto:
musicg-api
ótima API porém possui uma limitação séria: só trabalha com arquivos WAV
https://sites.google.com/site/musicgapi/technical-documents/wav-file-format
ringdroid
Não é uma API e sim uma biblioteca que provavelmente terá o que precisa para o problema descrito.
https://code.google.com/p/ringdroid/


Why is the fivestar rating displaying/suggesting 4 out of 5 stars when no votes have been made?

Question: When adding a node which has a fivestar field and there are no votes yet, it shows 4 out of 5 stars as "on". Is this intended/ normal?
In my preprocess_page hook:
<?php
if (isset($node->field_rating)) {
                $field = field_view_field('node', $node, 'field_rating', array('label' => 'hidden'));
                $rating = render($field);
                $vars['title_rating'] = $rating;
            }

which gets rendered like this (with no votes made yet):

            Cancel rating

            Give it 1/5

            Give it 2/5

            Give it 3/5

            Give it 4/5

            Give it 5/5

Answer: Same problem here. I've made a workaround putting a class and set "display: none" in it.


Design a tournament that avoids byes

Question: I'm designing a football (soccer) tournament for youth sides, and there are nine teams playing. When this was done before, byes were given to 7 teams, in a single elimination tournament.  This is probably what we will do again, but it previously caused complaints from the two teams that didn't get a bye in the first round.
I am looking for a tournament design that keeps the good qualities of single elimination, but avoids byes, and is more manifestly "fair" to all teams.
The advantages of a single elimination tournament with 2^n teams are:

If a stronger team always beats a weaker one, then the strongest team is certain to win.
All teams have equal opportunity to win: if every match is a coin toss, then every team would have the same probability of winning.
There is an exciting final.
There are relatively few matches.
The number of rounds is known in advance

Points 1 and 2 make the tournament "fair", 3 makes it "exciting", 4 and 5 make it "convenient". If byes are used, then a single elimination tournament fails point 2.
Prior research:

A league structure in which every team plays every other team is fair, but there is no final and there are a large number of matches.
A Swiss-style competition still has trouble with an odd number of teams.
A groups + knockout is possible for 12 teams (four groups of 3, followed by knockout of the four winners), but not for an odd number.
I thought of a tournament in which the teams play in a circle: A v. B,  B v. C ... H v. I,  I v. A.  (so every team plays twice in the first round) Those teams that win both matches would then play in a second round and third round if required.  This avoids byes, but there might be no "final" and the number of rounds wouldn't be known in advance (it is also possible for no team to win both matches, but this would be unusual).

Is it possible to design a tournament that is fair, exciting and convenient for a number of teams that is not a power of two?
Comment: Round robin for all 9 in one group and the top 4 advance to playoffs (semi finals) would work for everyone equally, but has a lot of games.
Answer:
3 groups of 3 teams
Round robin play per group
Winners of the group advance to championship bracket
Runner-ups placed in wild card group
Losers in consolation group

Round robin play for both wild card and consolation groups
Winner of runner-up group moves on to championship bracket as fourth team
After that you could play around with it have a losers bracket etc
Just have tiebreakers ready since group play isn't always clear cut
Comment: This is the format used by some youth rugby cup competitions in the UK where it is called a Waterfall Cup.
Answer: Pools leading into knockout are perfectly possible with an odd number of teams; you simply have one more team in one of the pools. This provides a direct competition loser, with the remaining places deciding appropriate final positions for all other teams (final is A1 v B1, third/fourth is A2 v B2, etc.).
If so desired, the top four teams can be crossed over in semifinals, or the top eight in quarterfinals. This has a known number of matches, requires only half the number of byes and half the number of rounds of a complete round robin, and provides for finals, but assigns a bye to every team in one pool, plus one additional bye.

Specifically for nine teams, divide them by either random sorting into, or by seeding in some manner that gives a balanced set of, three primary pools of three.
Each primary pool plays a complete round robin, giving three winners, three runners-up and three losers who form the corresponding secondary pools. Each secondary pool plays an additional complete round robin. This gives a ranking for each team, who are paired 1 v 2, 3 v 4, etc. in the finals. All teams have played seven matches except for the last-place team who receive the only bye in the finals round.

More generally, designing tournaments to satisfy these specific conditions requires significant thought in advance and, when dealing with larger numbers of teams, can involve decision-making that affects the structure in fundamental ways. This is the subject of lengthy documents used by a range of sporting organisations and would be inappropriate to replicate here.

Design a tournament that avoids byes


Error in Oauth2 process: No 'Access-Control-Allow-Origin' header is present on the requested resource

Question: I've been having a bit of trouble with receiving a code from the Sonos API for my application to then get a token for later API requests, specifically the error above. Is there any sort of test code or option to temporarily skip Oauth2 to test the API requests I already have written? Thanks!
Comment: Can you give an example of the GET requests you're making, and detail a little the environment in which you're making the call?
Answer: If there was that would be a bit of a gigantic security hole. Just fix your auth code.


Is it correct to run a mixed model (2-ways anova) on a unbalanced and incomplete design?

Question: I am studying the effect of land use on biomass. I am struggling to find the right model to correctly analyse my data.
Data description
I have surveyed land use changes in 20 fields for 3 years. I obtained various land uses which include, for instance, maize, rice, no crop (= fallow), young trees, mature trees, etc. Fields with no tree or young trees can be combined with any crop. In order to limit the number of factors, I have decided to focus on 2 qualitative variables: Tree "presence of trees" (no trees, young trees, mature trees) and Annual "type of crop" (no crop, rice, maize). As the data was obtained from observations the group sizes are very variable:
<code>       Tree     No tree        Young trees        Mature trees
   Annual
 No crop          4                 9                   15
 Rice             11                2                   0
 Maize            13                5                   0
</code>
For instance, mature trees are always associated with "no crop", because it is biologically impossible to grow rice or maize under the canopy. Fields which belong to the "mature trees" group also remain in this group in all 3 years, while the land use can change in others.
Questions
I want to determine the effect of land use (crop AND trees) on biomass. I am using mixed models to account for the repeated measurements :
<code>> mm = lmer(Biomass ~ Tree + Annual + (1|Field))
</code>

can I use pairwise comparisons tests (e.g. cld and emmeans)? It returns all combinations, so I don't know whether the estimates are right (and more precisely if I can test for significance).
<code>> emmeans(mm, ~ Tree | Annual)

Tree = no tree     emmean         SE                lower.CL       upper.CL
No crop         1.225439      0.02068960          1.183148      1.267730
Rice            1.239332      0.02022856          1.197828      1.280836
Maize           1.234615      0.01994940          1.193585      1.275645

Tree = Young trees emmean         SE                lower.CL       upper.CL
No crop         1.211010      0.02123882          1.167735      1.254284
Rice            1.224903      0.02198322          1.180324      1.269482
Maize           1.220186      0.02220742          1.175199      1.265173

Tree = Mature trees  emmean         SE              lower.CL       upper.CL
No crop          1.270676      0.02369005         1.221675      1.319676
Rice             1.284569      0.02459490         1.234082      1.335056
Maize            1.279852      0.02453023         1.229470      1.330233

Degrees-of-freedom method: kenward-roger  Confidence level used: 0.95
</code>
Is it correct to use mixed models when some individuals (fields here) are associated only to 1 combination (mature trees + no crop) ?
Comment: Looks ok to me, but note your model is additive so the comparisons of `X1` will be identical at each `X2`. BTW, may I suggest using meaningful variable names rather than X1 and X2? You're doing science, not math — right?
Comment: @rvl I am not sure I understand why the comparisons of X1 (Trees) will be identical on each X2 (Annuals). I do obtain different marginal effects (for instance using ggpredict). Also, I don't usually use X1/X2, I just hoped to make the question easier to read. I edited this.
Comment: You fitted a model without interaction so the effect of Trees is modelled as the same  for each level of Annuals and vice versa. Note that if you do add an interaction R will drop degree(s) of freedom because nothing grows under mature trees.
Comment: `emmeans()`'s results are based on the *model*, and the model you show is additive, i.e., it presumes that the effects of `X1` are independent of those of `X2`. Try `pairs(emmeans(mm, ~ Tree | Annual))` and see for yourself.
Comment: Regarding Q2, again, that's OK based on the *model*, which also presumes the variation among fields is homogeneous.  If you have no replications in one condition, then that condition simply doesn't contribute to the estimate of the common variance of fields.
Answer: There is a potential collinearity problem, because the levels of Tree are partially predicted by knowing Annual (i.e. if there are No trees then there is No crop). This means that you can't really trust the estimate of Mature Trees or No Crop.
Try testing for collinearity (look here for advice on collinearity with categorical variables).
Comment: Thanks for the reference, that's a very interesting discussion. But I was wondering, isn't VIF sufficient to test for collinearity in such models?
Comment: Did you test with collinearity with VIF? I think it shouldn't be a problem using VIF with categorical variables.
Comment: @Glen_b, I apologize for deleting it prematurely. Indeed, I did not understand what you suggested. Now I realize that you meant vector linear regression (is that the term?), and I found this reference that taught me that it is just like "regular" regression: https://stackoverflow.com/questions/47618414/linear-regression-in-python-with-vectors
I would gladly re-open the question to discuss the details more. Thank you so much for looking me up to answer my question!
Comment: If you wish, sure; I could turn it into a brief answer, but it's not essential if you're already set


Is there an R package with a pretty function that can deal effectively with outliers?

Question: One of the data sets I deal with is quite strange. The datawarehouse I downloaded the data from has a lot 999999999 values in one of the variables. Apparently the computer system on which the datawarehouse sits on does not support storing of null values. So they use 999999999 as the "null" value. Now if I just run pretty in R on the variable, it gives non-sensical ranges.

Is there a package with a version of pretty that can deal with outliers by putting them in the range of say (100,High) ?
Comment: or mark them as `NA`: `foo[foo == 999999999] <- NA`
Comment: Why not just remove the 999999999 values?
Comment: I can't remove them as there are 1000 variables in the dataset and it's difficult to know which variables has which default value. Also if confronted by a new dataset the same has to be done. Sometimes the default value is 99999990 for example. So it's better to have a general algorithm that can deal with that
Answer: If you're importing your data with a command like, say,
read.table('yourfile.txt', header=TRUE, ...)

you can indicate what values are to be considered as "null" or NA values, by specifying na.strings = "999999999". We can also consider different values for indicating NA values. Consider the following file (fake.txt) where we want to treat "." and "999999999" as NA values:
1 2 .
3 999999999 4
5 6 7

then in R we would do:
> a <- read.table("fake.txt", na.strings=c(".","999999999"))
> a
  V1 V2 V3
1  1  2 NA
2  3 NA  4
3  5  6  7

Otherwise, you can always filter your data as indicated by @Sacha in his comment. Here, it could be something like
a[a=="." | a==999999999] <- NA

Edit
In case there are multiple abnormal values that can possibly be observed in different columns with different values, but you know the likely range of admissible values, you can apply a function to each column. For example, define the following filter:
my.filter <- function(x, threshold=100) ifelse(x > threshold, NA, x)

then
a.filt <- apply(a, 2, my.filter)

will replace every value > 100 with NA in the matrix a.
Example:
> a <- replicate(10, rnorm(10))
> a[1,3] <- 99999999
> a[5,6] <- 99999999
> a[8,10] <- 99999990
> summary(a[,3])
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
 -1e+00   0e+00   0e+00   1e+07   1e+00   1e+08
> af <- apply(a, 2, my.filter)
> summary(af[,3])
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
-1.4640 -0.2680  0.4671 -0.0418  0.4981  0.7444  1.0000

It can be vector-based of course:
> summary(my.filter(a[,3], 500))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
-1.4640 -0.2680  0.4671 -0.0418  0.4981  0.7444  1.0000

Comment: You don't always know in advance what the special values are, especially if you are seeing the dataset for the first time! I was hoping there would be a cut2 or something function that deals with that already.
Comment: @xiaodai Yes there is a `cut()` function in R but it does probably not do what you want. I'll update my answer.
Answer: I encounter this quite frequently when dealing with customer daily time series data. It appears that many accounting systems IGNORE daily data that didn't occur i.e. no transactions were recorded for that day (time interval/bucket) and don't fill in a '0" number . Since time series analysis require a reading for every interval/bucket we need to inject a "0" for the omitted observation. Intervention Detection is essentially a scheme to detect the anomaly and replace it with an expected value based on an identified profile/signal/prediction. If there are many of these 'missing values" the system can break down The problem becomes a little more complex when there is strong day-of-the-week profile in the historical data and a "sequential patch of values" are not recorded, suggesting that replacement values be obtained by computing local daily averages as a precursor to fine-tuning these values.


Trouble proving divergence theorem

Question: Suppose we have a cone $z=\sqrt {x^2 +y^2},0\le z \le 1 $ and $\vec F(x,y,z) = <x,y,z>$
Find the flux across the surface.
I will start of with the right hand side of divergence theorem
$div \vec F = 1 + 1 + 1 = 3$
$\int \int \int 3dV$ = $ 3*(volume$ $cone)=\pi$
For left hand side, we have two surface that is the top flat surface, and the curved surface.
$\int \int_{curved} \vec F \cdot \vec n dS = \int \int <x,y,z> \cdot <\frac{x}{\sqrt{x^2+y^2}} , \frac{y}{\sqrt{x^2+y^2}} ,-1> = \sqrt {x^2+y^2} - z = 0$
$\int \int_{flat} \vec F \cdot \vec n = <x,y,z> \cdot <0,0,1> = \int \int z dA$
$\int_{0}^{2\pi} \int_{0}^{1} r^2 dr d\theta = \frac {2\pi}{3}$
What am I doing wrong?
edit* = found the mistake. forgot the substitute z=1 on the field for the last equation
Answer: The error is in the flat calculation:
$$\int_{0}^{2\pi} \int_{0}^{1} r^2 dr d\theta = \frac {2\pi}{3}$$
In fact
$$\int \int_{flat} \vec F \cdot \vec n = <x,y,z> \cdot <0,0,1> = \int \int z dA$$
But $z$, on the flat, is $=1$. Then we have
$$\int \int 1 dA=\pi$$

Trouble proving divergence theorem


If $p$ is a prime number and $p\mid a^p-b^p$. Then $p^2\mid a^p -b^p$.

Question: If $p$ is a prime number and $p\mid a^p-b^p$. Then $p^2\mid a^p -b^p$.
Comment: Fermat's little theorem may help here.
Comment: Searching the site also may help.
Comment: What's the question?
Answer: The case $a=b$ is going to be true anyways.
WLOG let's assume $a=b+x,x>0$
$$(b+x)^p-b^p=x^p+\sum_{r=1}^{p-1}\binom{p}{r}x^rb^{p-r}$$
Since it is divisible by p, $p|x$.
As $p>1,p^2|x^p$.Also $\binom{p}{r}x^rb^{p-r}$ is divisible by $p^r*p=p^{r+1}$
Since $r>0,r+1\geq 2$
Therefore, $$p^2|x^p+\sum_{r=1}^{p-1}\binom{p}{r}x^rb^{p-r}$$
Answer: You have $$a^p\equiv a\pmod p\\b^p\equiv b\pmod p .$$
$($*follows from Fermat's ittle theorem*$)$
So, $$a^p-b^p\equiv a-b \pmod p.$$
Now, you have $$a^p-b^p\equiv 0 \pmod p.$$ So, $$a-b\equiv 0 \pmod p\\\implies a\equiv b \pmod p\\\implies p\mid a-b\tag 1\\$$
Now, note that $$a^p-b^p=(a-b)(a^{p-1}+a^{p-2}b+a^{p-3}b^2+\dots+b^{p-1})$$
Now, $$\begin{align}
a^{p-1}&\equiv a^{p-1}\pmod p\\
a^{p-2}b&\equiv a^{p-2}a=a^{p-1}\pmod p[\text{since} \;a\equiv b\pmod p]\\
\vdots\\
b^{p-1}&\equiv a^{p-1} \pmod p
\end{align}$$
So, summing them up, $$(a^{p-1}+a^{p-2}b+a^{p-3}b^2+\dots+b^{p-1})\equiv pa^{p-1}\equiv 0\pmod p$$
So, $$p\mid a-b\\p\mid (a^{p-1}+a^{p-2}b+a^{p-3}b^2+\dots+b^{p-1})$$
So, $$ \bbox[yellow]{p^2\mid a^p-b^p}$$


GCD and roots of unity

Question: Say we have roots of unity $\zeta$ and $\rho$ where $o(\zeta)=a$ and $o(\rho)=b$. If $gcd(a,b)=1$, how can we prove that $o(\zeta\rho)=ab$?
Answer: Suppose $0\lt s\lt ab$. We'll prove $(\zeta\rho)^s\ne1$.
Since $\gcd(a,b)=1$, we know the least common multiple of $a$ and $b$ is $ab$, so $s$ is not a common multiple of $a$ and $b$. Assume, without loss of generality, that it's not a multiple of $a$. Then $bs$ is also not a multiple of $a$, and $(\zeta\rho)^{bs}=\zeta^{bs}\rho^{bs}=\zeta^{bs}\ne1$. So, $(\zeta\rho)^s\ne1$.

GCD and roots of unity


Why isn't necessary the contextAPI in that case?

Question: I thought that if I want to pass a state in another component, it is possible only through props or with contextAPI (provider etc).
For example:
import { createContext, useState, useContext } from 'react';

const FavContext = createContext();
//now we have a context object

function FavProvider(props) {
  //create the provider and its functionality
  const shape = {
    type: '', //films, planets, people
    id: 0, //id of film, or planet, or person
    data: {}, //the actual data object
  };
  const [fav, setFav] = useState(shape);

  function updateFav(type, id, data) {
    setFav({
      type,
      id,
      data,
    });
  }
  return <FavContext.Provider value={[fav, updateFav]} {...props} />;
}

function useFav() {
  //for pages that want to access the context object's value
  //custom hook use...
  const context = useContext(FavContext);
  if (!context) throw new Error('Not inside the Provider');
  return context; // [fav, updateFav]
}
export { useFav, FavProvider };

I wrap all the other components with the FavProvider, then I use the useFav wherever I want.
Here is a code without the contextAPI, and it works. Why is that? Why isn't necessary the contextAPI in that case?
import { useEffect, useState } from 'react';
import { getAuth, onAuthStateChanged } from 'firebase/auth';

export default function useAuthStatus() {
  const [loggedIn, setLoggedIn] = useState(false);
  const [checkingStatus, setCheckingStatus] = useState(true);
  useEffect(() => {
    const auth = getAuth();
    onAuthStateChanged(auth, (user) => {
      if (user) {
        setLoggedIn(true);
      }
      setCheckingStatus(false);
    });
  });
  return { loggedIn, checkingStatus };
}

and when I want to use the loggedIn and chekingStatus, I just bring it in like this:
import { Navigate, Outlet } from 'react-router-dom';
import useAuthStatus from '../hooks/useAuthStatus';
import Spinner from './Spinner';

export default function PrivateRoutes() {
  const { loggedIn, checkingStatus } = useAuthStatus();
  if (checkingStatus) {
    return <Spinner />;
  }

  return loggedIn ? <Outlet /> : <Navigate to='/sign-in' />;
}

Comment: You have other way than context api to pass props to components child. Context is one oh them, and I think react-router-dom might actually use it in its internals.
You could also achieve that by using high Order Component that would inject dependencies to all of its children.

But it woudl rise question about performance, et rerendering of components. (by the way, using context api does not fix rendering issue as Redux would ...)
Answer: Why did you use an array to pass the state through the value in the context API?


Univariate analysis vs univariate regression

Question: I am doing an analysis of various factors that determine whether a patient is likely to have an illness. I will do this using multiple linear regression.
I have noted that some papers perform a univariate analysis first (whether this is correct or not is not the purpose of this question) to determine/ reject factors that may be used in the multiple regression.
What I don't understand is whether the univariate analysis should be a univariate regression analysis or whether it should be the appropriate difference in populations (eg Chi-squared/ t-test etc depending on variable type).
Comment: What is your outcome variable? Is it something like Y = 1 if patient has the illness and 0 otherwise? If yes, you would need to perform a logistic regression. Usually people perform the univariate analyses using the tests you mention. But it's fine to use univariate regressions instead.
Comment: The outcome is binary. So I guess I would use regression. So in other words binary outcome vs continuous input means regression. Ok got it....
Comment: Regression is a general term so you need to be specific when specifying the nature of the regression model for cross-sectional data.  If you have a continuous outcome (e.g., blood pressure), then you would use a 'linear regression model'. If you have a count model (e.g., number of days free of pain), you would a 'count regression model'. If you have a binary outcome, then you would use a 'binary logistic regression model'.
Comment: If you have a categorical outcome whose categories don't follow a natural order (e.g., Treatment preference, with categories Treatment A, Treatment B, No Preference), you would use a 'multinomial regression model'. If you have a categorical outcome with ordered categories (e.g., level of pain, with categories 1 = low through 3 = high), you would use an 'ordered logistic regression'. Again, this terminology assumes that you collected data on your subjects at a single time point.
Answer: Assuming that Y is continuous (rather than binary) and the first predictor X1 is binary, you could perform either of the following:
1. A t-test which allows you to compare the mean value of Y when X1 = 1 against the mean value of Y when X1 = 0;

2. A simple linear regression model regressing Y on X1.

I am guessing most people use a t-test because it has the flexibility of allowing the variability of the Y values when X1 = 0 to be different from the variability of the Y values when X1 = 1. But if that needs to be allowed for, it can easily be accommodated in the context of a simple linear regression model too by allowing the error variance to depend on the values of X1.
Assuming that Y is continuous and the second predictor X2 is categorical with k categories, where k > 2, you could perform either of the following:
i. A one-way analysis of variance, which allows you to compare the mean value of Y when X1 = 1 across the categories of X2;

ii. A simple linear regression model regressing Y on X2. (When fitted to the data, this model will actually include k - 1 dummy variables as predictors, all of which help encode the effect of X2 on Y.)

By default, the one-way anova in i. and the simple regression model in ii. assume that the variability of the Y values is roughly the same across the categories of the X2 variable. If that is not the case, each of these methods can be modified accordingly.


iptables "host/network not found"

Question: If I add this line to my iptables:
-A INPUT -s /32 -i tcp -p tcp -m tcp --dport 22 -j DROP
I get the error:
iptables-restore v1.4.14: host/network' not found`
When running:
sudo iptables-restore /etc/network/iptables
Is there a problem with that line? If not, I will post the rest of the iptable configuration.
complete configuration:
*filter
:INPUT DROP [23:2584]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1161:105847]
-A INPUT -i lo -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 443 -j ACCEPT
# -A INPUT -s /32 -i tcp -p tcp -m tcp --dport 22 -j DROP
-A INPUT -s 192.168.0.10/24 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT

copied from here
Comment: I'm voting to close this question as off-topic because it is too narrowly focused to the OP's problem, and answers are unlikely to benefit future visitors.
Answer: There's also a problem with -i tcp.  That refers to traffic coming in the interface tcp; it's not syntactically invalid, but it won't match anything at all, because there isn't any such interface.
Comment: That was a very nice catch.
Comment: So it seems my entire configuration is quite flakey in that case. I've added it to my question. Would you mind reviewing it? If you visit the link where I copied it from, the blogger tells us what each line is intended to do.
Comment: We're [not really in the business of hand-holding through the acquisition of elementary skills at SF](http://serverfault.com/help/on-topic), so I'm sorry, but I'm afraid I do mind.  You should go away, buy a book on firewalling, read it, try it, then come back and ask questions that arise.  You've had your specific question answered, so I further suggest you accept JennyD or Gene's answer.
Comment: @MadHatter: I can't really verify if your answer is correct unless you can confirm that the full configuration is in fact wrong though
Comment: My answer doesn't answer your question, so you shouldn't accept it; it was merely an additional note on something else that was wrong with your rule.  As for verifying it, **try `man iptables` and look for the `-i` flag**.  Both Jenny D and Gene have given complete and definitive answers to the question you originally asked, and one of them deserves to have their answer accepted.  If you'd wanted your entire config sanity-checked, you should have asked that - so that we could vote to close the question.
Answer: It contains a netmask without a host. -s /32 should probably be -s 127.0.0.1/32 or something.
Answer: There is a problem with that line, specifically the -s /32 portion.  You have to define a host.  For example:
-A INPUT -s 184.108.40.206/32 -i tcp -p tcp -m tcp --dport 22 -j DROP

Comment: Apparently that line is supposed to lock others out from using ssh. Is that `ip/32` a single exception that will be allowed to ssh in?
Comment: If you have a DROP statement at the end of the rules for that table then you only need to permit specific hosts.  Update your question with your full iptables configuration (censor IPs as needed) and mention your need to filter SSH connections in the question.

iptables “host/network not found”