Identifying an outlier nominal group different than the norm, based on an ordinal dependent variable

Question: As part of my graduate thesis, I've measured the satisfaction levels of a couple hundred respondents across 7 districts of a city about a local public service, using a 4-point ordinal Likert scale (ranging from 1: Very dissatisfied to 4: Very satisfied). What I'm trying is to see if there are any districts that significantly differ from the norm of all districts as a whole, not just to each other, in terms of satisfaction. I'm familiar with difference tests such as Kruskal Wallis on the SPSS program that I'm using right now, but these tests often are built for pairwise comparisons, and not really for 4+ groups of the independent variable such as my case. Therefore I'd like to ask if there any appropriate methodology that I can use to do this effectively. Thanks everyone and appreciate all your insights. Cheers!
Answer: Do I understand correctly, you want to investigate whether the medians of your districts differ from the overall median of all districts? This may not be the best way, but since nobody else has commented: I guess you could use 7 one-sample median tests and use the overall median as the hypothesized median. However, if you did this you'd need to manually adjust your critical p-value as lower because you'd be running 7 tests "as if" they were independent.
As for differences between districts, Kruskal-Wallis is the non-parameteric equivalent of ANOVA, so you can have more than 2 groups in K-W, and it does give you an omnibus test for any differences between 7 groups, as well as the pairwise comparisons.


Java Properties: How to keep non key=value lines?

Question: I am trying to modify a config file in Java using Properties.
I read, write and modify the lines successfully using Properties.store, load and setProperty, but I noticed that after doing such operation the file is overwritten and thus I loose al the lines in the config file that are not key-value pairs.  Namely, I loose the comments.
Is there a way to keep such lines using java.util?
Placing a prefix in each line is not a problem.  I know how to do it 'manually' reading line by line; I'am asking instead for an alternative
Comment: Related : http://stackoverflow.com/questions/565932/a-better-class-to-update-property-files  (not duplicate )
Answer: I don't think it is possible. Note that properties also don't promise that the ordering will be the same from load() to store(), or from one store() to another. If it is possible, the javadoc for Properties will tell you how.
Comment: true but maybe there's some other class we dont know about
Comment: @quinestor sure, you could write it yourself. It's pretty straightforward. If you're looking for some non-API library that does it, that's probably better handled with a google search.
Comment: It's true. I modified the log levels on one instance of Glassfish using the console, and the comments and order are lost.
Answer: <code>import java.io.*;
import java.util.*;

/**
 * The CommentedProperties class is an extension of java.util.Properties
 * to allow retention of comment lines and blank (whitespace only) lines
 * in the properties file.
 *
 * Written for Java version 1.4
 */
public class CommentedProperties extends java.util.Properties {

/**
 * Use a Vector to keep a copy of lines that are a comment or 'blank'
 */
public Vector lineData = new Vector(0, 1);

/**
 * Use a Vector to keep a copy of lines containing a key, i.e. they are a property.
 */
public Vector keyData = new Vector(0, 1);

/**
 * Load properties from the specified InputStream.
 * Overload the load method in Properties so we can keep comment and blank lines.
 * @param   inStream   The InputStream to read.
 */
public void load(InputStream inStream) throws IOException
{
    // The spec says that the file must be encoded using ISO-8859-1.
    BufferedReader reader =
    new BufferedReader(new InputStreamReader(inStream, "ISO-8859-1"));
    String line;

    while ((line = reader.readLine()) != null) {
        char c = 0;
        int pos = 0;
        // Leading whitespaces must be deleted first.
        while ( pos < line.length()
                && Character.isWhitespace(c = line.charAt(pos))) {
            pos++;
        }

        // If empty line or begins with a comment character, save this line
        // in lineData and save a "" in keyData.
        if (    (line.length() - pos) == 0
                || line.charAt(pos) == '#' || line.charAt(pos) == '!') {
            lineData.add(line);
            keyData.add("");
            continue;
        }

        // The characters up to the next Whitespace, ':', or '='
        // describe the key.  But look for escape sequences.
        // Try to short-circuit when there is no escape char.
        int start = pos;
        boolean needsEscape = line.indexOf('\\', pos) != -1;
        StringBuffer key = needsEscape ? new StringBuffer() : null;

        while ( pos < line.length()
                && ! Character.isWhitespace(c = line.charAt(pos++))
                && c != '=' && c != ':') {
            if (needsEscape && c == '\\') {
                if (pos == line.length()) {
                    // The line continues on the next line.  If there
                    // is no next line, just treat it as a key with an
                    // empty value.
                    line = reader.readLine();
                    if (line == null)
                        line = "";
                    pos = 0;
                    while ( pos < line.length()
                            && Character.isWhitespace(c = line.charAt(pos)))
                        pos++;
                } else {
                    c = line.charAt(pos++);
                    switch (c) {
                        case 'n':
                            key.append('\n');
                            break;
                        case 't':
                            key.append('\t');
                            break;
                        case 'r':
                            key.append('\r');
                            break;
                        case 'u':
                            if (pos + 4 <= line.length()) {
                                char uni = (char) Integer.parseInt
                                           (line.substring(pos, pos + 4), 16);
                                key.append(uni);
                                pos += 4;
                            }   // else throw exception?
                            break;
                        default:
                            key.append(c);
                            break;
                    }
                }
            } else if (needsEscape)
                key.append(c);
        }

        boolean isDelim = (c == ':' || c == '=');

        String keyString;
        if (needsEscape)
            keyString = key.toString();
        else if (isDelim || Character.isWhitespace(c))
            keyString = line.substring(start, pos - 1);
        else
            keyString = line.substring(start, pos);

        while ( pos < line.length()
                && Character.isWhitespace(c = line.charAt(pos)))
            pos++;

        if (! isDelim && (c == ':' || c == '=')) {
            pos++;
            while ( pos < line.length()
                    && Character.isWhitespace(c = line.charAt(pos)))
                pos++;
        }

        // Short-circuit if no escape chars found.
        if (!needsEscape) {
            put(keyString, line.substring(pos));
            // Save a "" in lineData and save this
            // keyString in keyData.
            lineData.add("");
            keyData.add(keyString);
            continue;
        }

        // Escape char found so iterate through the rest of the line.
        StringBuffer element = new StringBuffer(line.length() - pos);
        while (pos < line.length()) {
            c = line.charAt(pos++);
            if (c == '\\') {
                if (pos == line.length()) {
                    // The line continues on the next line.
                    line = reader.readLine();

                    // We might have seen a backslash at the end of
                    // the file.  The JDK ignores the backslash in
                    // this case, so we follow for compatibility.
                    if (line == null)
                        break;

                    pos = 0;
                    while ( pos < line.length()
                            && Character.isWhitespace(c = line.charAt(pos)))
                        pos++;
                    element.ensureCapacity(line.length() - pos +
                                           element.length());
                } else {
                    c = line.charAt(pos++);
                    switch (c) {
                        case 'n':
                            element.append('\n');
                            break;
                        case 't':
                            element.append('\t');
                            break;
                        case 'r':
                            element.append('\r');
                            break;
                        case 'u':
                            if (pos + 4 <= line.length()) {
                                char uni = (char) Integer.parseInt
                                           (line.substring(pos, pos + 4), 16);
                                element.append(uni);
                                pos += 4;
                            }   // else throw exception?
                            break;
                        default:
                            element.append(c);
                            break;
                    }
                }
            } else
                element.append(c);
        }
        put(keyString, element.toString());
        // Save a "" in lineData and save this
        // keyString in keyData.
        lineData.add("");
        keyData.add(keyString);
    }
}

/**
 * Write the properties to the specified OutputStream.
 *
 * Overloads the store method in Properties so we can put back comment
 * and blank lines.
 *
 * @param out   The OutputStream to write to.
 * @param header Ignored, here for compatability w/ Properties.
 *
 * @exception IOException
 */
public void store(OutputStream out, String header) throws IOException
{
    // The spec says that the file must be encoded using ISO-8859-1.
    PrintWriter writer
    = new PrintWriter(new OutputStreamWriter(out, "ISO-8859-1"));

    // We ignore the header, because if we prepend a commented header
    // then read it back in it is now a comment, which will be saved
    // and then when we write again we would prepend Another header...

    String line;
    String key;
    StringBuffer s = new StringBuffer ();

    for (int i=0; i<lineData.size(); i++) {
        line = (String) lineData.get(i);
        key = (String) keyData.get(i);
        if (key.length() > 0) {  // This is a 'property' line, so rebuild it
            formatForOutput (key, s, true);
            s.append ('=');
            formatForOutput ((String) get(key), s, false);
            writer.println (s);
        } else {  // was a blank or comment line, so just restore it
            writer.println (line);
        }
    }
    writer.flush ();
}

/**
 * Need this method from Properties because original code has StringBuilder,
 * which is an element of Java 1.5, used StringBuffer instead (because
 * this code was written for Java 1.4)
 *
 * @param str   - the string to format
 * @param buffer - buffer to hold the string
 * @param key   - true if str the key is formatted, false if the value is formatted
 */
private void formatForOutput(String str, StringBuffer buffer, boolean key)
{
    if (key) {
        buffer.setLength(0);
        buffer.ensureCapacity(str.length());
    } else
        buffer.ensureCapacity(buffer.length() + str.length());
    boolean head = true;
    int size = str.length();
    for (int i = 0; i < size; i++) {
        char c = str.charAt(i);
        switch (c) {
            case '\n':
                buffer.append("\\n");
                break;
            case '\r':
                buffer.append("\\r");
                break;
            case '\t':
                buffer.append("\\t");
                break;
            case ' ':
                buffer.append(head ? "\\ " : " ");
                break;
            case '\\':
            case '!':
            case '#':
            case '=':
            case ':':
                buffer.append('\\').append(c);
                break;
            default:
                if (c < ' ' || c > '~') {
                    String hex = Integer.toHexString(c);
                    buffer.append("\\u0000".substring(0, 6 - hex.length()));
                    buffer.append(hex);
                } else
                    buffer.append(c);
        }
        if (c != ' ')
            head = key;
    }
}

/**
 * Add a Property to the end of the CommentedProperties.
 *
 * @param   keyString    The Property key.
 * @param   value        The value of this Property.
 */
public void add(String keyString, String value)
{
    put(keyString, value);
    lineData.add("");
    keyData.add(keyString);
}

/**
 * Add a comment or blank line or comment to the end of the CommentedProperties.
 *
 * @param   line The string to add to the end, make sure this is a comment
 *             or a 'whitespace' line.
 */
public void addLine(String line)
{
    lineData.add(line);
    keyData.add("");
}
</code>
}
Comment: http://www.coderanch.com/t/544562/java/java/save-properties-file-affecting-existing
Comment: http://www.dreamincode.net/forums/topic/53734-java-code-to-modify-properties-file-and-preserve-comments/


Sample from the posterior of normal model using rstan?

Question: I have a very simple Bayesian model $y_j \mid \mu ,\sigma^2 \sim N(\mu,\sigma^2)$, $\mu \sim N(0,100)$, $\sigma \sim InvGamma(0.01,0.01)$. I am try to sample from the posterior using rstan. However, I get the following error
RStan code (normal.stan)
data {
  int n;
  vector[n] y;
}

parameters{
  real mu;
  real<lower = 0> sigma;
}
model{
  target += normal_lpdf(y | mu, sigma);
  target += normal_lpdf(mu | 0, 100)
  target += inv_gamma_lpdf(sigma | 0.01, 0.01);
}

R Code
require(rstanarm)
require(rstan)
# Simulation
n <- 500;
set.seed(111)
y <- rnorm(n,0,1)

posterior <- stan(file   = "normal.stan",
             data   = list(y, n),
             warmup = 1000,
             iter   = 11000,
             chains = 1)

However, when I run the R code, the posterior object is empty.
I would appreciate any hints.
Comment: You'll probably get more help if you post in the stan forums: https://discourse.mc-stan.org/latest.
Answer: The list of data that you pass to the stan function must be a named list, like
data = list(y = y, n = n),


I keep getting an exception even though the code is within a try/catch block

Question: I wrote some code that encounters an exception if the given user input is invalid so I put it in a try/catch block yet it still threw an exception. The code itself is quite long so here's a simplified version of the code that also encounters the exception. The exception itself is clear, the position "3" doesn't exist so naturally it would throw an exception, but it's inside of a try/catch block so it should get caught, but it doesn't.
int main() {
    try
    {
        vector<string> test = vector<string>{ "a","b","c" };
        string value = test[3];
    }
    catch (...)
    {

    }
}

Running this code just results in the following exception regardless of whether or not it's in a try/catch block.

I also tried specifying the exception (const out_of_range&e) but that didn't help either. It just caused the exact same exception.
int main() {
    try
    {
        vector<string> test = vector<string>{ "a","b","c" };
        string value = test[3];
    }
    catch (const out_of_range&e)
    {

    }
}

I'm using Visual Studio, could this be an issue with the IDE or the compiler it uses?
Comment: There are different types of *exceptions* om computer systems. You can only catch those that are thrown from C++ itself, not those that are caused by software, operating system or hardware errors. A crash like that you need to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/).
Comment: No, using `operator[]` will *not* throw an exception using standard C++. The Visual C++ runtime library might do that in a debug build, but that's a non-standard extension. And it might also throw up a dialog when the exception is thrown. If you let the program continue the `catch` might catch it. Visual C++ is kind of weird sometimes.
Comment: As for the reason behind your *crash* you should go back to your [beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and review them.
Comment: I know the reason behind the crash, this is example code to cause the out of range exception to showcase how the exception isn't caught as stated in my post "The exception itself is clear, the position "3" doesn't exist so naturally it would throw an exception, but it's inside of a try/catch block so it should get caught, but it doesn't."
Comment: Element 3 is not member of your vector. Use 2 instead.
Comment: @Shayna -- That is **not** an exception.  The Visual Studio debug runtime did a check on your `operator []` using `assert()`, and your out-of-bounds access triggered the assertion.   If you want to see it in action, put in your code `assert(1 == 2);` -- you should see the box pop-up, but with different text describing the assertion.
Answer: If you want std::vector to throw an std::out_of_range exception, you need to use the .at() method. The operator[] does not throw an exception.
For example, you can do something like this:
std::vector<int> myvector(10);
try {
    myvector.at(20)=100;      // vector::at throws an out-of-range
}
catch (const std::out_of_range& e) {
    std::cerr << "Out of Range error: " << e.what() << '\n';
}

Answer: That is not an exception. That is a debug assert failure.
If you want an exception you need to use vector's at(index) function and not the array subscript operator.
Answer: operator[] is overloaded in the vector container but is not exception safe (Behaviour is undefined in case of failure, such as in your post above)
You should use the .at() function instead. It is exception safe. The cplusplus.com reference says:
Strong guarantee: if an exception is thrown, there are no changes in the container.
It throws out_of_range if n is out of bounds.

Read:
http://www.cplusplus.com/reference/vector/vector/operator[]/
http://www.cplusplus.com/reference/vector/vector/at/
Look at the bottom for exception safety.


C: Program crashes trying to free 2D array itself

Question: I have a 2D float array contained within a structure n:
float **matrix;
This is dynamically allocated using:
n->matrix = (float**)malloc(n->rows * sizeof(float*));
for (i = 0; i < n->rows; i++) {
    n->matrix[i] = (float*)malloc(n->columns * sizeof(float));
}

Where n->rows and n->columns are defined just beforehand.
When it comes to de-allocation, the following function is used:
void de_allocate(float** matrix, int nrows) {
    int row;
    for (row = 0; row < nrows; row++) {
        free(matrix[row]);
    }
    free(matrix);
}

The program successfully get through the for-loop within the de-allocation process but gives HEAP CORRUPTION DETECTED [location of error]. CRT detected that the application wrote to memory after end of heap buffer.
Note that I am only trying to free the memory for the n->matrix not the structure itself.
It makes me think that n->data is a different length than what the free() method is expecting and trying to write over, but I'm none too sure.
Any ideas what could be causing the problem?
Comment: This is not a 2D array, but only an emulation of it. Such code belongs into the museum. Nowadays, you should have a C99 compatible compiler that is able to allocate a 2D in one go, `float (*matrix)[nrows] = malloc(sizeof(float[ncols][nrows]));` No need for a `for` loop and messing around with several calls to `free`.
Comment: @JensGustedt I've seen this exact same comment on several questions whilst I was searching for a solution, and unfortunately I can't use a C99 compiler for this, painful as it may be.
Comment: The code in the question looks okay to me. Have you tried using valgrind for more accurate debugging?
Comment: You might overflow a buffer somewhere, or pass the wrong number for `nrows`, or any other problem really. To get a good answer, post a [MCVE](http://stackoverflow.com/help/mcve)
Answer: Without knowing when exactly the problem occurs, it could be hard to track down. What you could do is:

Run the app via valgrind - it will tell you not only that the issue occurred, but also when / how.
Make sure the app crashes when it does something weird - -fsanitize=address or -fsanitize=undefined could be helpful.
Comment: The problem occurs when I try to free the array itself (not its elements) due to it trying to write outside of the heap buffer.
Comment: @user3893820 Not exactly. What you see when calling `free()` is the side effect of a problem which occurred much earlier - something wrote over the allocation data of the matrix. To figure out why you're crashing, you need to find where the initial write happened and fix that.
Comment: I see @viraptor. Oddly enough the problem still occurs if I use `calloc()` inside the for-loop to initialise the elements to zero and don't change the values before attempting to free things.
Comment: Ah, running valgrind showed that the error turned up during another function that was altering the structures themselves and messing things up, cheers.


The L deductive System in Propositional Logic

Question: I am trying to proof a few statements in the deductive system L, in propositional logic. The system contains 3 axioms (I, II, III below) and a few proven statements (1,2,3,4). In addition, the only inference rule is the modus ponens. In a book I found, the system differs from mine in the 3rd axiom. In the book, the 3rd axiom is (~b->~a)->((~b->a)->b) while my 3rd axiom is  (~b->~a)->(a->b). I wanted to ask, is it possible, given my axioms, statements the MP rule and the deduction theorem, to prove the 3rd axiom from the book? Thank you.
Answer: You will need Law of Clavius. For how to derive it using your System L, see help with some Hilbert style proofs in a propositional logic axiom system. Here's an answer to your question.
Theorem $\vdash(\neg\beta\rightarrow\neg\alpha)\rightarrow((\neg\beta\rightarrow\alpha)\rightarrow\beta)$
Proof:

$(\neg\beta\rightarrow\neg\alpha)$ (premise)
$(\neg\beta\rightarrow\alpha)$ (premise)
$(\neg\beta\rightarrow\neg\alpha)\rightarrow(\alpha\rightarrow\beta)$ (Axiom 3 in your System L)
$(\alpha\rightarrow\beta)$ (m.p. 1,3)
$(\neg\beta\rightarrow\beta)$ (Theorem/Statement 4 in your System L, combining 2,4)
$(\neg\beta\rightarrow\beta)\rightarrow\beta$ (Law of Clavius)
$\beta$ (m.p. 5,6) (conclusion)
Therefore $(\neg\beta\rightarrow\neg\alpha),(\neg\beta\rightarrow\alpha)\vdash\beta$ (1,2,7,)
$(\neg\beta\rightarrow\neg\alpha)\vdash(\neg\beta\rightarrow\alpha)\rightarrow\beta$ (deduction theorem)
$\vdash(\neg\beta\rightarrow\neg\alpha)\rightarrow((\neg\beta\rightarrow\alpha)\rightarrow\beta)$ (deduction theorem)
Answer: You would need some sort of rule of inference for you to change deductions from I., II., or III. to a form like p -> (q -> (r -> p).  Let '{' and '}' stand in place of the third parenthesis form types, considered by their size. Let '[' and ']' stand in place of the second form types, also considered by their size.
That might require a rule of inference to change
{p -> [q -> (r -> p)]}
into
p -> [q -> (r -> p)].
That last step does not accord with the rules of formation.
That all said, something similar does appear to seem possible:


Can I solve this limit?

Question: I'd just like to make sure I'm right about this. I'm pretty sure this limit is unsolvable, or at least not with easy algebra.
$$\lim_{x\to 5}\frac{x^2-5x+6}{x-5}$$
I tried completing the square to factor out the $x-5$ but that renders a fraction. So am I right to say this limit does not exist?
Comment: You don't even need to factorise it, really, just show that the polynomial in the numerator evaluates to something other than $0$ at $x=5$.
Comment: Why would you complete the square?  The numerator immediately factors as $(x-2)(x-3)$.  Use rational roots test if it isn't immediate for you...
Comment: Are you asked to prove that the limit doesn't exist?
Comment: Misakov: No, just note if it does not.
Comment: G. H. Faust: Oh yeah, that should have been obvious to me.
Answer: $$\lim_{x\to 5^+}\frac{x^2-5x+6}{x-5}=\frac{6}{0^+}=+\infty$$
$$\lim_{x\to 5^-}\frac{x^2-5x+6}{x-5}=\frac{6}{0^-}=-\infty$$
So limit does not exist!
Answer: HINT:
If $x-5\ne0,$
$$\frac{x^2-5x+6}{x-5}=x+\frac6{x-5}$$
Now if $x\to5,x\ne5$

Can I solve this limit?


Polynomials with Numpy

Question: I am trying to write Bernstein polynomial on python, but I have a problem with poly1d function. In Bernstein function the variable is t, but in poly1d function default variable is x. In the following codes I call variable as t:
1: print((nCr(2, 0)*((np.poly1d([1, 0], variable="t"))**0)*((np.poly1d([-1, 1], variable="t"))**(2-0))))

2: print(np.poly1d([-1, 1], variable="t"))

But I got outputs like that:
1: 1 x^2 - 2 x + 1

2: -1 t + 1

How can I solve my problem for the first one?
Answer: np.poly1d() provides a variable argument just for printing purposes. It doesn't actually use it in operations.
For example:
>>> vars(np.poly1d([1, 2], variable='t'))
{'coeffs': array([1, 2]), '_variable': 't'}

# but:
>>> vars(1 * np.poly1d([1, 2], variable='t'))
{'coeffs': array([1, 2]), '_variable': 'x'}

So, I would recommend adding the variable bit as a last step:
pol = np.poly1d((nCr(2, 0) * ((np.poly1d([1, 0]))**0)*((np.poly1d([-1, 1]))**(2-0))), variable='t')
print(pol)
# out:
   2
1 t - 2 t + 1

Polynomials with Numpy


A counter that decreases by 1,000 every 5 minutes

Question: Hello guys I have searched high and low for something like this.
So for example:
starting number 100,000
decreases to 99,000 after 5 minutes
thanks for all the help.
Comment: Then surely you have found something to start with?
Answer: var count=100000;
setInterval(function(){
  count-=1000;
},1000*60*5);

Answer: See bellow example :
Decrease by 1 seconde :

var number = 100000;
var i = 0;
setInterval(function(){
  number-= 3.333333333333;
  number = Math.round(number);
  document.getElementById("counter").innerHTML = number;
  console.log("number equals " +number +" - in " + (++i)+ "seconde");
},1000);
h3 {
  display:inline-block;
}

Counter :

100000

decrease by 5 min :

var number = 100000;
var i = 0;
setInterval(function(){
  number-= 1000;
  console.log("number equals " +number );
},1000*60*5);
Comment: Hello! thanks for this. I am having trouble making this show in a html file. Can you help me?
Comment: see my answer just add your html and above the html open


Orthogonal linear transformations and the dot product

Question: I am trying to solve a problem on the dot product, and I do some manipulations and come to the conclusion that
$\langle x, A^{T} Ax \rangle = \langle x,x \rangle$. $x$ is a column vector with $n$ rows, and $A$ an $n \times n$ matrix.
I know in general that if $a \cdot b = a \cdot c$, then this does not mean that $b = c$. However, from the above concerning the matrix $A$, can I conclude that the only way the left and right hand sides are equal is when $A^{T}A = I$, the identity matrix? Otherwise how can the two quantities be equal?
If the statement were not true, I would be glad if someone could provide a counter example.
Thanks
Answer: $\langle x, A^{T} Ax \rangle = \langle x,x \rangle$ implies $\langle x, (A^{T} A-I)x \rangle = 0$. Now it is true that $\langle x, Tx \rangle = 0$ for all $x$ implies $T=0$ because you can  write $4\langle x, Ty \rangle = \langle x+y, T(x+y) \rangle - \langle x-y, T(x-y) \rangle = 0$. (This is a polarization identity.) Taking $x=Ty$ implies $Ty=0$ for all $y$, and so $T=0$.
There's a related question, which you can use once you realize that $\langle x, A^{T} Ay \rangle$ defines an inner product.
Comment: BTW, I strongly recommend Axler's [Linear Algebra Done Right](http://linear.axler.net/index.html). The [sample chapters](http://linear.axler.net/LADRSampleChapters.html) are  especially relavant to your question.
Comment: Hi I've seen the book and the chapters and the bit relevant starts talking about self-adjoint operators and stuff. But what about this: if $\langle x , Tx \rangle = 0$, then this must be true for all $x \in \mathbb{R}^n$. The only way this can be true is if $T$ is the zero matrix. What do you think?
Comment: @D Lim, I meant Proposition 7.2 on page 129.


Div selection switch

Question: So basically i wanted to make a set of divs that can be selected.
If a div clicked and mouse is moved, it will do something to all selected divs.
I wanted to test with 1 div selection first but I'm facing a problem.
If i clicked and move the mouse to quickly it will change the cursor to no-drop and the function wont execute unless I clicked again.
Here is the code:

boxes = document.querySelectorAll('.box');
let itemList = [];
let selectedList = [];
//I made selectedList because i want to put multiple selection later.

for (let box of boxes) {
  itemList.push(box);
  box.addEventListener("mousedown", mousedown);
};
//Each box listen mousedown and put into itemList.

function mousedown(e) {
  item = e.target;
  deselectAll(); //First, i will deselect all div if one of the divs clicked

  item.selected = true; //Then, i select the div that clicked
  selectedListUpdate(); //Then i update the selectedList to determine which                           one selected and not

  window.addEventListener('mousemove', mousemove);

  function mousemove(e) {
    for (let item of selectedList) {
      item.style.background = getRandomColor();
      //This function will do something to all item in selectedList if the           mouse move.
      //Later, i want to change this function to function that can drag the         div.
    };
  };

  window.addEventListener('mouseup', mouseup);

  function mouseup(e) {
    window.removeEventListener('mouseup', mouseup);
    window.removeEventListener('mousemove', mousemove);
  }; //If i let go the mouse, moving mouse no longer do something but selected items are still selected
};

function selectedListUpdate() {
  selectedList = itemList.filter(x => x.selected == true);
  for (let x of selectedList) {
    x.style.outline = "white solid 2px";
  };
  unselectedList = itemList.filter(x => x.selected == false)
  for (let x of unselectedList) {
    x.style.outline = "0";
  };
}; //outline every item in selectedList

function deselectAll(e) {
  for (x of itemList) {
    x.selected = false;
  };
  selectedList = [];
}; //Reset selectedList

function getRandomColor() {
  let r = Math.floor((Math.random() * 155) + 100);
  let g = Math.floor((Math.random() * 155) + 100);
  let b = Math.floor((Math.random() * 155) + 100);
  return `rgb(${r} ${g} ${b})`;
}; //Example of function to execute when mouse is moving
body {
  margin: 0px;
  background: #333;
}

.box {
  margin: 10px;
  background: white;
  width: 100px;
  height: 100px;
<!DOCTYPE html>

  <meta charset="UTF-8">
  <title>TEST</title>

Answer: I think cancelling the drag event on the boxes seems to work?

boxes = document.querySelectorAll('.box');
let itemList = [];
let selectedList = [];
//I made selectedList because i want to put multiple selection later.

for (let box of boxes) {
  itemList.push(box);
  box.addEventListener("mousedown", mousedown);
  box.addEventListener("dragstart", dragStart);
};
//Each box listen mousedown and put into itemList.

function mousedown(e) {
  item = e.target;
  deselectAll(); //First, i will deselect all div if one of the divs clicked

  item.selected = true; //Then, i select the div that clicked
  selectedListUpdate(); //Then i update the selectedList to determine which                           one selected and not

  window.addEventListener('mousemove', mousemove);

  function mousemove(e) {
    for (let item of selectedList) {
      item.style.background = getRandomColor();
      //This function will do something to all item in selectedList if the           mouse move.
      //Later, i want to change this function to function that can drag the         div.
    };
  };

  window.addEventListener('mouseup', mouseup);

  function mouseup(e) {
    window.removeEventListener('mouseup', mouseup);
    window.removeEventListener('mousemove', mousemove);
  }; //If i let go the mouse, moving mouse no longer do something but selected items are still selected
};

function dragStart(e) {
  e.preventDefault();
  return false;
}

function selectedListUpdate() {
  selectedList = itemList.filter(x => x.selected == true);
  for (let x of selectedList) {
    x.style.outline = "white solid 2px";
  };
  unselectedList = itemList.filter(x => x.selected == false)
  for (let x of unselectedList) {
    x.style.outline = "0";
  };
}; //outline every item in selectedList

function deselectAll(e) {
  for (x of itemList) {
    x.selected = false;
  };
  selectedList = [];
}; //Reset selectedList

function getRandomColor() {
  let r = Math.floor((Math.random() * 155) + 100);
  let g = Math.floor((Math.random() * 155) + 100);
  let b = Math.floor((Math.random() * 155) + 100);
  return `rgb(${r} ${g} ${b})`;
}; //Example of function to execute when mouse is moving
body {
  margin: 0px;
  background: #333;
}

.box {
  margin: 10px;
  background: white;
  width: 100px;
  height: 100px;
<!DOCTYPE html>

  <meta charset="UTF-8">
  <title>TEST</title>

Comment: Oh ya. I just realized i didn't disabled the drag event. Seems to work now tho. Thank you!! :)

Div selection switch


Module recommendation needed for recurring monthly membership plan?

Question: I'm building a site that features yoga/excercise videos and will require users to sign up as members which will require a recurring monthly payment. Could someone recommend which modules to use for this? i see there are a few different options available. Any experience with something like this or advice would much appreciated, thanks!
i also need to make these videos inaccessible to non-members, something else to consider
Comment: This question appears to be off-topic because it is a [shopping question(http://blog.stackoverflow.com/2010/11/qa-is-hard-lets-go-shopping/) and should not be asked here. You can ask how to do things, but recommendations are too opinion based and too easy to get outdated and no longer valid.
Answer: There are a few options available.
I've seen this: https://www.drupal.org/project/commerce_recurring which integrates with Drupal commerce. It doesn't have a stable version though.
For something more stable, and which is independent of the Drupal commerce module, try Recurly
https://www.drupal.org/project/recurly
Recurly is a 3rd party subscription billing system.
Using the Recurly roles module: https://www.drupal.org/project/recurly_roles
You can have people subscribe to your website, and assign a paid membership user role after signup and subscription. If a user stops the subscription, then you can take away the role.
Then configure your videos to only be accessible to the paid member role.


Variance of a particular stochastic process

Question: Given the following stochastic process:
$$X_t = \epsilon_t\epsilon_{t-1}~~~~,~\epsilon_t \sim N(0, \sigma^2)$$
Which is the variance of the process $X_t^2$?
Firstly I defined the process $X_t$ as:
$$W_t = (\epsilon_t\epsilon_{t-1})^2= \epsilon_t^2\epsilon_{t-1}^2$$
Then its expected value as:
$$E[W_t]= E[\epsilon_t^2\epsilon_{t-1}^2]=E[\epsilon_t^2]E[\epsilon_{t-1}^2]= \sigma^4$$
And the variance as:
$$\text{Var}[W_t]=E[(\epsilon_t^2\epsilon_{t-1}^2-E[X_t])^2]$$
$$=E[\epsilon_t^4\epsilon_{t-1}^4]-2E[\epsilon_t^2\epsilon_{t-1}^2]\sigma^4+\sigma^8 = \sigma^8 - 2\sigma^8+\sigma^8=0$$
I am not able to understand where is my error.
Comment: is $\mathbb{E}\epsilon^4 = \sigma^4$?  Think carefully...
Comment: Are $\epsilon_t$ and $\epsilon_{t-1}$ independent? Because you are using the property of independence in your derivations. Also, are you interested in the variance of $X_t$ or $W_t$? You start with the former but end with the latter.
Comment: Yeah here is the problem..... $E\sigma^4$ is the kurtosis
Comment: $E[\epsilon^4]=3\sigma^4$
Comment: Hint: first solve the general problem of computing the variance of $XY$ in terms of moments of $X$ and $Y$ where $X$ and $Y$ are independent random variables.  Apply this to the case where $X=\epsilon_t^2$ and $Y=\epsilon_{t-1}^2$. That reduces the problem to finding the second and fourth moments of a zero-mean Normal variable.
Answer: \begin{eqnarray*}
Var(X_t^2) &:=& Var(W_t) \\
&=& E[W_t^2] - E[W_t]^2 \\
&=& E[X_t^4] - E[X_t^2]^2 \\
&=& E[(\varepsilon_t\varepsilon_{t-1})^4] - E[(\varepsilon_t\varepsilon_{t-1})^2]^2
\end{eqnarray*}
Recall that the product of two standard normal random variables (that is, $N(0,1)$) is distributed according to a $\chi^2_1$ distribution. Then,
\begin{eqnarray*}
\varepsilon_t\varepsilon_t &=& \sigma^2\frac{\varepsilon_t}{\sigma}\frac{\varepsilon_t}{\sigma} \\
&:=& \sigma^2Y \\
\Rightarrow Y &\sim& \chi^2_1 \\
\Rightarrow \sigma^2Y &\sim& \Gamma\left(\frac{1}{2},2\sigma^2\right) \\
\Rightarrow \varepsilon_t\varepsilon_t &\sim& \Gamma\left(\frac{1}{2},2\sigma^2\right)
\end{eqnarray*}
The second implication in the above work is taken from this link. Note that the exact same holds for $\varepsilon_{t-1}\varepsilon_{t-1}$ because $\varepsilon_t$ and $\varepsilon_{t-1}$ follow the same distribution. Thus we can write $X^4$ as $(\varepsilon_t\varepsilon_t)^2(\varepsilon_{t-1}\varepsilon_{t-1})^2$, where $X$ follows a $\Gamma\left(\frac{1}{2},2\sigma^2\right)$ distribution.
For any random variable $X$, $E[X^n] = M^{(n)}_X(0)$, where $M^{(n)}_X(0)$ is the $n$th derivative of the moment generating function (mgf) of $X$, evaluated at 0. (This is Theorem 2.3.7 in Casella and Berger's Statistical Inference.) The moment generating function for a $\Gamma(\alpha,\beta)$ variable is $M_X(t)=\left(\frac{1}{1-\beta t}\right)^\alpha$. Then...
\begin{eqnarray*}
E[X_t^2] &=& M^{(2)}_X(0) \\
&=& \frac{d^2}{dt^2}\left(\frac{1}{1-\beta t}\right)^\alpha\biggr\rvert_{t=0} \\
&=& \frac{d^2}{dt^2}(1-\beta t)^{-\alpha}\biggr\rvert_{t=0} \\
&=& \frac{d}{dt}(-\alpha\cdot(1-\beta t)^{-\alpha-1}\cdot-\beta)\biggr\rvert_{t=0} \\
&=& \frac{d}{dt}(\alpha\beta(1-\beta t)^{-\alpha-1})\biggr\rvert_{t=0} \\
&=& \left((-\alpha-1)\cdot\alpha\beta(1-\beta t)^{-\alpha-2}\cdot-\beta\right)\biggr\rvert_{t=0} \\
&=& \left(\beta(\alpha+1)\alpha\beta(1-\beta t)^{-\alpha-2}\right)\biggr\rvert_{t=0} \\
&=& \left(\frac{\beta(\alpha+1)\alpha\beta}{(1-\beta t)^{\alpha+2}}\right)\biggr\rvert_{t=0} \\
&=& \left(\beta^2\alpha(\alpha+1)\right)
\end{eqnarray*}
You can find $E[X^4]$ in the same manner or by using formulas available online. Be careful, however, as some Web sites will have different parameterizations of the Gamma distribution.
\begin{eqnarray*}
E[X^4] &=& M^{(4)}_X(0) \\
&=& \beta^4\alpha(\alpha+1)(\alpha+2)(\alpha+3)
\end{eqnarray*}
From the top, we had:
\begin{eqnarray*}
Var(X_t^2) &=& E[X_t^4] - E[X_t^2]^2 \\
&=& \beta^4\alpha(\alpha+1)(\alpha+2)(\alpha+3) - \left(\beta^2\alpha(\alpha+1)\right)^2 \\
&=& \beta^4\alpha(\alpha+1)(\alpha+2)(\alpha+3) - \beta^4\alpha^2(\alpha+1)^2 \\
Var(X_t^2) &=& \beta^4\alpha(\alpha+1)\left[(\alpha+2)(\alpha+3)-\alpha(\alpha+1)\right]
\end{eqnarray*}
Comment: The product of two *independent* standard Normals cannot possibly have a $\chi^2$ distribution, because half the time that product is negative!
Comment: You're right. Let me reconsider and edit my post.
Answer: The mistake of the OP lies in the last line of the post: in reality
$$E[\epsilon_t^4\epsilon_{t-1}^4] \neq \sigma^8$$
because, under assumed independence,
$$E[\epsilon_t^4\epsilon_{t-1}^4] = E[\epsilon_t^4]\cdot E[\epsilon_{t-1}^4]$$
and
$$E[\epsilon_t^4] = E[(\epsilon_t^2)^2] > [E(\epsilon_t^2)]^2 = (\sigma^2)^2 = \sigma^4$$
due to Jensen's Inequality.


Pass graph property names to gephi using apoc.gephi.add()

Question: I am streaming graphs from neo4j to Gephi using the apoc.gephi.add(). The method only passes one property of the node as a node label in Gephi. Which is not only desired node label I want. Is there a way to transfer other property as a node label to Gephi?
For example, My query is as below:
MATCH p=(a:Artist)-[r:LOVES]->(b:Artist) WITH p LIMIT 5
call apoc.gephi.add('http://localhost:8080','workspace2', p) yield nodes, relationships, time
return nodes, relationships, time

In the above query, it only shows the names of the artists.

The above nodes has other properties like type, year_of_work etc. I want to display the other properties too in the node in Gehi. The apoc method only pass one property as node label. Following is the Node table in Gephi.

So is there a way to pass other properties? Is there any other way to stream a graph in Gephi from neo4j with the desired behavior?
Comment: I tried to add the `properties` yield option but it still didn't work
Answer: This functionality was just recently added. You can use:
MATCH p=(a:Artist)-[r:LOVES]->(b:Artist) WITH p LIMIT 5
call apoc.gephi.add('http://localhost:8080','workspace2',p,'weight',['type', 'year_of_work']) yield nodes, relationships, time
return nodes, relationships, time

Where fourth parameter can be used to export weight, and fifth parameter as an array of all the properties you want to export from both nodes and relationships. Check documentation for more.
Answer: All examples in the APOC Procedure docs show the paths being collected before the apoc.gephi.add call. So try it:
MATCH p=(a:Artist)-[r:LOVES]->(b:Artist)
WITH p LIMIT 5
WITH collect(p) AS ps
call apoc.gephi.add('http://localhost:8080','workspace2', ps) yield nodes, relationships, time
return nodes, relationships, time

Comment: This solution doesn't work. I tried that. It shows the same behavior.


Select registros menores que a data e hora atual

Question: Qual a melhor forma de fazer uma consulta a base dados onde os dados sejam menores que a data e hora atual
SELECT * FROM agenda WHERE agendamento < '".date('Y-m-d H:m:s')."'

Answer: Você pode utilizar a própria função do MySQL.
Caso sua coluna possua hora:
SELECT *
  FROM agenda
 WHERE agendamento < NOW()

NOW
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS' format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.

Em tradução livre:

Retorna a data e hora atual como o valor no formato 'YYYY-MM-DD HH:MM:SS' ou YYYYMMDDHHMMSS, dependendo do contexto em que a função é utilizada (string ou numérico). O valor é expresso na time zone atual.

Pode testar aqui!
Caso sua coluna seja apenas de data:
SELECT *
  FROM agenda
 WHERE agendamento < CURDATE()

CURDATE
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.

Em tradução livre:

Retorna a data atual como o valor no formato 'YYYY-MM-DD' ou YYYYMMDD, dependendo do contexto em que a função é utilizada (string ou numérico).
Comment: @MarcosPaulo dessa forma você não precisará passar parâmetro. Mais simples e direto do que isso é impossível.
Comment: @Sorack , bem simples, porem estou trabalhando com uma empresa que tem a versão 5.5.53 do Mysql, e pelo que conheço do pessoal la, não poderei atualizar, pois existem outros sistemas funcionando

So descobri isso agora que coloquei a consulta que vc me passou e deu  erro

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1
Comment: @MarcosPaulo a versão 5.5 também possui a função `NOW`. Provavelmente o erro está na forma em que você está montando a sua `string` com a `query`
Comment: puts, foi um pequeno vacilo, ficou uma aspas simples solta, agora não aparece o erro, porem não retornou nada
Comment: @MarcosPaulo cola aqui como ficou sua `query`
Comment: Retirei a data atual e coloquei ela separada , ai deu certo
Answer: $today = date("d/m/Y");
$dia = date("d");
$mes = date("m");
$ano = date("Y");
$total = 0;

function selectAllPessoa(){
    $banco = abrirBanco();
    $sql = "SELECT * FROM agenda WHERE status = 'N' AND(DAY(data_consulta)='$dia') AND (MONTH(data_consulta)) ORDER BY nome";

fiz assim por anos mas no meu codigo parou de fncionar tenta aí1


Do not login user after registering if user already logged in

Question: Yes, you read that right. Let me explain. I am using the default authentication process that comes with laravel 5.4, and am looking to allow admin users the ability to register new users. I have modified the middleware RedirectIfNotAuthenicated to allow access to /register if the user who is logged in is admin (in my case that means they have a type of '1'):
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {

        if(!Auth::user()->type == '1'){
            return redirect('/');
        }

    }

    return $next($request);
}

My question now becomes, how within the existing RegisterController do I NOT log the user in upon successful registration if the account is being created by an admin. Obviously this will involve a check to see if the user is currently logged in and an admin, but where I'm hung is after that block, as to me it looks like something that is being done elsewhere?
protected function create(array $data)
{

    if(isset($data["director"])){
        $this->redirectTo = '/organizations/create';
    }

    if(Auth::user()->type == '1'){

        //create the user and not log them in

    } else {

        return User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

    }

}

Answer: First of all, you don't actually need to use the RegisterController shipped with Laravel, to create users as admin. In whatever the AdminController you use, you can have the following code to create a new user with proper validation of cause:
User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

You just need to persist the user data in the respective tables and once the user logs in from frontend LoginController will handle the rest.
Also, RegisterController shipped with Laravel uses RegistersUsers Trait, which has the additional functionality you noticed in the controller.
Illuminate\Foundation\Auth\RegistersUsers.php

Comment: This does appear to be the best way to handle the situation. I added logic to the default register view that checks if user is guest or admin so that it can be reused with a separate admin controller (if admin post to a different route). My main concern was maintaining the same form in two separate locations.


Rotate and resize image with jquery and CSS

Question: I have an image img.png with dimensions: width=300px, height=200px.
This image is in a div container:

And the CSS are:
div.thumbnail {
    width: 500px;
    height: 300px;
}

div.thumbnail > img {
    height: 100%;
    display: block;
    margin-left: auto;
    margin-right: auto;
}

The image is displayed nicely. However, dynamically I can rotate the image with jquery using:
$("#imgX").css("transform","rotate(90deg)");

With this the image is rotated but it's out of the div container. How can I keep it inside?
Comment: Pls show us the image you are using ?
Comment: probably its better to rotate the entire thumbnail container $(".thumbnail").css("transform","rotate(90deg)");
Answer: Not sure if that's what you mean, but what about
div.thumbnail {overflow:hidden;}

Comment: Good idea! But I'd rather resize the image. But what's the simplest way to do it?
Comment: `var image = $("#imgX"); image.css("width",image.height());` because after rotating 90deg the vertical edge is called width. But still needs some improvement.


Which method is correct? (generalized additive model, mgcv)

Question: If possible could you please let me know if I am on the right track? I don't know anyone who works with GAMs who I can ask and I would be so appreciative of any help I receive. I use mgcv in R.
My data is daily flower counts ('Total'), with different plant genetic lines ('RIL') and 4 different treatments ('Trt'). I basically want to compare the 4 treatments and see if there is a difference in flower production over time between them. Are the shapes of the flowering curves different?
An example of my gam:
gam1 <- gam(Total ~ Trt * RIL + s(DateNum, k = 9, bs = "fs") + s(Plant, bs = "re"), data=ce1230)

I understand that using anova.gam(gam1) I can see if there is a significant difference between treatments and rils. However when I plot gam1, it shows one curve that represents everything. I was hoping to see a plot with separate curves for each of my 4 treatments. Does this mean that I need to make gamtrt1, gamtrt2, gamtrt3, gamtrt4 and somehow compare the separate gams to each other? or use the original anova(gam1) and just plot each treatment separately? Or am I looking at this the entirely wrong way?
Comment: As you have it structured, the treatment only affects the intercept, not the slope over time.  In order to get an interaction between the `Trt` and the `DateNum` smooth, which is what I suspect you want, I believe you have to add `by=Trt` to the interior of the `s(DateNum,...` smooth term and you will also need to have `Trt` as a factor term independent of `RIL`, i.e., `Total ~ Trt + Trt*RIL + s(DateNum, by=Trt, k=9, bs="fs") + ...`.
Comment: Assuming you want to smooth on `RIL` and make separate smooths, see the
help on "Specifying generalized additive models"; `?gam.models`, particularly the section on `by` and the corresponding example.
Comment: @jbowman: the `bs="re"` makes it a random effect.
Comment: What is "Plant"?  The reason I ask is that it sounds categorical, in which case a smooth term for it may not be appropriate.
Comment: I had 840 plants in my experiment, so the term Plant was supposed to be for any random effect due to individual plant differences in flower output.
Comment: How are you plotting? The help page for `gam` suggests `plot(gam1, pages=1, residuals=TRUE)`, which puts multiple plots on the same output. Without the `pages=1` it should ask you to hit return to see the next plot, are you getting that?
Comment: Also, it only plots the "smoothed" variables; in this model RIL is just a linear term, is that what you want?
Answer: I'd guess you want the by parameter for s.  The resulting set of plots then has one for each level of the by factor.  In this example from ?gam.models, there are three levels of fac, so there are three plots for x2 and one for x0.
dat <- gamSim(4)
b <- gam(y ~ fac + s(x2, by=fac) + s(x0), data=dat)
plot(b, pages=1)

Comment: Thank you! This is exactly what i was looking for- Adding `by=Trt` in the `s(DateNum)` term gave me a plot for each treatment. Here are my graphs: https://ibb.co/iWvZMy - Hopefully the link works!
Do you have any insight as to why the GAM fitting my data would make my flower counts (y-axis) become negative? Obviously a plant cannot have -10 flowers. Is there a way to tell the GAM to not be negative? Sorry if these questions sound silly.
Comment: I used this `plot_smooth(gam1, view="DateNum", plot_all="Trt", rm.ranef=TRUE)` from itsadug, and it gave me this graph: https://ibb.co/bP6r1y, which is pretty and allows me to see both treatments on the same graph, but the peak of the graph is different. And it still becomes negative. But it looks more like my data.
Comment: I suppose this might be okay, if I just don't graph the negative portions? https://ibb.co/nKKNPJ
Comment: If negative values are impossible, then your model should reflect that. Don't just hide the parts that don't work for you! A Poisson model (as @AnonymousEmu mentioned) would be worth investigating, or at least log-transforming the response.
Answer: I would first try an ANOVA and look at that F-test. If it rejects that, there is evidence that there is variation between treatments. After that, since your outcome if count data, I would do a poisson regression. Unlike GAMs where it can be hard to test the important of effects, Poisson regression gives you your usually p-values which makes it simple to see which things are important to causing the differences in your data.
https://stats.idre.ucla.edu/r/dae/poisson-regression/
If there is significnet difference between your mean and variance, (poisson says that they should be the same), modify the poisson regression with a Poisson-Gamma model.
Comment: I'm confused... why would switching to a Poisson regression be appropriate? The point of the GAM is to deal with non-linearity, not count data or non-constant variance.
Comment: I'm not sure if a Poisson regression would work as one of the assumptions is that the data needs to have independence of observations, and because I am counting flower production on the same plants each day, flower production on day 3 is not independent of day 2, etc. @Aaron do you still think that this is appropriate?
Comment: Just to be sure, could you be explicit in what ways they would not be independent?
Comment: The number of flowers produced by a plant each day depend on # of flowers the days before due to use of resources by the plant. Flower production in the plant I am studying typically follows a pattern - increase flower production each day until the plant hits peak flower production, then afterwards flower production declines each day as resources are also now being used to mature seeds. Ex: flower counts on plant#10 over 13 days might be 1, 4, 10, 16, 25, 39, 44, 33, 31, 25, 10, 5, 0, (plus perhaps some unpredictable flowering at the end due to treatment effects). And I have 840 plants total.
Comment: I didn't read the question fully -- I see now they are counts! Of course a Poisson regression would be worth investigating.
Comment: Non-independence like that is "independent" (hah!) of whether or not you used a Poisson model. That is, you need to account for the dependence no matter what model you use, even the additive model you were asking about to start with.
Comment: For the gam model, would that mean a term for autocorrelation?
Answer: You have some problems with your data; you can't have a random effect of Plant as a factor and get the plot you showed in the link in comment to @Aaron's Answer. Are you sure that Plant is a factor with more than 1 level? If not, you need to code your data correctly to get a random intercept per plant. Also, can you include both RIL and Plant level effects? Once you've accounted for the separate Plant effects (intercepts), won't that also logically account for the genetic line effects also?
Second, if you are using bs = 'fs', you need to pass in a continuous variable and a factor; so far you only pass in DateNum. At the moment you have
s(DateNum, k = 9, bs = "fs")

and I think you wanted
s(DateNum, Trt, k = 9, bs = "fs")

This model is similar to the one proposed by @Aaron, but is somewhat different in detail. The full model might be
gam(Total ~ Trt * RIL + s(DateNum, Trt, k = 9, bs = "fs") + s(Plant, bs = "re"),
    data=ce1230)

but I suspect even that is wrong? (For fs smooths you don't need the parametric Trt.) So I think,
gam(Total ~ RIL + Trt:RIL + s(DateNum, Trt, k = 9, bs = "fs") +
    s(Plant, bs = "re"), data=ce1230)

Where the main effect of Trt is actually contained in the fs smooth, so we don't specify it parametrically.
The main difference between this model and @Aaron's in that here, there is a single smoothness parameter for the smooths of DateNum by Trt, whereas in @Aaron's answer each of the smooths gets it's own smoothness parameter. This boils down to a choice between whether you expect similar wiggliness (the shapes of the smooths can be different) for each smooth or whether you expect some of the four smooths to be a lot wigglier than others?
What you seem to want is interactions between RIL and Trt plus separate smooths for each Trt. But do you want separate smooths for each RIL and Trt combination? That would require a separate variable formed by the combinations of RIL and Trt available in your data:
ce1230 <- transform(ce1230,
                    RILTrt = interaction(RIL, Trt, drop = TRUE))

And then you could fit
gam(Total ~ s(DateNum, RILTrt, k = 9, bs = "fs") +
    s(Plant, bs = "re"), data=ce1230)

or
gam(Total ~ s(DateNum, k = 9, by = RILTrt) +
    s(Plant, bs = "re"), data=ce1230)

depending on whether you wanted similar wigliness (use bs = 'fs') or different wigglinesses (use by) for each estimated smooth of DateNum.
The model also needs to respect the non-negative nature of the response; you have counts and you can't have negative counts of anything. Using family = poisson or family = nb would be reasonable starting points.
Comment: You are amazing! Thank you so much for this in depth answer. I will try all of these once I get a chance to properly sit down with R. I included the plant term because someone mentioned to me that each plant in my experiment (#1-840) will likely produce flowers slightly different from each other and I needed a term in my model to account for the variation.  So no, plant does not have more than one level. The column 'Plant' in my data sheet is just the label # on that plant's pot. Also the column RIL in my data is categorical - indicates if the plant is one of the several genetic lines I used.
Comment: @Sarah `plot.gam` shows the smooths on the scale that they are estimated on (the log scale for `poisson` and `nb` families, by default) and *most importantly* the smooths are subject to a sum-to-zero constraint for identifiability reasons. This constraint centres all the smooths about 0 (to which the intercept gets added to ultimately give predictions). Hence you should see negative values here, and even negative predictions. But once you put predictions on the response scale, through the use of the inverse of the link function, there will be no negative values.
Comment: I basically want to be able to see if the treatments differ and then how the RILs within each treatment vary in their response. I think what I want is closest to the second option you gave me, with separate smooths for each RIL / Treatment combination. Thank you for providing that code to me!  That would be hectic to graph all together, so I'd like to make one more general graph (x= date, y=flower count) with a smooth for each treatment overall, and then a graph for each treatment that shows the response of the RILs within that treatment.
Comment: Even with your new code suggestion and trying family=poisson or family = nb, I am still seeing the same issue with the negative values in the response when I plot(gam). The gam also does not represent the maximum flowers produced either - its almost like the whole curve has been shifted down from where it actually should be. I am not sure how to solve this.
Comment: Hi again Gavin, after a long August of growth chamber experiments, I'm finally able to sit down with the gams again. I followed your advice constructing the gam models with separate smooths for each RIL and Trt combination:
`day1943ce.int <- transform(day1943ce, RILTrt = interaction(RIL, Treatment, drop = TRUE))`
`m3<-gam(Total ~ s(Day, RILTrt, k = 9, bs = "fs"), data=day1943ce.int)`

This gives me the plot: https://paste.pics/27042ec5b88ebde84417e093c22317ef
(I still need to figure out predict.gam to get the data back on the response variable).
Comment: Is there a post hoc test that can tell me which of these curves are significantly different from each other? This graph is the response of flower counts through time after my damage treatment, and it would be nice to know which specific RILTreatment combinations have different responses.
Comment: This is the output for summary(gam)

`Formula: Total ~ s(Day, RILTrt, k = 9, bs = "fs")

Parametric coefficients:

            Estimate Std. Error t value Pr(>|t|)

(Intercept)   -3.446     14.128  -0.244    0.807

Approximate significance of smooth terms:
                edf Ref.df     F p-value
s(Day,RILTrt) 86.68    107 33.24  <2e-16 ***

R-sq.(adj) =  0.764   Deviance explained = 78.3%
GCV = 181.54  Scale est. = 166.92    n = 1089`

So sorry for the horrible formatting. I can't seem to figure it out
Comment: @Sarah 1: `predict.gam()` has argument `type` which if you set to `"response"` will give you predicted values on the response scale, just don't use the standard errors of the predictions to forma confidence interval on the predictions as it will be wrong. You can always get the correct inverse link function to do any backtransformation to the response scale using `ilink <- family(model)$linkinv`, where `ilink` I now a function you can use to backtransform a vector of predicted values.
Comment: 2: This is possible but will require some tedious coding and will require you to follow the ideas in this [blog post](https://www.fromthebottomoftheheap.net/2017/10/10/difference-splines-i/) but modify the code there to work with `fs` smoothers. I don't think I have a clear idea how you can compare all pairs of curves *and* correct for doing all those multiple comparisons.
Comment: It has been suggested that simultaneous intervals for all differences be evaluated at once (i.e. we want to expand the standard interval by some amount such that you don't just include 95% of all draws from the posterior distribution of the difference between one pair of curves, but include 95% of posterior draws from the differences of all pairs of curves). This is doable but I'm not aware that anyone has coded this up yet.

3: You can't post code blocks in comments.
Comment: If you have a specific problem re interpreting this model, perhaps as this as a question. Otherwise I doubt we'll get far using comments and Moderators might frown on this - [stats.se] is not a discussion forum. I'd be happy to talk about this more in a chat room here if you want, but I'm travelling until the weekend so I won't be able to chat until early next week. I'm also not promising I can help much with code to do what i said just yet - I have a few other pressing work commitments that I need to attend to.


The text of the message will be out of the parent

Question: Development Environment
・ react
・ typescript
・ styled-components
The messages are displayed using the map.
If the text is long, the text will protrude from the parent.
I want the text to wrap around the parent if it extends beyond it.
Even if you apply box-sizing to the parent tag, if the text in {message.data.body} is too long, it will stick out from the parent.
const Main = styled.main`
  padding-left: 54px;
  display: grid;
  grid-template-columns: minmax(160px, 400px) 1fr;
  overflow: hidden;
`;

const Message = styled.li<TMessage>`
  display: flex;
  justify-content: ${({ user }: TMessage) => (user ? 'flex-end' : 'flex-start')};
  padding-bottom: 16px;
  .right {
    margin-right: 8px;
    background: #0068b4;
    color: #ffffff;
    font-size: 12px;
    padding: 8px 12px;
    border-radius: 20px 20px 0px 20px;
    white-space: pre;
  }
  .left {
    background: #f0f2f5;
    color: ${({ user }: TMessage) => (user ? '#FFFFFF' : '#000000')};
    padding: 8px 12px;
    font-size: 12px;
    border-radius: 20px 20px 20px 0px;
    word-break: break-word;
    max-width: 870px;
  }
`;

return (
  <MatchContainer>

            {messages.map((message) =>
                <Message key={message.data.id} user={message.data.fromMe}>
                  {message.data.fromMe && (

{message.data.body}

                  )}
                  {message.data.fromMe === false && (

{message.data.body}

                  )}
                </Message>
              )}

  </MatchContainer>
);

style.scss

  section {
      &.section-one{
        display: grid;
        /* grid-template-rows: 1fr auto; */
        align-content: start;
        overflow-y: auto;
        padding: 16px;
        border-right: 2px solid #e1e6ed;
      }
      &.section-two{
        position: relative;
        border-right: 2px solid #e1e6ed;
        display: grid;
        overflow: auto;
        grid-template-rows: auto 1fr;
      }
      &.bubble-message {
        display: grid;
        grid-template-rows: 1fr auto;
        border-right: 2px solid #e1e6ed;
        overflow-y: auto;
      }
      &.room-profile {
        padding: 24px;
        overflow-y: auto;
      }

div {
  &.meessage-container {
    display: grid;
    grid-template-columns: 1fr minmax(300px, 450px);
    overflow: auto;
    grid-template-rows: 1fr auto;
  }
}

Answer: You can either add overflow-wrap or word-wrap css properties to the parent and set them to either break-word or normal
Comment: I applied overflow-wrap: normal to const Message, but it didn't change.
Answer: Try to apply both properties:
overflow-wrap: break-word;
word-wrap: break-word;


Can't get non-nullable feature to work on flutter

Question: I've set my SDK to >=2.10.0-0 <3.0.0 on pubspec.yaml, which is the minimum version the warning tells me to use, and then tried running both Pub get and Pub upgrade but nothing is happening.
Is there a step I'm missing?
Comment: @olibiaz weird. The warning only told me to use 2.10.0-0 or higher. Switched to dev channel, upgraded and it started working. Can you create an answer so I can give you the correct answer tag?
Comment: Do you have more debug information (error traces etc..)? Did you switch to beta channel, which version of flutter and dart do you have?
Comment: @olibiaz There's no error, I just can't compile because it keeps giving me the error on non-nullable when I try to. Running flutter --version gets me Dart version 2.10.5 and Flutter version 1.22.6
Comment: when you run `flutter --version`, do you see that you are in beta channel? `Flutter 1.25.0-8.1.pre • channel beta, Dart 2.12.0 (build 2.12.0-133.2.beta` I think the null sound safety is still only in beta. So your Dart should be at least `2.12`
Answer: It probably a problem of version. Currently, i think the null sound safety is only on the beta channel, so your Dart should be 2.12 or greater.
So you should have something like that:
> flutter --version

Flutter 1.25.0-8.1.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision 8f89f6505b (2 months ago) • 2020-12-15 15:07:52 -0800
Engine • revision 92ae191c17
Tools • Dart 2.12.0 (build 2.12.0-133.2.beta)

Try upgrading your libraries and switch to beta channel:
flutter channel beta

More info here: Null safety announcement


What does "$s-$" mean in $F(s_n)\uparrow F(s-)\le F(s)$

Question: In the case at hand $F$ is the distribution function for a random variable and $s_n \uparrow s$, and so $F(s_n)\uparrow F(s)$ but what about this $F(s-)$?
Thanks in advance!
Comment: It's just a guess, but if the distribution has a discrete "density" at $s$, this might be shorthand for the limit of $F(x)$ as $x$ approaches $s$ from below.  More context would be helpful to understand your Question.
Answer: It's the notation to show the limit from under : $$F(s-) = \lim\limits_{\begin{array}{l} t\to s\\t < s\end{array}} F(t)$$
Related, you could have a look at what "càdlàg" (in french : continue à droite, limite à gauche) which means continuous on the right and limit on the left.


What is the cardinality of the subset of [0,1] consisting of infinite decimal expansions with only the digits 2 and 5?

Question: Wouldn't this be uncountably infinite because ie,
$$x_1=0.222225..., x_2=0.555555552..., x_3=0.5255...,$$
and if we keep going on...there can be $2^{\aleph_0}$ combinations. Then the cardinality would just be $\aleph_1$?
Comment: Only if you assume the continuum hypothesis.
Comment: Find a bijection between this set and $2^{\bf N}$.
Comment: what you say is basically correct, but I suggest you work on your wording of it before you submit your answer.
Comment: Is there a way I can prove it using Cantor's diagonalization method? I want to provide an answer that shows a proof of some sort
Answer: Same as the reals.
Map $2 \to 0$ and $5 \to 1$
and consider the result
as a binary number.
Comment: I can tell the set is uncountable because I can create a number based on Cantor's diagonalization method but how do i conclude by mapping what the cardinality is?


Can't add dll created by vb.net in vb 6 project

Question: I'm trying to add a dll created from vb.net and i want to import it in existing vb 6 project but it gives me error "can't add reference to the specified file." . Anyone knows how can i fix this issue?
Answer: There are the steps to follow:
1) Create a class library in VS choose whatever language you want either Vb.NET or C#
2) Goto to Project Properties->ConfigurationProperties->build, check the Register for COM Interop checkbox. This creates the COM wrapper for your .NET assembly and hence need not use regasm.
3) Write whatever code you want, expose the classes publicly, expose the properties, functions you want publicly.
Now in your VB6 project, add the claslibrary.tlb created by the above program.
Now use the classes and functions as you want.
Comment: I did the above, but my namespace and classes were not recognised. Any other tips?


Listview in kendo mobile is not binding to observable object in mvvm pattern

Question: I am using kendoui library to build a mobile application. I am having a problem in listing the data in listview. I am binding my listview to the observable itemViewModel   But it seems it is not binding well, it seems its a silly error in the code. This is the code.
javascript
var itemViewModel = new kendo.observable({
item: {
    name: " a name",
    price: 30
},
itemDataSource: new kendo.data.DataSource({
    data: [{
        name: "cover",
        price: 20
    }, {
        name: "charger",
        price: 20
    }, {
        name: "bag",
        price: 30
    }]
})
});

var app = new kendo.mobile.Application(document.body);

Html

For Sale

This is a link to the code.
If the code is fine.
My question : Is it enough to just instantiate the kendo.mobile.application in order to bind the view to the modelView or I want to do anything else?
Please I need some help.
Answer: The problem was the data-bind="itemViewModel" it should be data-model="itemViewModel" .

  ........
  ....the rest of my code
  ........


Fazer cópias de listas em determinadas posições com Python

Question: Olá, por favor, gostaria de fazer a cópia de uma determinada lista em python. O tamanho total da lista é 150 e desejo remover os 50 últimos elementos da lista. Aqui irei representar uma lista 100 vezes menor, ou seja com 15 elementos e que desejo remover os 5 últimos.
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Como poderia fazer isso em um lacço for? ou while?
Muito obrigado!
Comment: Eu postei uma resposta fiz de uma maneira pythonica, vc precisa que isso seja dentro de um laço ou não tem necessidade ?
Comment: (10 vezes menor )
Answer: Na realidade para copiares os ultimos 5 eles para outra lista:
l2 = l[-5:] # l2 = [11, 12, 13, 14, 15]

Para removeres os ultimos 5:
del l[-5:] # l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Answer: Em Python versão 2.7 é só fazer assim
l2= l[:-5]

Comment: yeah vc simplesmente diz quantos elementos quer remover do final da sua lista/vetor neste caso foi `-5 ` ou seja remover os últimos 5 elementos da lista, na sua pergunta se vc quer remover os últimos 50 é só colocar `-50`
Comment: Mas é claro sem problema algum, pode colocar variável vai funcionar perfeitamente, eu testei em python3 aqui e funcionou tbm :-)
Comment: humm sim tenta isso
`import math
b=50.1345
print(math.floor(b))`
Comment: Ai já não sei,  tem pouca informação,  fórmula uma pergunta com o máximo de informação possível que fica mais fácil te ajudar
Comment: Pega algum valor que tá dando problema no seu while pra entendemos  ...
Comment: mas deu certo hehe o problema era em `tamanho_populacao` =)
Comment: estou a utilizar o Python 3. Poderia explicar-me o que foi feito nessa linha sua? l2 = l[:-5]? eu poderia substituir por: l2 = l[:-taxa_c]?
Comment: entendi amigo, sobre o que perguntei ali de substiuir o -5 por uma variável é possível? É que meu algoritmo é variante então a taxa de quanto deve ser removido varia conforme o tamanho da lista.
Comment: legal, muito obrigado mesmo @ederwander. Aproveitar para te perguntar também, tentei encontrar aqui na comunidade, mas não achei a resposta. Se por exemplo tenho um valor float: `value = 50.1345` mas eu gostaria de pegar apenas a parte inteira dele, nesse caso o `50` apenas, Fazendo um cast aqui para `int(value)` funcionam algumas vezes, mas nem sempre. Isso faz com que meu `while` nem sempre funcione. Sabe como "truncar" no 50? Sem converter?
Comment: então, se fizer em um b como ali funciona, mas aqui na formula não funcionou =/ será que talvez seja por causa da forma que esteja sendo resolvida? Minha formula é algo como: `b = (tamanho_populacao * taxa)`
Comment: Vamos [continuar esta discussão no chat](http://chat.stackexchange.com/rooms/42602/discussion-between-allan-and-ederwander).
Comment: olá amigo, entra no chat ae que já explico =) http://chat.stackexchange.com/rooms/42602/discussion-between-allan-and-ederwander
Comment: e se eu precisasse pegar os 10 primeiros? Como ficaria dessa forma que você mostrou?
Answer: Encontrei uma solução, mas não sei se está correta:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

percent_l = 0.5

TAM = len(l)
taxa_c = int(percent_l * TAM)

print("taxa_c",taxa_c)

new_list = []

for i in l[0:taxa_c]:
    new_list.append(i)

print("new_list", new_list)

Nesse caso estou a calcular por meio de uma taxa, quantos últimos em porcentagem eu gostaria de remover.
Comment: que código enorme haha, da uma olhada no meu veja se funciona sua versão é 2.7?


Hold until pods are created java-client Kubernetes

Question: I create a custom object using java-client for Kubernetes API in the beforeAll method of the integration tests. After the custom object is created the pods get created too. However, it only works when I set Thread.sleep for a few seconds. Without it, the object is created and then all the tests executed. I also defined watch on custom object statuses, but it does not help either.
Is there any other way (except for Thread.sleep) to hold for a few seconds until the pods get created?
My code for the custom object creation:
<code>def createWatchCustomObjectsCalls() = {
    client.getHttpClient.setReadTimeout(0, TimeUnit.SECONDS)
    val watchCalls: Watch[V1Namespace]  = Watch.createWatch(client,
      apiInstance.listNamespacedCustomObjectCall(crdGroup, crdVersion,  crdNamespace, crdPlural, "true",  null, null, true,null,  null),
      new TypeToken[Watch.Response[V1Namespace]]{}.getType)
    watchCalls
  }

 override def beforeAll(): Unit = {
    val creationResourcePath = Source.getClass.getResource("/" + httpServerScriptName).getPath
    val serverStartupProcessBuilder = Seq("sh", creationResourcePath, "&") #> Console.out
    serverStartupProcessBuilder.run()

    val body = convertYamlToJson()
    val sparkAppCreation = apiInstance.createNamespacedCustomObject(crdGroup, crdVersion, crdNamespace, crdPlural, body,"true")
    println(sparkAppCreation)
}
</code>
Answer: You can synchronously check in a while loop if pods had been created:
<code>// while
val currentPodList = getCoreV1Api()
                    .listPodForAllNamespaces(null /* _continue */,
                                            null /* fieldSelector */,
                                            null /* includeUninitialized */,
                                            null /* labelSelector */,
                                            null /* limit */,
                                            "false" /* pretty */,
                                            null /* resourceVersion */,
                                            null /* timeoutSeconds */,
                                            false /* watch */)
                    .getItems();
// check items from currentPodList
// end while
</code>


Pegar somente um valor no resultado da consulta ao banco de dados

Question: Gostaria que ele passasse um return com apenas o numero de dados daquela tabela, porém ele esta retornando isso:

C:\E. S>node dao.js
Connected!
[ RowDataPacket { 'COUNT(*)': 4 } ]

Código:
contaQuantidade(tabela){
        var conector = this.conectaBanco();
        conector.connect(function(err) {
            if (err) throw err;
            console.log("Connected!");
            conector.query("select COUNT(*) from "+tabela, function (err, result) {
                if (err) throw err;
                var i = result;
            });
            conector.end();
        });
        return i;
    }

Como faço para pegar somente o numero 4?
Answer: Você pode usar o AS para criar um alias para COUNT(*), para ficar mais fácil de acessar no JavaScript. Então a sua query ficará assim:
SELECT COUNT(*) AS count FROM nome_da_tabela;

O resultado virá assim:
+-------+
| count |
+-------+
|     4 |
+-------+

Em seguida, para acessar o valor, precisamos acessar o primeiro item do RowDataPacket retornado. Para isso, usaremos a seguinte notação:
// Acessa o primeiro elemento dos resultados:
//    ↓↓↓
result[0]

E para acessar o número retornado, basta acessar a propriedade count, conforme definimos como alias em nossa query.
result[0].count

O código fica mais ou menos assim:
connection.query('SELECT COUNT(*) AS count FROM ' + table + ';', (err, result) => {
  if (err) throw err

  // ℹ Acessamos o número de tabela na linha abaixo:
  const number = result[0].count

  console.log('Temos ' + number + ' registros na tabela ' + table + '.')
})


Ableton Live: Using a USB microphone and Audio Kontrol 1 at the same time

Question: I'm using Ableton Live 11 Suite.  I have a USB microphone, which I can record with in Ableton.  I also have a Native Instruments Audio Kontrol 1 (henceforth AK1), which is an audio interface with various inputs and outputs.
The AK1 has a built in sound card which works really well with Ableton, so it's great to use it just for that.  I never have any audio inputs going into it but sometimes I plug midi devices into it.
Anyway, when I enable the AK1 in Ableton, I can no longer use the USB mic.  It seems Ableton expects all inputs to come via the AK1.  When Ableton starts it has seperate options for Input and Output, but they disappear once the AK1 is activated.
It might be the case that I need to have Driver Type set to MME/DirectX for the USB mic, and I need to have Driver Type set to Asio for the AK1, and so it's simply not possible to enable both at the same time.
Does anyone know a way I can set Ableton so that I can still use the AK1 as an interface but use the USB mic as an audio input?
Comment: It's not an Ableton problem, it's how digital audio and computer audio works. Sell the USB mic and buy a regular analog XLR-connected microphone to use with the NI interface. On the Mac you can create "aggregate" audio devices, which might work to some extent. On Windows, I don't know. Maybe you'll find some kind of a workaround kludge. Search for "aggregate" and "multiple audio interfaces". But really, just don't buy USB microphones. There are multiple questions about this issue on this site at well.
Comment: "USB microphones" are really audio interfaces. They contain: (1) microphone, (2) mic preamp, (3) A/D converter, (4) USB audio interface which talks with a USB host computer. Many "USB microphones" also contain an audio _output_, so you can connect headphones to the audio interface that's inside the "microphone". This is a bad idea. Don't buy.
Comment: Can other apps see the AK1 as MME/DirectX/WASAPI/WDM/whatever device? Or in more general terms, can apps that don't support ASIO see the AK1?
Comment: @piiperiReinstateMonica since your comments got upvoted I feel oblicated to inform you that you're more or less misinformed about how digital audio in general works. But it's always great to put down the newbie.
Comment: @piiperiReinstateMonica ok, let's start from the inability to use different devices at the same time. It's a problem with the poor design of Windows audio subsystem and the tradition of using ASIO drivers to work around it. ASIO itself is another poor design from the 90s that doesn't have any concept of multiple audio devices (an  ASIO driver itself can implement one but apps that use it won't know a thing). You're correct that Core Audio on MacOS can handle it quite smoothly and on Windows hacks like ASIO4All or FlexASIO can work around it
Comment: @piiperiReinstateMonica I don't really understand the concept of why having a headphone output in USB microphone is a bad idea. As you wrote, they are really all-in-one packages with audio interface and microphone, and it seems that all audio interfaces have an headphone output. Some of them sound quite bad. For quality, "audio interface" is not better than "usb microphone" by rule. Try a Behringer if you haven't already. Or the headphone output on Focusrite Scarletts.
Comment: @ojs The USB microphone, that's the bad idea. There will be multiple audio clocks in a system like that. Something somewhere has to negotiate clock drift between the devices. It's complicated, and not well supported by currently available software and hardware. Use a conventional mic and connect it to the audio interface's mic input.
Comment: @piiperiReinstateMonica I know that clock drift can be a theoretical problem, but is it an actual problem? The last time I saw noticeable clock drift was on a built in PC sound card more than ten years ago and even then it happened only with 44.1 kHz and 48kHz was in perfect sync. Anyway, the composite device on MacOS has option for adaptive clock syncing and I haven't noticed any audible artifacts from it. So it's doable, but Windows makes it difficult.
Comment: @ojs Ok, your issue seems to be about what is what in theory. Or finding someone to blame i.e. Microsoft? In theory everything can be solved, and in theory, Microsoft could do something to ease the situation. So could Steinberg, Ableton and many other DAW vendors. But the OP has an actual problem with an actual USB microphone he's trying to use in today's Windows. If you think that it can be made to work, the box below is waiting for your answer. My advice is to sell the USB mic and buy a regular mic and a cable.
Comment: @piiperiReinstateMonica my issue is smugly telling newbies who have a honest question that "It's not an Ableton problem, it's how digital audio and computer audio works" when it's really an Ableton and Windows problem.
Answer: The USB microphone contains an audio interface that's logically just like the NativeInstruments box, but it's just hidden inside the mic. The same thing applies to e.g. USB headphones, webcams etc. If it's connected with USB and it transfers audio to or from your computer, then it is a USB audio interface.
Audio applications such as DAWs are usually written with the assumption that one audio device handles all I/O, and particularly in Steinberg's ASIO driver "standard" this assumption is baked in the system. There are valid technical reasons for this restriction and assumption. Ableton allows you to have different devices for input and output, but not with ASIO. You'll see that if you switch to ASIO - you can only select one device in Ableton's settings. If you use Windows multimedia drivers or Mac, you can have different devices for input and output, and if they are independent devices with their own unsynchronized clocks, Ableton handles differences between clock speeds in some way. The handling of different speeds may create audible artifacts and add latency, and I think that's why Steinberg haven't wanted to support it in the ASIO system.
What comes to Ableton (and AFAIK all other DAWs as well), there is no facility for using multiple audio interfaces simultaneously. But it is possible to try and create virtual "aggregate" audio devices. On Apple Mac computers it is a built-in feature of the OS, but on Microsoft Windows you'll have to resort to tricks like the ASIO4All driver thingy. These solutions utilize some kinds of tricks in order to make the different devices look like one unified device from e.g. Ableton's point of view. There's probably some additional buffering and resampling/time-stretching going on behind the scenes. (which can add latency and maybe introduce wow/flutter artifacts in the audio)
Sweetwater has a guide article on how to build an aggregate audio device. I have not tried this on Windows.
https://www.sweetwater.com/sweetcare/articles/aggregate-audio-devices-and-drivers-for-pc-and-mac/
Quote from the article:

Aggregate devices don't always perform as well as when the interfaces are alone. You might experience digital clock source problems, dropouts, noise, or other playback-related issues. If you experience poor performance or connectivity issues, an aggregate device may not be ideal.

It may work with some combination of hardware and software, for some use. If you absolutely want to keep the USB microphone and Windows, you'll have to go and try if you can get it to work well enough. However, having to do that at all is optional. I recommend buying a regular microphone and connecting it to your regular audio interface's microphone input instead.
Related questions:
Recording two mics in garageband going out of sync
Aggregate audio interfaces and use outputs into analog mixer - possible?
Audio Interface and USB Mic
Comment: This is fairly correct, except for one detail: With ASIO you don't select a device but a driver. If a device manufacturer decides to do things like supporting multiple physical devices maybe synced to external clock, it happens inside the driver and the app isn't aware of that (except the external clock, it has a flag for some reason). This of course rules out interoperation between different brands.
Comment: Thanks.  Looks like I need to get an XLR mic instead.


Parse user not updating for just one specific user

Question: I was wondering if anyone has seen a case where Parse User table doesn't update for a specific user. I have a pretty simple code:
<code> PFUser.current()?["TorF"] = true

    PFUser.current()?.saveInBackground(block: { (success, error) in

        if error != nil {

            print(error)

        } else {

        }
    })
</code>
I have checks in other places of the app regarding whether there is a current PFUser, and my database shows that the user is logged in and PFUser.current() is correctly assigned to this user. The simple operation above works for all other users except for one specific user. Has anyone encountered something like this?
Comment: try to remove the app from the device and install it again
Answer: I found out that this was happening because the user had changed his password and therefore invalidated his access token. The place to check for whether the user's access token is valid is via Graph request.


Yet another "Why is this statement running slowly" inquiry

Question: I've been reading similar questions to mine on SO and googling for days and can't seem to find a solution that works for my situation. Please see the details below. When I run the sql statement below, it takes over a minute. It also is not using the index on tableA.col4. Why?
Like I said, I have been googling for quite some time but seem to be running in circles at this point. Any help is greatly appreciated!
---THE DETAILS--- (Sorry it is soooo long, but better to have all the info, I guess)
I have the following two tables, table A and B:
tableA: (5M rows...will eventually be 20M)
tableA | CREATE TABLE `tableA` (
  `col1` varchar(50) NOT NULL,
  `col2` int(10) NOT NULL DEFAULT '0',
  `col3` varchar(300) DEFAULT NULL,
  `col4` varchar(50) DEFAULT NULL,
  `col5` datetime DEFAULT NULL,
  PRIMARY KEY (`col1`,`col2`),
  KEY `col4` (`col4`),
  KEY `col5` (`col5`),
  KEY `col1` (`col1`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |

Indexes on tableA:
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tableA |          0 | PRIMARY    |            1 | col1         | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| tableA |          0 | PRIMARY    |            2 | col2         | A         |     4780162 |     NULL | NULL   |      | BTREE      |         |               |
| tableA |          1 | col4       |            1 | col4         | A         |         426 |     NULL | NULL   | YES  | BTREE      |         |               |
| tableA |          1 | col5       |            1 | col5         | A         |     2390081 |     NULL | NULL   | YES  | BTREE      |         |               |
| tableA |          1 | col1       |            1 | col1         | A         |     2390081 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

tableB: (1M rows...will eventually be around 5M)
| tableB | CREATE TABLE `tableB` (
  `col1` varchar(300) NOT NULL DEFAULT '',
  `col2` varchar(300) DEFAULT NULL,
  `col3` int(10) DEFAULT NULL,
  `col4` varchar(300) DEFAULT NULL,
  `col5` varchar(300) DEFAULT NULL,
  `col6` varchar(300) DEFAULT NULL,
  `col7` varchar(300) DEFAULT '0',
  `col8` varchar(300) DEFAULT '0',
  `col9` varchar(300) DEFAULT '0',
  `col10` varchar(300) DEFAULT NULL,
  `col11` varchar(300) DEFAULT NULL,
  `col12` mediumtext,
  `col13` decimal(15,2) DEFAULT NULL,
  `col14` decimal(15,2) DEFAULT NULL,
  `col15` varchar(300) DEFAULT NULL,
  `col16` varchar(300) DEFAULT NULL,
  `col17` decimal(15,2) DEFAULT NULL,
  `col18` decimal(15,2) DEFAULT NULL,
  `col19` varchar(300) DEFAULT NULL,
  `col20` varchar(300) DEFAULT NULL,
  `col21` decimal(15,2) DEFAULT NULL,
  `col22` decimal(15,2) DEFAULT NULL,
  `col23` varchar(300) DEFAULT NULL,
  PRIMARY KEY (`col1`),
  KEY `col3` (`col3`),
  KEY `col5` (`col5`),
  KEY `col2` (`col2`),
  KEY `col1` (`col1`),
  FULLTEXT KEY `col12` (`col12`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |

Indexes on tableA:
+----------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name      | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tableB |          0 | PRIMARY        |            1 | col1           | A         |      989765 |     NULL | NULL   |      | BTREE      |         |               |
| tableB |          1 | col3           |            1 | col3           | A         |        9799 |     NULL | NULL   | YES  | BTREE      |         |               |
| tableB |          1 | col5           |            1 | col5           | A         |       98976 |     NULL | NULL   | YES  | BTREE      |         |               |
| tableB |          1 | col2           |            1 | col2           | A         |      197953 |     NULL | NULL   | YES  | BTREE      |         |               |
| tableB |          1 | col1           |            1 | col1           | A         |      989765 |     NULL | NULL   |      | BTREE      |         |               |
| tableB |          1 | col12          |            1 | col12          | NULL      |           1 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+----------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

EXPLAIN:
EXPLAIN SELECT *
FROM tableA LEFT OUTER JOIN tableB ON tableA.col1 = tableB.col1
WHERE tableA.col4 NOT IN ('8/G','2','9/D','7','6/M') ORDER BY tableA.col5 DESC LIMIT 25

+----+-------------+----------+--------+-------------------+---------+---------+-----------------------+---------+-----------------------------+
| id | select_type | table    | type   | possible_keys     | key     | key_len | ref                   | rows    | Extra                       |
+----+-------------+----------+--------+-------------------+---------+---------+-----------------------+---------+-----------------------------+
|  1 | SIMPLE      | tableA   | ALL    | col4              | NULL    | NULL    | NULL                  | 4780162 | Using where; Using filesort |
|  1 | SIMPLE      | tableB   | eq_ref | PRIMARY, col1     | PRIMARY | 902     | mydb.tableA.col1      |       1 |                             |
+----+-------------+----------+--------+-------------------+---------+---------+-----------------------+---------+-----------------------------+

PROFILING:
Status  Time
starting    0.000012
Waiting for query cache lock    0.000003
checking query cache for query  0.000079
checking permissions    0.000004
checking permissions    0.000003
Opening tables  0.000013
System lock 0.000006
Waiting for query cache lock    0.000027
init    0.000028
optimizing  0.000008
statistics  0.000404
preparing   0.000009
executing   0.000003
Sorting result  2.135376
Sending data    0.004359
Waiting for query cache lock    0.000004
Sending data    0.004735
Waiting for query cache lock    0.000004
....
....
....
Sending data    0.008573
Waiting for query cache lock    0.000006
Sending data    0.005630
Waiting for query cache lock    0.000005
Sending data    0.005760
Waiting for query cache lock    0.000005
Sending data    0.005295
Waiting for query cache lock    0.000005
Sending data    0.005239
Waiting for query cache lock    0.000004
Sending data    0.004954
Waiting for query cache lock    0.000004
Sending data    82.246597
end 0.000012
query end   0.000004
closing tables  0.000009
freeing items   0.000014
logging slow query  0.000002
logging slow query  0.000003
cleaning up 0.000003

Answer:
It also is not using the index on tableA.col4. Why?

Well, the tableA.col4 NOT IN ('8/G','2','9/D','7','6/M') is the culprit.  MySQL really can't use an index to satisfy a NOT IN.
What is happening is MySQL is running through all ~5 million rows of tableA and building up a set of rows where tableA.col4 NOT IN ('8/G','2','9/D','7','6/M').  Then it is sorting that set in descending order by col5 then (hopefully) picking the top 25 rows out of that and then performing the join.
It is possible that a multi-column index on col5 and col4 (in that order) on tableA will help this query because I believe MySQL will work the index in reverse order and will stop after it gets 25 rows that satisfy tableA.col4 NOT IN ('8/G','2','9/D','7','6/M').
So my suggestion would be:
CREATE INDEX ix_tableA_col5_col4 ON tableA(col5,col4);

Then re-run your query.
Comment: Yes, your right about the reason it can't currently use the index - but I don't think it will use the index you suggest either
Comment: You may be right.  I wish MySQL would implement descending indexes.  It would help a lot of common queries.
Comment: Thank you for the answer and explanation of why it isn't using my index. I tried your multi-column index but the query doesn't use that either....it was a good suggestion.
Answer: Hmm. Just doing a quick shoot. The profiling output says that Sending data takes 82.246597, so  maybe you get lots of output which takes lots of time to transfer?
Comment: "Sending data" naturally sounds like it should refer to using network bandwidth, but it turns out it refers to the work of moving data around in memory within the mysqld process.  E.g. from the storage engine to the SQL layer.