how to detect wrong encoding

Question: This program write on 2 files.
In the right file the string is "IL RITROVO AL 1° PIANO"
In the wrong file the string is "IL RITROVO AL 1NUL PIANO".
In the second case, the "°" charater has wrong econding; how can I detect this case before I write it?
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class WrongWriter {
    static File wrongFile = new File("C:/Users/utente/Desktop/wrongFile.txt");
    static File rightFile = new File("C:/Users/utente/Desktop/rightFile.txt");

    public static void main(String[] args) throws IOException {

        byte[] wrongBytes = new byte[]{
                73, 76, 32, 82, 73, 84, 82, 79, 86, 79, 32, 65, 76, 32, 49, 0, 32, 80, 73, 65, 78, 79, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
                };

        write(wrongFile, wrongBytes) ;

        byte[] rightBytes = "IL RITROVO AL 1° PIANO".getBytes();

        write(rightFile, rightBytes) ;
    }

    static void write(File file, byte[] bytes) throws IOException{
        OutputStreamWriter stream = null; //10227
        stream =  new OutputStreamWriter( new FileOutputStream( file )  , "ISO-8859-15");
        stream.write( new String(  bytes ) );
        stream.flush();
        stream.close();

    }

}

Comment: Fist thing. Never use `String.getBytes()`. Always specify an encoding (such as utf-8). When decoding you can use a CharsetDecoder and specify the behavior when an unmappable character is encountered. http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html#newDecoder()  http://docs.oracle.com/javase/7/docs/api/java/nio/charset/CharsetDecoder.html#onUnmappableCharacter(java.nio.charset.CodingErrorAction)
Answer: String/char/Writer/Reader are Unicode text in java. (This makes java unique among other languages.) Java text can always contain any mix of scripts.
byte[]/InputStream/OutputStream are binary data in Java. To be interpreted as text they must be given their encoding.
So you can do:
OutputStreamWriter stream = null; //10227
stream =  new OutputStreamWriter( new FileOutputStream(file), "ISO-8859-15");
stream.print("IL RITROVO AL 1° PIANO");
stream.close();

The class OuputStreamWriter bridges this and writes the Unicode text into bytes having that enocoding.
In general the conversions are:
bytes[] inISO15 = "IL RITROVO AL 1° PIANO".getBytes("ISO-8859-15");
String s = new String(inISO15, "ISO-8859-15");

You took the OutputStream functionality in writing bytes, bypassing the conversion. This the should be done as:
stream.write(inISO5015);

But then better not use a Writer, but maybe immediately the FileOutputStream or a BufferedOutputStream.
Comment: My problem is "from a given byte array, how can I understand if this bytes are valid in a given encoding? " (in this case ISO-8856-1).  I solved with the  static public boolean isISO_8859_1  method, but II'd like view something that do the same thing in java core libraries
Comment: ISO-8859-1 is a single-byte encoding, indistinguishable from any other ISO-8859-... even if Greek. Windows-1252 is a superset of ISO-8859-1 (with comma-like quotes). UTF-8 would be recognizable with a high probability. I wrote one a language+encoding detector, using frequency lists of the most frequent words in any language.
Answer: Thanks, but this is what I wanted, but I'm not be able to do with CharsetDecoder....
package dummy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class WrongWriter {
    static File wrongFile = new File("C:/Users/utente/Desktop/wrongFile.txt");
    static File rightFile = new File("C:/Users/utente/Desktop/rightFile.txt");

    public static void main(String[] args) throws IOException {

        byte[] wrongBytes = new byte[]{
                73, 76, 32, 82, 73, 84, 82, 79, 86, 79, 32, 65, 76, 32, 49, 0, 32, 80, 73, 65, 78, 79, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
                };

        if (CharacterChecker.isISO_8859_1(wrongBytes)) {
            write(wrongFile, wrongBytes) ;
        } else{
            System.out.println("Bad input");
        }

        byte[] rightBytes = "IL RITROVO AL 1° PIANO".getBytes("ISO-8859-15");

        write(rightFile, rightBytes) ;
    }

    static void write(File file, byte[] bytes) throws IOException{
        OutputStreamWriter stream = null; //10227
        stream =  new OutputStreamWriter( new FileOutputStream( file )  , "ISO-8859-15");
        stream.write( new String(  bytes,  "ISO-8859-15" ) );
        stream.flush();
        stream.close();

    }

}
class CharacterChecker {

    static public boolean isISO_8859_1(byte[] bytes) throws UnsupportedEncodingException{

        for(int i=0;i< bytes.length;i++)
           {
               if(
                      ( bytes[i]<32 && bytes[i] >=0)
                      || (bytes[i]<-65 && bytes[i]>-69)
                      || bytes[i]==-72
                      || bytes[i]==-76
                      || bytes[i]==-88
                      || bytes[i]==-90
                      || bytes[i]==-92
                      ) {
                   return false;
               }

           }

        return true;
    }

    static public boolean isISO_8859_1(String s) throws UnsupportedEncodingException{
        byte[] bytes = s.getBytes("ISO-8859-1");

        return isISO_8859_1(bytes);
    }

    static public String replaceNotISO_8859_1_characters(String s, char chracter) throws UnsupportedEncodingException{
        String cString = Character.toString(chracter);
        byte sobs = cString.getBytes("ISO-8859-1")[0];

        byte[] bytes = s.getBytes("ISO-8859-1");

        for(int i=0;i< bytes.length;i++)
           {
               if(
                      ( bytes[i]<32 && bytes[i] >=0)
                      || (bytes[i]<-65 && bytes[i]>-69)
                      || bytes[i]==-72
                      || bytes[i]==-76
                      || bytes[i]==-88
                      || bytes[i]==-90
                      || bytes[i]==-92
                      ) {
                   bytes[i] = sobs;
               }

           }

        return new String(bytes,"ISO-8859-1");
    }

}

how to detect wrong encoding


Prove that $f$ is linear when$\frac1{t-s}\int_s^tf(x)\,dx=\frac{f(s)+f(t)}2$ for all reals $s < t$

Question:
Let $f:\mathbb R\to\mathbb R$ have a continuous second derivative. Suppose for all $s,t \in \mathbb R$ with $s<t$ we have $$\frac1{t-s}\int_s^tf(x)\,dx=\frac{f(s)+f(t)}2.$$ Show there exist $\alpha$ and $\beta$ such that $f(x)=\alpha x+\beta$.

I think I use second fundamental theorem of calculus or I use the mean value theorem of integrals. Another thing I thought is the right hand side is the midpoint between two functions; so I think the function has to have the midpoint of the two endpoints involved.
Comment: Have you tried applying any of these ideas?
Comment: Please provide a meaningful title next time your post a question. Requesting for help isn't providing good knowledge on your question!
Comment: @robjohn So what I have is by the FTC, for any $p$ which $s \leq p \leq t$, then  $\int_{s}^{x} f(p) dp  = f(x)$. Then, $f(x) = \frac{d}{dx} (t - s) \frac{f(s) + f(t)}{2}$. Am I on the right track?
Comment: This is the integral version of the well-known series result: $\frac{1}{n-m+1}\sum_{k=m}^n a_k = \frac{a_m + a_n}{2}$ when $a_k$ is an arithmetic progression (a linear function).
Comment: @stevieman The FTC should read $\int_s^x f'(p)dp=f(x)-f(s)$ or $\partial_x\int_s^x f(p)dp=f(x)$.
Comment: @robjohn I tried using FTC for both sides but I'm not sure if this is the way to go.
Comment: Your comments came after the complete answer that was given to this question. If you'd had these ideas, why not simply apply them? If you get stuck, then ask a question.
Comment: Next time: Please give more [context](http://meta.math.stackexchange.com/a/9960). Providing context not only assures that this is not simply copied from a homework assignment, but also allows answers to be better directed at where the problem lies and to be within the proper scope. Please [avoid "I have no clue" questions](https://math.meta.stackexchange.com/a/27933). Defining keywords and trying a simpler, similar problem often helps.
Answer: Since $\int_s^t fdx=(t-s)(f(t)+f(s))/2$, differentiating with respect to $t$ gives $f(t)=(f(t)+f(s))/2+(t-s)f'(t)/2$ so $f(t)-f(s)=(t-s)f'(t)$. Differentiating with respect to $s$ gives $-f'(s)=-f'(t)$, so $f'$ is constant.


Fender California Series Newporter guitar preamp built-in tuner

Question: I am a new player and I have an electro-acoustic Fender Newporter guitar. This guitar is from what they call their California series and they are all equipped with a built-in preamp with a tuner from Fishman. It looks like this:

I only play acoustically for now, I don't have an amp or anything. I have a regular tuner, but I was wondering if this on-board tuner can be used to tune the guitar when playing acoustically? I wasn't able to figure out how to turn it on. I guess it only comes on when the guitar is connected to an amp? Since I don't have one, I can't verify this.
Does anyone have a guitar from this series or know about them to be able to tell me whether the tuner can be used when the guitar is unplugged?
Answer: I've found the answer by myself. It's not very visible on the photo I've posted in the question, but there's two icons by the sides of the display. One is a fork icon and the other looks like an empty set icon. These are both physical buttons. They are kinda mushy and I first thought I was breaking the screen by pressing on them, but they are. Not sure what the latter does, but long-pressing the former turns the tuner on or off.
Comment: Scratch that, it is phase inversion. See page 6 ( https://www.fmicassets.com/Damroot/Original/10001/OM_leg_acougtr_Fender_Paramount_Owner's_Manual_2015.pdf )
Comment: When you say empty set you mean [ ] or something else. I can't tell from the image.
Comment: probably a low-battery icon.


How to request publish permissions using Facebook's Android SDK?

Question: Without using any UI/Button, how to ask for publish permissions for Facebook app using Facebook's Android SDK? I'm currently using this code but it registers the callback twice giving me duplicate results. What is the correct way to ask for publish permissions?
<code>    List<String> basicPermissions = Arrays.asList("user_friends", "user_games_activity");
    List<String> publishPermissions = Arrays.asList("publish_actions");

    mCallbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().logInWithReadPermissions(this, basicPermissions);
    LoginManager.getInstance().logInWithPublishPermissions(this, publishPermissions);

    LoginManager.getInstance().registerCallback(mCallbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResults) {

                    new GraphRequest(
                            AccessToken.getCurrentAccessToken(),
                            "/{my-app-id}/scores",
                            null,
                            HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    // my code
                                }
                            }
                    ).executeAsync();

                }

                @Override
                public void onCancel() {

                    Log.e("dd","facebook login canceled");

                }

                @Override
                public void onError(FacebookException e) {

                    Log.e("dd", "facebook login failed error");

                }
            });
</code>
Answer: According to FB, you shouldn't request the publish permissions until after the user has successfully returned from the read permissions request. So you'd have to change it to something like so:
<code>private boolean publishPermissionsRequested = false;
private LoginResult readResultSuccess;

private void loginWithFacebook(){
    LoginManager.getInstance().registerCallback(mCallbackManager,
        new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResults) {
                if(!publishPermissionsRequested){
                    publishPermissionsRequested = true;
                    readResultSuccess = loginResults;

                    LoginManager.getInstance()
                            .logInWithPublishPermissions(this, publishPermissions);

                    return;
               }

               onFacebookLoginSuccessful(loginResults);
            }

            @Override
            public void onCancel() {
                if(publishPermissionsRequested && readResultSuccess != null)
                    onFacebookLoginSuccessful(readResultSuccess);
            }

            @Override
            public void onError(FacebookException e) { {
                if(publishPermissionsRequested && readResultSuccess != null)
                    onFacebookLoginSuccessful(readResultSuccess);
            }
        });

    LoginManager.getInstance()
            .logInWithReadPermissions(this, basicPermissions);
}

private void onFacebookLoginSuccessful(LoginResult loginResults){
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/{my-app-id}/scores",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                 public void onCompleted(GraphResponse response) {
                      // Your code
                 }
            }
    ).executeAsync();
}
</code>
This way it will still continue as a success if they accept the read permissions but reject (or fail out of) the publish permissions request.
Hopefully that will point you in the right direction.


Display block content in current language

Question: I have multilingual Drupal 8 site.
I want to display views block in home page.
It should display block contents in current site language.
Example:

I have content type called "Book". Which has translated content in EN & NL.
Created views block for "Book" content type.
Placed this block in Home page.
Enabled language switcher block in Home page.

When I visit my home page mysite.com & mysite.com/nl above views block shows always English contents.
When I visit site in NL language, I want to display this block in NL language.
Comment: Just for clarification, you surely have seen, that on your view's edit screen under "Advanced" in the very right corner there's a language option? Which can be set to "current language"? Or something similar?
Answer: Added language condition in Filter Criteria to fix it.


In swift 4, uicollectionView can I set minimumLineSpacing for only particular indexPath?

Question:
In the picture I have made a UICollectionView like the top one.
But I want to make it like the bottom collectionView.
For particular indexPath I want more space between cells
I googled but I think there is no way to do that with minimumLineSpacingForSectionAt.
Is there a better way to do this?
Answer: This method "minimumLineSpacingForSectionAt" is used to give spacing between section not between cells. To achieve the above thing, you can return empty CollectionView cell or you can also return different size for this particular cell.
Answer: There is a better way to achieve this i.e. by using UICollectionViewFlowLayout. You need to subclass UICollectionViewFlowLayout, customise it and then assign this to you collection view.


Geometric Meaning behind the algorithm (slope of the line + ray casting)

Question: I'm trying to dissect the classic algorithm for finding if a point is inside a (simple) polygon.
Please see:
http://erich.realtimerendering.com/ptinpoly/
and
https://stackoverflow.com/questions/11716268/point-in-polygon-algorithm
I got the algorithm to work and got to know where the left hand side of the comparison came from. I also understood why the need of having the Y coordinates to show different signs.
That is, that:
(verty[i]>testy) != (verty[j]>testy)) &&
     (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )

But I still can't add a geometric meaning to it. After toying with the equation I realized that the "ray casting" logic test came from the following inequation:
verty[j] - vert[i] / vertx[j] - verty[i] < testy - verty[i] / testx - vertx[i]

where the authors solved for testx (where testx is the point to be tested if it is contained inside the polygon).
from the last equation if see that it is the coefficient m from the slope line equation being compared in both sides. but why test both equations to see if one is less than the other?
Besides that when we solve for testx where is the "ray casting" occuring? and why at just one side of the inequation?
Answer: What's happening is a ray is being cast to the right (in the positive $x$ direction) and we're counting edge crossings.  The first condition is a bit of a cryptic way to say "if the $y$-coordinate of starting and ending points are both above or below the test $y$-coordinate then we definitely don't cross an edge".  The next condition is calculating the $x$-coordinate of the intersection of the horizontal line going through testy and the edge, i.e. the line joining the start and end vertices.  It then checks if testx is less than that $x$-coordinate. If so, that means we're going to cross an edge going from left to right.
Comment: I'm really not being able to visualize "the x-coordinate of the intersection of the horizontal line going thru $testy$ and the edge". How can we be sure that it is a straight line (no slope) since with started with a comparison between the slope coefficients...
Comment: The "horizontal line" is the "ray" we cast from (`testx`,`testy`).  We cast it out straight in the positive $x$ direction.  We know it's horizontal because we chose it to be.
Comment: where in the equation can we see that? :_(
Comment: Write out the formula to intersect two lines with arbitrary slopes and arbitrary $y$-intercepts, in particular, the formula to calculate the $x$-coordinate of the intersection.  Once you do that, set the slope of one of the lines to $0$ and look at the formula that results.


clustered-stratified random sampling

Question: i've got questions about clustered-stratified random sampling.
let's say I want to do a research in narcotics.
the population that I will be working with is a combination of academics, students, and government employees (law enforcers, DEA, etc). I consider the population's unknown because I couldn't get the exact number of the population.
from that population, I'm thinking of clustering the population into two groups, those from academics and the practitioners (the government employees). From that clusters, I developed two levels,
the academic cluster contains lecturer group and students group.
the practitioners group contains high-level group and low-level group.
Have I already clustered and stratified my sample in right manner?
How do I decide the exact sample for the cluster without knowing specific population of each group and without knowing total population?
and how do I decide the exact sample for each level of the cluster?
thank you very much, I appreciate your time and suggestions :)
Answer: What you described is stratification: you know before you sample that a given unit is a professor, or a student, or a law enforcement officer. If you know something about an observation unit beforehand, that's typically is (or can be) a stratification variable. Now, the clusters would be units you would sample together for logistics reasons: you don't have a full list of students in your country, but you have a full list of universities, and you can sample may be 20 of these, and try to reach students or professors in these universities somehow (that's difficult, but I will leave these difficulties to you). Now, university is then a cluster. Within that cluster, you stratify your potential respondents into professors and students, and take samples of these independently. So you have multiple complex sample features:

You stratified your population into academic vs. government
You took a cluster sample of universities in the academic stratum
You stratified each cluster into professors and students
You took samples of professors and students from their respective (within-cluster) strata
You stratified the government sample by agency (say you have police to deal with crimes, and education people to deal with universities)
You can further take a cluster sample of police departments within the enforcement agency
Finally, you get down to policemen by sampling within the cluster (police department)

There will be tricks along the way -- you would want to sample proportional to size when taking clusters, for the reasons you can find in any good sampling book.

clustered-stratified random sampling


C++ Function signature that returns a PPL task?

Question: I am a total noob when it comes to using PPL tasks within the C++ environment, so I am having a hard time to figure out what would be the C++ syntax of the following C# code:
private static async Task<RandomAccessStreamReference> GetImageStreamRef()
{
    return RandomAccessStreamReference.CreateFromStream(await GetImageStream());
}

private static async Task<IRandomAccessStream> GetImageStream()
{
    var stream = new InMemoryRandomAccessStream();
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, width, height, 96, 96, imageBytes);
    await encoder.FlushAsync();
    return stream;
}

This C# code was taken from the Windows Store reversi Microsoft sample code. The best I could get so far is this:
Concurrency::task<IRandomAccessStream^> GetImageStream()
{
    auto stream = ref new InMemoryRandomAccessStream();
    task<BitmapEncoder^>(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, Stream)).then([this, stream, width, height, imageBytes](BitmapEncoder^ encoder)
    {
        encoder->SetPixelData(BitmapPixelFormat::Rgba8, BitmapAlphaMode::Ignore, width, height, 96.0, 96.0, imageBytes);
        return encoder->FlushAsync();
    }).then([this, stream]()
    {
        return stream; //Does this even make sense?
    });
    //return stream; //Not sure if I should have this here?
}

But it generates the following compile error:
error C4716: 'GetImageStream' : must return a value

I understand why this error happens, but I have no clue how I can have a function that returns a task without having a return value at two different locations? I haven't even tackled GetImageStream yet.
I am not even sure I took the right path into this...
Thank you!
Comment: Are you compiling the above code as C++/CLI code or plain C++ code? It's not going to work as plain C++ code as the caret signatures for garbage collected reference types are a C++/CLI extension.
Comment: @TimoGeusch: it's not C++/CLI, it's C++/CX: http://programmers.stackexchange.com/questions/162168/what-are-c-cx-and-c-cli-and-how-do-they-relate-to-c-and-winrt
Comment: @TimoGeusch Yes I am compiling as C++/CX. Thanks.
Answer: You're real close.  A key point you might be missing is that then returns you a new task.  So the last then in the chain determines the type of your task.
auto t = task<int>([] { return 0; });
// t is task<int>

auto t = task<int>([] { return 0; })
.then([](int i) { return 3.14; });
// t is task<double>

auto t = task<int>([] { return 0; })
.then([](int i) { return 3.14; })
.then([](double d) { return "foo"; });
// t is task<const char*>

If you just glance at the first line, it looks like you've always got a task<int>, but as you can see that's not necessarily the case if you immediately call then on it.
Secondly, keep in mind that your function is returning a task, not the stream itself.
Usually you'd have the last then in your chain return you the task that you'll return from your function, and rather than store the task in a local variable, you just return it.  For example:
task<const char*>
get_foo()
{
    return task<int>([] { return 0; })
    .then([](int i) { return 3.14; })
    .then([](double d) { return "foo"; });
}

Again, it looks a tad strange because a quick glance makes you think that you're returning a task<int>.  This is nicely handled by using create_task rather than calling the task constructor explicitly.  It frees you from having to explicitly specify the task's type at all.  Additionally, it's easily changed to create_async if you instead want to return an IAsyncInfo derivative.
I'm not at all familiar with BitmapEncoder, but here's a tweaked version of your code that might do the trick:
Concurrency::task<IRandomAccessStream^> GetImageStream()
{
    auto stream = ref new InMemoryRandomAccessStream();
    return create_task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream))
    .then([this, width, height, imageBytes](BitmapEncoder^ encoder)
    {
        // are width, height, and imageBytes member variables?
        // if so, you should only need to capture them OR "this", not both
        encoder->SetPixelData(BitmapPixelFormat::Rgba8, BitmapAlphaMode::Ignore, width, height, 96.0, 96.0, imageBytes);
        return encoder->FlushAsync();
    }).then([stream]()
    {
        // this should work fine since "stream" is a ref-counted variable
        // this lambda will keep a reference alive until it uses it
        return stream;
    });
}

The only real change is using create_task and immediately returning its result.
I'm still learning PPL myself, but one thing I've learned that's held up so far is that you should pretty much always be doing something with any task you create.  The usual thing to do is use then to turn it into a new/different task, but you still need to do something with the task returned by the last then.  Oftentimes you just return it, as above.  Sometimes you'll add it to a container of tasks which are then grouped together with when_all or when_any.  Sometimes you'll just call get() on it yourself to wait for its result.  But the point is that if you create a task and don't do anything with it, then there's probably something wrong.
Comment: Thanks, your answer was extremely insightful! In fact, I was able to make it all work now! :)
Comment: One last detail, the function GetImageStream should return a InMemoryRandomAccessStream^ instead of a IRandomAccessStream^ otherwise you get a compile error.
Comment: Great, I'm glad you got it working!  One minor point on your answer (which I can't seem to comment on directly): you only get the exception you mention from `.get()` when you call it **on the UI thread**.  This is WinRT's way of not letting you stall the UI to wait for a long-running operation.  If you're in a background thread, though, you can call `.get()` as necessary.
Comment: Another minor comment on the code in your answer: you may want to use `catch (...)` to catch all exceptions, not just those that derive from `std::exception`.  You can re-throw the same exception from such a catch block by just using `throw;` with no argument.
Answer: Also if anyone cares, here is how to implement the GetImageStreamRef mentioned above, in C++ version:
task<RandomAccessStreamReference^> GetImageStreamRef()
{
    return GetImageStream().then([](IRandomAccessStream^ pStream)
    {
        return RandomAccessStreamReference::CreateFromStream(pStream);
    });
}

Also, make sure to NEVER use .get() on any of those tasks, otherwise you will get an exception thrown saying "Illegal to wait on a task in a Windows Runtime STA". The proper way to get the value of this image stream reference is, for example on a DataRequestHandler that sets a bitmap data on the DataRequest:
void OnBitmapRequested(DataProviderRequest^ request)
{
    auto Deferral = request->GetDeferral();
    GetImageStreamRef().then([Deferral, request](RandomAccessStreamReference^ pStreamRef)
    {
        try
        {
            request->SetData(pStreamRef);
            Deferral->Complete();
        }
        catch( std::exception ex )
        {
            Deferral->Complete();
            throw ex;   //Propagate the exception up again
        }
    });
}


Draw pixel trace by moving an object

Question: I'm working in Python using Pygame and I'm wondering how to draw pixels depending on moving some object with cursor keys.
So, what I want to do is to draw a "trace of movement", for example when the player is moving his object rightward, the program must draw pixels behind this object from the start of movement to the end of movement.
Comment: Do you have any code to show us?
Comment: not exactly... object is moving with cursor keys. typical movement with event keys (event.key).

i just need some example to draw pixels behind that moving object.
Comment: if the object is moving by cursor key input surely you have *some* code?
Answer: The easiest way is with surface.set_at():
surface.set_at((x, y), color)

The x and y is the position of your moving object. Color is a RGB three element tuple like (100,0,200).
Answer: You can keep a track of the past positions of the object..
In the code below, a pygame.Rect is being moved around with arrow keys.
The parts you'll be interested in are in the Player class, namely

lasts (list)

This list keeps a record of the previous positions of the player.

update_list() (function)

This function manages the list,

Adding the current position into the list.
Removing the first element, once the list is as long as fade.

draw()(function)

Actually only the for loop here, it uses indexes and draws the fading Rects.

Note: This is not the most efficient code.

The Code
import pygame

bg = (255, 255, 255)

def add_colors(color1, color2):
    retval = []
    for i in range(3):
        c1 = color1[i]
        c2 = color2[i]
        c = c1+c2
        if c > 255: # just basic handling
            c -= 255
        retval.append(round(int(c)))
    return tuple(retval)

def calc_colors(color, fade):
    retval = []

    # calculating the color change rate
    diff_color = []
    for i in range(3):
        diff = bg[i] - color[i]
        c_diff = diff/float(fade)
        diff_color.append(c_diff)
    diff_color = tuple(diff_color)

    # get the fading colors
    lastcolor = bg
    for i in range(fade):
        new_color = add_colors(lastcolor, diff_color)
        retval.append(new_color)
        lastcolor = new_color

    return retval[::-1]

class Player:
    def __init__(self, color, fade, pygame_Rect_args):
        """Give arguments as given to pygame.Rect"""
        self.rect = pygame.Rect(*pygame_Rect_args)
        self.size = self.rect.size
        self.vx = 0.0
        self.vy = 0.0
        self.lasts = []

        self.maincolor = color
        self.colors = calc_colors(color,fade)
        self.fade = fade

    def draw(self):
        for i in range(len(self.lasts)):
            pos = self.lasts[i]
            c_color = self.colors[i]
            pygame.draw.rect(screen, c_color, pygame.Rect(pos, self.size))
        pygame.draw.rect(screen, self.maincolor, self.rect)

    def update_list(self):
        self.lasts.append(self.rect.topleft)
        if len(self.lasts) > self.fade:
            self.lasts.remove(self.lasts[0])

    def update(self):
        self.rect.x += int(round(self.vx))
        self.rect.y += int(round(self.vy))

    def handle_keys(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_UP]:    self.vy -= 0.1
        if key[pygame.K_DOWN]:  self.vy += 0.1
        if key[pygame.K_LEFT]:  self.vx -= 0.1
        if key[pygame.K_RIGHT]: self.vx += 0.1

player = Player((255,255,0), 10, (10,10,10,10))

pygame.init()
screen = pygame.display.set_mode((640,480))
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            break

    if not running:
        break

    player.handle_keys()
    player.update_list()
    player.update()

    screen.fill(bg)
    player.draw()
    pygame.display.flip()

    clock.tick(40)

You may also want to do this tutorial (not the entire thing, but just the last part (#9))

Draw pixel trace by moving an object


Can I disable Subversion's locking features?

Question: My team has no reason to use the feature, but some of the GUI tools place the revert and lock button very close together and we end up with accidental locks.
We are running the distro svn package, v1.6, on CentOS 5.x and 6.x.
http://svnbook.red-bean.com/en/1.7/svn-book.html#svn.advanced.locking
Answer: No. You can't disable locking in Subversion, but you can at least prevent chaos.
Quote from your link

In particular, the pre-lock and pre-unlock hooks allow administrators to decide when lock creation and lock releases are allowed to happen.

i.e you can disable any creation of locks in hook.
In worse case locks can be removed (by cron?) in repository regularly (svnadmin lslocks|rmlocks)


Como coletar tweets através da API Streaming, restringindo um local geográfico?

Question: Eu gostaria de coletar tweets através de determinada palavra chave em tempo real, entretanto gostaria de tweets somente postados no brasil, pois ao fazer a coleta a api me retorna tweets do mundo inteiro. Estou utilizando a biblioteca twitter4j. No código abaixo estou coletando todos os tweets referente as palavras chave "carro".
package ToCollect;
import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;

public class Main {
    public static void main(String[] args) {
        Connect.join();
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey("X");
        cb.setOAuthConsumerSecret("X");
        cb.setOAuthAccessToken("X");
        cb.setOAuthAccessTokenSecret("X");

        /*
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        try {
            twitter.updateStatus("It's Free!");
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        */
        TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

        StatusListener listener = new StatusListener() {

            @Override
            public void onException(Exception arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDeletionNotice(StatusDeletionNotice arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScrubGeo(long arg0, long arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatus(Status status) {
                User user = status.getUser();

                // gets Username
                String username = status.getUser().getScreenName();
                System.out.println(username);
                String profileLocation = user.getLocation();
                System.out.println(profileLocation);
                long tweetId = status.getUser().getId();
                System.out.println(tweetId);
                String content = status.getText();
                System.out.println(content +"\n");
                if (content.contains("RT @") == false)
                Connect.insere(username, tweetId, content=Replace.trata(content));

            }

            @Override
            public void onTrackLimitationNotice(int arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStallWarning(StallWarning arg0) {
                // TODO Auto-generated method stub

            }

        };
        FilterQuery fq = new FilterQuery();

       String keywords[] = {"chuteira da nike"};
       fq.track(keywords);

        twitterStream.addListener(listener);
        twitterStream.filter(fq);

    }
}

Answer: A Streaming API aceita o parâmetro language, que pode ser atribuído com "pt" ou "pt-BR". Na biblioteca twitter4j ele está presente na classe FilterQuery, então você tem que alterar seu código para:
String keywords[] = {"chuteira da nike"};
fq.track(keywords);

twitterStream.addListener(listener);
twitterStream.filter(fq);
twitterStream.language("pt-BR");

Não recomendo utilizar o parâmetro locations pois além dele depender de tweets geolocalizados, que não são tão frequentes, ele não restringe o casamento das keywords que você passa no parâmetro track, ou seja, tweets que possuem a palavra "carro" e que não estão dentro da location box que você passou, também serão retornados. E Isso não acontece com o parâmetro language.
Comment: Humm, entendo, muito obrigado Luis Cipriani! Abraços


Error : std::length_error at memory location

Question: When I call transpose function I got this error: Error :
std::length_error at memory location

I do not know exactly what could cause the error
//class Matrix.h
class Matrix
{
private:
    std::vector<std::vector<double>> content;
    size_t numberOfRows;
    size_t numberOfColumns;
    double determinant;
};

inline Matrix::Matrix(size_t nRows, size_t nColumns)
    : content(nRows,std::vector<double>(nColumns)),
      numberOfRows(nRows), numberOfColumns(nColumns) {}

const double getValue(size_t i, size_t j) const {
    return (i >= numberOfRows) || (j >= numberOfColumns) ? NULL : content[i][j];
}

void setValue(size_t i, size_t j, double value) {
    (i >= numberOfRows) || (j >= numberOfColumns) ? content[i][j] = NULL : content[i][j] = value;
    determinant = NULL;
}

Matrix Matrix::transpose()
{
    Matrix result(numberOfColumns, numberOfRows);
    for (size_t i = 0; i < numberOfRows; i++)
        for (size_t j = 0; j < numberOfColumns; j++)
            result.setValue(j, i, getValue(i, j));
    return result; /// Error here
}

Comment: (I know that there are better ways to implement this method, but i was trying to 'translate' code from java to c++)
Comment: Related: https://stackoverflow.com/a/4643721/4944425 .Also NULL is used in C for pointers, while you are initializing `double`s, so you should use 0.0 instead (or a completely different logic).
Comment: https://stackoverflow.com/questions/3350385/how-to-return-an-object-in-c
Comment: I totally agree with the [accepted answer](https://stackoverflow.com/a/3350395/4944425) of that question. Have you read it?
Comment: Possible duplicate of [C++ Returning reference to local variable](https://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable)
Answer: The solution is return the object as a pointer or pass a variable as reference, here is the correct example:
Matrix* Matrix::transpose()
{
    Matrix *result=new Matrix(numberOfColumns, numberOfRows);
    for (size_t i = 0; i < numberOfRows; i++)
        for (size_t j = 0; j < numberOfColumns; j++)
            result.setValue(j, i, getValue(i, j));
    return result;
}

Thank you


Two relatively prime polynomials

Question: show that two polynomials f,g in K[x] are relatively prime if and only if 1 is in I(f,g). I(f,g) being the linear combinations of the two polynomials. I dont know how to start this since shouldnt 1 have to be in there in order for them to be relatively prime?
Comment: What is K[x]? Is it polynomials with integer coefficients?
Comment: What is K? If $K=\Bbb Z/4\Bbb Z$,then for $f=2$ and $g=x$ the assertion is not true.
Answer: This result is called Bézout's identity for polynomials and it's proof is based on Euclidean algorithm.
Comment: For that $K[X]$ must be an Euclidean Domain. right?
Comment: Yes since  we should do an euclidean division.
Comment: So, what are the minimum conditions on K so that $K[X]$ is an ED.

Two relatively prime polynomials


add "items_per_page" number in title tag and meta tag

Question: I created a view which has set up "Exposed items per page options." I used "24,48,72" as my options. Here is my view "Exposed setting":
When I choose "items per page" as 72, the url of that page will change to "https://www.domain.com?items_per_page=72". Here is a test page screenshot:

My question is how can I add the number to "title" tag in order to distinguish "title" for SEO. There are some websites suggest to used [current-page:page-number] token, but this is not working in my case.  Does anyone have any idea?
Answer: I got a solution. Just add the following code in template.php file.
function yourtheme_preprocess_html(&$vars){
    // add pager number to title.
    parse_str($_SERVER['QUERY_STRING']);
    if(isset($items_per_page)){
        $vars['head_title'] .= $items_per_page;
    }
}


Email layout. Changing tr order in responsive table

Question: I'm creating a layout for a newsletter. Usually this has to be pretty static but I'm using the Mailchimp guideline to achieve some responsiveness. It works well but the problem is that I would like the second image to come before the text (the problem is very obvious in the example link, it's hard to explain). Basically I would like to achieve something like you could do with Bootstrap using push and pull for example where an image that is placed second becomes first when resizing.
So image and then text when the layout is a single column.
Example: https://codepen.io/SergiOca/pen/vmqMoZ?editors=1010
 @media only screen and (max-width: 480px){
        #templateColumns{
            width:100% !important;
        }
    .templateColumnContainer{
        display:block !important;
        width:100% !important;
    }

    .columnImage{
        height:auto !important;
        max-width:480px !important;
        width:100% !important;
    }

    .leftColumnContent{
        font-size:16px !important;
        line-height:125% !important;
    }

    .rightColumnContent{
        font-size:16px !important;
        line-height:125% !important;
    }
}

 Selección de profesionales en plantilla o freelance.

 A partir de un Job description acordado con el cliente, procedemos a entrevistar y a validar técnicamente a los candidatos que cumplan con los requisitos técnicos y personales, previamente establecidos.

 Selección de profesionales en plantilla o freelance.

 A partir de un Job description acordado con el cliente, procedemos a entrevistar y a validar técnicamente a los candidatos que cumplan con los requisitos técnicos y personales, previamente establecidos.

 Selección de profesionales en plantilla o freelance.

 A partir de un Job description acordado con el cliente, procedemos a entrevistar y a validar técnicamente a los candidatos que cumplan con los requisitos técnicos y personales, previamente establecidos.

Comment: Is it every second image? Do you have the liberty of using nth-child?
Comment: No, it's just the second image. It's very clear in the example. Basically I want the mobile, resized row to be, image-text, image-text, image-text. I could use anything.
Answer: What you need my son is direction in life.
You can apply the css property direction to swap the order of the table cells. Direction can be applied to text or inline-block elements.
You can apply it different ways.
<row dir="rtl">

.main-content {direction: rtl;  /* Right to Left */}

This is a sample of code how the row or td could be applied:

<container class="header">
  <row dir="rtl">
    <columns large="6" small="12" class="text-right" dir="rtl">

<a href="<%@ include view='MirrorPageUrl' %>" target="_blank">View this email on the web

    </columns>
    <columns large="6" small="12" dir="rtl">

    </columns>
  </row>
</container>

You can try  rtl ot ltr inherit from parent elements.
I would suggest placing direction on a class for the tables in a @media query so that you can swap them in mobile view. Something like this:
@media only screen and (max-width: 600px) {
  .leftColumnContent {direction: rtl}
}

For more information on direction, check out:
https://css-tricks.com/almanac/properties/d/direction/
Good luck.
Comment: Doesn't really change the order, can you apply that to my exmaple in any way to change the layout order after the 480px media query? I just see text moving from left to right but I don't think it can change the layout order?
Comment: Apply it to the table `` or the ``. I am doing this for responsive emails using Zurb Foundation for Emails 2.
Comment: I just manage for the text to move from a side to the other like a float of sorts. It does not change the layout order.
Comment: Direction will help. Put direction RTL on parent container (div or TD) then direction ltr on the siblings. You can swap the direction around if it doesn't work the right u want it stacked.
Comment: Please dont try to style emails with DIV's. Its a recipie for broken emails.
Comment: Yes, the `dir` attribute is what you want. I included an answer that uses your code and should work with your existing media query.
Answer: If you have the liberty of using nth-child then you can use
.templateColumns:nth-child(2n) > tbody > tr > td:nth-child(1){
   float: right;
}
.templateColumns:nth-child(2n) > tbody > tr > td:nth-child(2){
   float: left;
}

but you have to change the markup too, for push and pull to work you need to have the same type of content inside left column and right column.
I have added class .templateColumns to the table
https://codepen.io/srajagop/pen/NjZVpo?editors=1100
Note: nth-child is not supported by all the email readers, gmail doesnt support it
Comment: The problem remains after the 480px query. The layout order remains the same and the initial layout is now gone.
Comment: Updated the answer. With some markup change. see if it helps
Comment: That does the trick but it's a pity it won't work in Gmail. I'll accept it in a bit.
Answer: You can use the HTML dir attribute to manipulate the layout despite what the source order of each column is.
If you layout the email how you want it to appear on mobile, you can then then change the desktop layout using the dir attribute. Using your code, here's an example of a two-column layout where the image will stack above the text on mobile, but appear in the right column on desktop:

 Selección de profesionales en plantilla o freelance.

 A partir de un Job description acordado con el cliente, procedemos a entrevistar y a validar técnicamente a los candidatos que cumplan con los requisitos técnicos y personales, previamente establecidos.

Using your existing media query to stack the columns should work wth this code.
Answer: This is what you are after. I have taken one table of yours and given an example of how column swapping can be done. You will need to keep both the elements together for it to work.
The method used here is called hybrid. Both columns are created using div's which is encased in a td

This td has direction which will tells everytrhing inside the div to be right to left (direction:rtl)
Next each columns are created using div's which are 50% width of the container td.

These div's have a max width of 280px, width of 100% and direction set to left to right (direction:ltr)
Everything inside the div can be coded as 100% width table.
I have added what people call ghost columns. This is Outlook conditional statement that tells outlook that this is a column.

 @media only screen and (max-width: 480px){
        #templateColumns{
            width:100% !important;
        }
    .templateColumnContainer{
        display:block !important;
        width:100% !important;
    }

    .columnImage{
        height:auto !important;
        max-width:480px !important;
        width:100% !important;
    }

    .leftColumnContent{
        font-size:16px !important;
        line-height:125% !important;
    }

    .rightColumnContent{
        font-size:16px !important;
        line-height:125% !important;
    }
    .hundred{width:100% !important;}
}

 Selección de profesionales en plantilla o freelance.

 A partir de un Job description acordado con el cliente, procedemos a entrevistar y a validar técnicamente a los candidatos que cumplan con los requisitos técnicos y personales, previamente establecidos.

For the second row of code you can swap the divs around to get the desired result.
Let me know if any of this didnt make sense or you want me to explain further.
Cheers


Clarifications regarding reading a nomogram

Question: Following is a nomogram created from mtcars dataset with rms package for the formula:
mpg ~ wt + am + qsec

The model itself seems good with R2 of 0.85 and P<0.00001
> mod

Linear Regression Model

ols(formula = mpg ~ wt + am + qsec, data = mtcars)

                Model Likelihood     Discrimination
                   Ratio Test           Indexes
Obs       32    LR chi2     60.64    R2       0.850
sigma 2.4588    d.f.            3    R2 adj   0.834
d.f.      28    Pr(> chi2) 0.0000    g        6.456

Residuals

    Min      1Q  Median      3Q     Max
-3.4811 -1.5555 -0.7257  1.4110  4.6610

          Coef    S.E.   t     Pr(>|t|)
Intercept  9.6178 6.9596  1.38 0.1779
wt        -3.9165 0.7112 -5.51 <0.0001
am         2.9358 1.4109  2.08 0.0467
qsec       1.2259 0.2887  4.25 0.0002

I am not clear what are these 'Points', 'Total Points' and 'Linear Predictor'. Which one of these represent mpg, the outcome variable? I will appreciate any explanation.
Edit: considering excellent suggestions by @Glen_b for easy reading of points etc, following could be an alternative nomogram:

Since outcome or response variable is available, it can be used instead of the term 'Linear Predictor'. It also become self-explanatory as to how the nomogram needs to be read.
Comment: nice diagram in edit there
Answer: Well, since your model is linear, with the expected mpg equal to the linear predictor, you can read mpg straight off the linear predictor scale.
For each variable, you find its value on the relevant scale. For example, imagine we wanted to find a predicted mpg for a car with wt=4, am=1, qsec=18:

which gives a predicted mpg of about 18.94. Substituting into the equation gives 18.95, so that's pretty close. (In practice you would probably only work to the nearest whole point -- and so get about 2 figure accuracy - "19 mpg" - out, rather than 3-4 figures as here.)
One of the chief benefits of such a diagram to my mind is that you instantly see the relative effect of changes in the different predictor variables (IV) on the response (DV). Even when you don't need the diagram for any calculations, it can have great value in terms of simply displaying the relative effects of the variables.

Followup question from comments:

Does it work the same way for non-linear or polynomial regressions?

For cases where $E(Y)$ is nonlinear in some predictors, some minor - and perhaps obvious - modifications are needed. Imagine that we have $\hat{y} = b_0+b x_1+f(x_2)$
where either:
(a) $f$ is monotonic; or
(b) $f$ is not monotonic
In either case, the scale for $x_1$ would work exactly as above, but in case:
(a) the scale for $x_2$ won't be linear; e.g. if $f$ is monotonic decreasing but (roughly) quadratic, you might have something like this:

(b) the non-monotonic scale for $x_2$ will "break" at a turning point and flip over. e.g.

-- here the function $f(x)$ has a minimum somewhere around $x=2.23$
It's possible for such functions to have several turning points, where scales would break and flip over multiple times - but the axis line only has two sides.
With points-type nomograms this presents no difficulty, since one may move additional scale-sections up or down (or more generally, orthogonally to the direction of the axis) a little until no overlap occurs.
(More than one turning point can be a problem for alignment-type nomograms; one solution shown in Harrell's book is to offset all the scales slightly from a reference line, on which the value's position is actually taken.)

In the case of GLMs with nonlinear link function, the scales work as above, but the scale of the linear predictor will be marked with a nonlinear scale for $Y$, something like (a) above.
Examples of all of these situations can be found in Harrell's Regression Modeling Strategies.

Just a couple of side notes

I'd much prefer to see two points scales, at the top and bottom of the relevant section; otherwise it's hard to "line up" accurately because you have to guess what 'vertical' is. Something like this:

However, as I note in comments, for the  last section of the diagram (total points and linear predictor) perhaps a better alternative to a second points scale would be to simply have a pair of back-to-back scales (total points on one side, linear predictor on the other), like this:

whereupon we avoid the need to know what 'vertical' is.
With only two continuous predictors and a single binary factor, we can quite readily construct a more traditional alignment nomogram:

In this case you simply find the wt and qsec values on their scales and join them with a line; where they cross the mpg axis, we read off the value (while the am variable determines which side of the mpg axis you read). In a simple case like this, these kind of nomograms are faster and simpler to use, but can be less easy to generalize to many predictors, where they can become unwieldy. The points-style nomogram in your question (as implemented in Regression Modeling Strategies and in the rms package in R) can add more variables seamlessly. This can be quite an advantage when dealing with interactions.
Comment: This is an amazing post Glen.  I think I'm going to modify the `nomogram` function's `plot` method to add the extra `Points` scale by default.  Great suggestion!!
Comment: I have edited my answer to respond to this additional question.
Comment: @FrankHarrell No, because you don't read any relationship between points and total points - they needn't even be on the same page. The problem is finding 'vertical' so you can read from total-points to the linear predictor accurately. If those two scales are very close or touching, rather than say two inches apart, it's not an issue. When they're inches apart, a few degrees of error in finding vertical can mean a fair bit of error in the result. You still need two points scales in the first section though, because you can have many predictors and they can't all be adjacent to the points scale.
Comment: @FrankHarrell Personally I think a grid clutters the display, and tends to obscure its value; I avoid grids when I can. They also don't allow the same degree of precision in getting vertical unless the grid is as fine as the scale (which I think would be terrible visually), Without an option for one, I'd end up editing in a second points scale (just as I did here) to get it to be in what I'd see as a usable state for the two purposes I see it having (i. making reasonably accurate estimation of predicted values without obscuring  ii. the clarity of the relative contributions to the response)..
Comment: Thanks for a clear, illustrated explanation. Does it work the same way for non-linear or polynomial regressions?
Comment: @FrankHarrell Thanks for your kind words. For the last section of the diagram (total points and linear predictor) perhaps a better alternatve to a second points scale would be to simply have a pair of back-to-back scales (total points on one side, linear predictor on the other).
Comment: Neat idea and would save space.  Or should it be Points and Total Points back-to-back?
Comment: @FrankHarrell : please see my edit with diagram.
Comment: Well, I guess they can, if you replicate a points scale for each predictor. There are some reasons (cleaner visual comparison of effect sizes) why a second point scale at top and bottom might be better, but for just reading the values rnso's scale replication is lind of nice.
Comment: @Glen_b interesting idea to put a points scale with every predictor's axis.  I wonder if that is going to get too busy when there are 15 predictors or when a predictor interacts with a 10-level categorical predictor.  The only remedy I can think of is to put each points axis and their tick mark labels in gray scale.  But that is still fairly busy looking.
Comment: @FrankHarrell I agree; that would only work with a few predictors. But the Points scale at both the top and bottom of all the scales would still work; it might be a better default.
Comment: If Points scale is only on the sides, a vertical grid should be provided for easy checking of points for each predictor.
Comment: There has long been an option for that.  I wonder if that solves the issue other than adding a duplicate points scale at the bottom.
Comment: I can't figure out how  the "points" related to the logodds? could you explain @Glen_b
Comment: Read Harrell's *Regression Modeling Strategies*.
Comment: @Glen_b: Do you happen to know what formula should be used to retrieve the predicted value of mpg from nomogram information?  Let's say we know the predictor values (i.e., wt = 4, am = 1, qsec = 18) and can use the nomogram to derive the corresponding Total Points for these values.  We also know the Points per unit of linear predictor from the nomogram.  What formula can we plug this information into to get the predicted value of the response variable mpg?  Does this formula rely on the Intercept of the ols() model?
Comment: No formula is needed, that's the point of it. With the simpler kinds of nomograms for only a few predictors you can just read the predictions directly off their scale. With ones with more than a couple of predictors sum the points, then read the prediction off from the prediction scale. A nonidentity link function can be encoded directly into that scale. You should never need to do anything harder than adding points


How to show the parent and child taxonomy term?

Question: I have a vocabulary like this:

Parent 1

Child 1
Child 2
Child 3

Parent 2

Child 1
Child 2

Now I would like to use a view to create a list of a Parent term with its Child terms. I try to do this with Views UI, but only show the term parent... No the child term's. It is very confusing for me.
Can anyone help with this case? Thanks :D
Answer: Views can do this. Views is a UI for sql searches so you are setting the definition for the query that will be made of the content database.

Create a view to display a taxonomy term with a page with a path configured along this line category/%. The % is a wildcard that will make the view respond to any value applied in the URL that matches this format.
Set up your display and any filters desired.
Define a relationship (third column of view UI page) and set it to taxonomy term: parent. This tells the view to look at the parent field in each taxonomy term searched. (Note that Drupal includes a field for each term that identifies its parent, but not its siblings or children).
Next we tell the view to interpret the wildcard from the URL we used in the path for the view: Define a contextual filter (above the relationship selection) and set it to a taxonomy:termID. In the form, set the relationship to Parent in the drop down box. This tells View that the term being called in the URL will be applied as a filter for the 'parent' field of the taxonomy terms as they are searched.
Finally we must complete refining the contextual filter. You can tell the view what to do if it does not find an answer in that section of the form if you want, but we must tell it what to do if it does find solutions in the query. To do so check the validation check box. Set the Validator to Taxonomy term and select the vocabulary that applies to your hierarchy. Then select either Term ID or Term name converted to term ID as the Filter value type. If the value you are passing in the % is a term ID, then you are done. If the passed value is a term Name, then you may want to check the box to convert dashes to spaces. IF you have multiple word term names, the URL will have dashes, which do not match the actual spaces in the term name. This little correction makes them agree so a test succeeds.
Finally go to the preview area and enter the name or ID for a term you know has children and you should get the list of children terms. Use the sorting settings to arrange them in whatever order you need.

Good Luck!
Comment: The identity of the parent is a field of the taxonomy term and the field is 'pid'. It is stored there as a term number reference, so it is already there, but only as a reference.
Comment: I find "Taxonomy term: Parent term" in Relationships, but I can't add the parent ID in the "Field" section. I want to show a table with the parentID in a column (if it exists).
Comment: Hello Ashlar, thank you for this explanation. Do you have any idea why **taxonomy term: parent** is not showing in the list of relationships?
Answer: I have archive this use view too, but in slightly different way compare Ashlar.

Add  Taxonomy term: Term ID  in CONTEXTUAL FILTERS.
Add  Taxonomy term: Parent term  in RELATIONSHIPS.  (leave Require this relationship unchecked.)
Add Taxonomy term: Parent term in filter section. select great than 0.
do not forget to filter your taxnonmy term in mycase Taxonomy vocabulary: Machine name (= Business Directory).
add Taxonomy term: Rendered Taxonomy term to your fields , select relationship to parent
, check the Exclude from display.
In setting, under FORMAT section. In grouping field 1, select (Parent) Taxonomy term: Rendered Taxonomy term.

You should good to go. you can use view tpl to add more class base on the group. I have enclose my screenshot for references. Sorry for my English.
Comment: did not work for me. Shows all not-top-level taxonomy terms.
Answer: If you'd like to use that view as a field, you can create such a list without using Views, by using Taxonomy Term Reference Filter by Views module and you can specify the parent, so the list can be filtered automatically.


How to install "memcached" to php 5.6?

Question: I had installed PHP 5.6.20 from this source. https://webtatic.com/packages/php56/
But I cannot install memcached. Just only memcache.
How to install memcached to PHP 5.6?
This is what i tried.
[root@testcentos ~]# yum -y install php56w-pecl-memcached.x86_64
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirror2.totbb.net
 * epel: ftp.jaist.ac.jp
 * extras: mirrors.nwsuaf.edu.cn
 * remi-safe: mirror.neolabs.kz
 * updates: mirror2.totbb.net
 * webtatic: sp.repo.webtatic.com
Resolving Dependencies
--> Running transaction check
---> Package php56w-pecl-memcached.x86_64 0:2.2.0-2.w6 will be installed
--> Processing Dependency: php-pecl-igbinary(x86-64) for package: php56w-pecl-memcached-2.2.0-2.w6.x86_64
--> Processing Dependency: libmemcachedutil.so.2()(64bit) for package: php56w-pecl-memcached-2.2.0-2.w6.x86_64
--> Processing Dependency: libmemcached.so.11()(64bit) for package: php56w-pecl-memcached-2.2.0-2.w6.x86_64
--> Running transaction check
---> Package libmemcached10.x86_64 0:1.0.16-1.ius.el6 will be installed
---> Package php56w-pecl-igbinary.x86_64 0:1.2.1-2.w6 will be installed
--> Processing Conflict: libmemcached10-1.0.16-1.ius.el6.x86_64 conflicts libmemcached < 1.0
--> Finished Dependency Resolution
Error: libmemcached10 conflicts with libmemcached-0.31-1.1.el6.x86_64
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

Comment: Having webtatic + ius + remi configured on the same serveur can only raise issues. Example pulling php56w-pecl-igbinary from webtatic and libmemcached10 from IUS can be a source of problem. You should probably stick with a single provider for the PHP stack.
Answer: Test first with
yum remove libmemcached-0.31-1.1.el6.x86_64

And then
yum install php56w-pecl-memcached.x86_64

Or just
yum install php56w-pecl-memcached

Greetings.
Comment: It works! Thanks a lot.
I'm not familiar with these command, thanks again.

How to install "memcached" to php 5.6?


limiting the log-likelihood function for Weibull distribution

Question:
Let $X_{1}, \cdots, X_{n}$ be random samples from the Weibull distribution $\operatorname{Weibull}(\alpha,1), \alpha >0$. Prove that the solution of the likelihood equation $\dot{l}(\alpha)=0$ is the MLE for $\alpha$.

Here is my attempt:
Since
$f(x;\alpha)=\alpha x^{\alpha-1}\exp{(-x^{\alpha})}I_{x>0}$
,
$$l(\alpha)=n\log{\alpha}+(\alpha-1)\sum_{i=1}^{n}{\log{x_{i}}}-\sum_{i=1}^{n}{x_i^\alpha}$$
$$\dot{l}(\alpha)=\frac{n}{\alpha}+\sum_{i=1}^{n}{\log{x_{i}}}-\sum_{i=1}^{n}{x_i^\alpha \log{x_{i}}}$$
$$\ddot{l}(\alpha)=-\frac{n}{\alpha ^2}-\sum_{i=1}^{n}{x_i^\alpha( \log{x_{i}})^2}<0$$
Therefore, $l(\alpha)$ is a concave function.
Meanwhile,
$$\lim_{\alpha→+\infty}{l(\alpha)}=-\infty$$
$$\lim_{\alpha→+0}{l(\alpha)}=-\infty$$
Therefore, there exists a unique solution to the equation $\dot{l}(\alpha)=0$, which is the MLE for $\alpha$.
I know that I can solve the problem in this way, but I'm struggling with proving that
$$\lim_{\alpha→+\infty}{l(\alpha)}=-\infty$$
.
Answer: For the sake of convenience, let $$\xi = \sum_{i=1}^n \log x_i = \log \prod_{i=1}^n x_i, \quad g(\alpha) = \sum_{i=1}^n x_i^\alpha; \tag{1}$$ then $$\ell(\alpha) = n \log \alpha + (\alpha - 1) \xi - g(\alpha). \tag{2}$$
Now by the AM-GM inequality (since $x_1, \ldots, x_n > 0$),
$$g(\alpha) \ge n \left(\prod_{i=1}^n x_i^\alpha\right)^{1/n} = n e^{\xi \alpha/n}. \tag{3}$$  So $$\ell(\alpha) \le n \log \alpha + (\alpha-1)\xi - n e^{\xi \alpha/n}. \tag{4}$$  We easily see that for $n > 0$, $\ell(\alpha) \to -\infty$ as $\alpha \to 0^+$ irrespective of $\xi$.
If $\xi > 0$, then the last term in $(4)$ dominates, and $\ell(\alpha) \to -\infty$ as $\alpha \to \infty$.  If $\xi < 0$, then the second term dominates and again the log-likelihood approaches $-\infty$.  The only case in which the inequality $(4)$ is inconclusive is if $\xi = 0$.  So we return to $(2)$ and compute
$$\ell(\alpha \mid \xi = 0) = n \log \alpha - g(\alpha). \tag{5}$$  Let $x_{(n)} = \max_i x_i$ be the maximum order statistic.  If $x_{(n)} > 1$, then $$g(\alpha) > x_{(n)}^\alpha \to \infty$$ as $\alpha \to \infty$, and this term dominates in $(5)$.  The case $x_{(n)} < 1$ is impossible because $\xi = 0$ implies $e^\xi = \prod_{i=1}^n x_i = 1$ hence there must be at least one $x_i$ whose value is at least $1$.  The final case, $\xi = 0$ and $x_{(n)} = 1$, implies $x_i = 1$ for all $i$, thus $g(\alpha) = n$ and $$\ell(\alpha \mid \xi = 0, g = n) = n (-1 + \log \alpha), \tag{6}$$ which does not have a global maximum since in this situation $\ell'(\alpha) = n/\alpha$ which has no critical point.  However, such a sample occurs with probability $0$.
Answer: I believe that the issue is about the sufficiency of the solution to the first order condition with respect to the global optimization problem.
As you showed that the log-likelihood function is strictly concave, any solution for first order condition, that is $\alpha: \dot l(\alpha)=0$ is indeed a strict (unique) global maximum. I don't see what more you need to prove. This is a well known result from optimization theory.
But to actually find this optimal $\alpha$, you will need a numerical method, as closed form solutions are not available in this case.
Comment: I need to prove that the existence of the solution to the $\dot{l}(\alpha)=0$ as well, because being concave doesn't guarantee the existence of maximum ($\log{x}$?).
Comment: Look at the statement in the question you presented: "Prove that the solution of the likelihood equation $\dot l(\alpha)=0$ is the MLE for $\alpha$".  It is not asking you to prove the existence of the solution, but to prove that if the solution to $\dot l(\alpha)=0$ exists then it is MLE for $\alpha$.
Comment: Maybe that's a translation problem... The original problem is not in English and I couldn't think of a proper translation for it. But initially it is not asking to prove that "if the solution to l˙(α)=0 exists then it is MLE for α". Proving the existence of the solution is also a part of the problem.
Comment: Even if proving the existence of the solution is not what the problem is asking, I'm still curious about how to prove it.
Comment: I believe that $\lim_{\alpha\rightarrow +0} \dot l(\alpha)=+\infty$.
Comment: https://www.wolframalpha.com/input?i=limit%28log%28a*x%5E%28a-1%29*exp%28-x%5Ea%29%29%2Ca%2C0%29


Spring AutoWired and SessionFactory

Question: Here is My Exception
Exception in thread "main" java.lang.IllegalArgumentException: Property 'sessionFactory'       is required
  at   org.springframework.orm.hibernate3.HibernateAccessor.afterPropertiesSet(HibernateAccessor.java:314)
at org.springframework.orm.hibernate3.HibernateTemplate.<init>(HibernateTemplate.java:139)
at com.cetin.services.Imp.MyServiceImp.<init>(MyServiceImp.java:29)
at com.cetin.services.Imp.Program.main(Program.java:10)

Here is My ApplicationContext file
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:faces="http://www.springframework.org/schema/faces"
xmlns:int-security="http://www.springframework.org/schema/integration/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/integration/security http://www.springframework.org/schema/integration/security/spring-integration-security-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config />
<context:component-scan base-package="com.xxx" />

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
        value="com.mysql.jdbc.Driver">
    </property>
    <property name="url"
        value="jdbc:mysql://localhost:3306/nodeser">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="002210"></property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

 <tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

And My class
package com.cetin.services.Imp;

import com.cetin.services.IMyService;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

@Service("myService")
public class MyServiceImp
implements IMyService{

@Override
public String Hello(String msg) {
    return "Hello "+msg;
}

@Autowired(required=true)
private SessionFactory sessionFactory;

private HibernateTemplate hibernateTemplate;

public MyServiceImp()
{

    hibernateTemplate = new HibernateTemplate(sessionFactory);
}

public int getSize()
{
return hibernateTemplate.find("from Category").size();
}

 }

How can I fix this Problem ?
Answer: Your Session factory is not injected and is null.
Its probably happened because com.cetin.services.Imp.MyServiceImp is out of Spring Application  Context.
create <bean id="..." ....> initiation for this class for this and inject SessionFactory using Setter injection.
Otherwise mark it as @Repository and make it visible for component-scan

Spring AutoWired and SessionFactory


Logrotate - how change it so that it never deletes logs and will create a .gz every 6 months?

Question: I read the man logrotate, but it's still unclear to me. My questions are mainly related to the NGINX webserver logs.
This is my current logrotate.conf:
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

and in /etc/logrotate.d I have among the others also: nginx:
/var/log/nginx/*log {
    create 0644 nginx nginx
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

And these are some of the .gz I can see:
access.log-20150430.gz
access.log-20150509.gz (created after ~10 days)
access.log-20150524.gz (created after ~15 days)
access.log-20150528.gz (created after ~4 days)

Why the .gz logs are created so often and at so "random" timeframes?
What should I change in order to have new .gz created every 6 months and/or after a .log file will be bigger than 100MB?
With rotate 10 does it mean that if I have already 10 .gz files, then when it compresses the 11th, it will automatically delete the oldest .gz file? If yes, how can I avoid deleting logfiles? I must keep them all, even if very old.
What is the difference between daily and rotate 10?
Is the create permission for those files ok?
After I change something on the logrotate.conf, should I restart some service, or the changes should be applied immediately?
Answer: I think logrotate manual is clear about most of your points. Here are my answers:

Using daily causes log rotation to happen every day. So, you need to check why it is not happening on daily basis. For example, this can be caused by the other option notifempty if the log file was empty for few days.
I can't find an option to do it every 6 months, but you have monthly and yearly. For the size confitions, you can use size 100M.
Yes. To keep them all, you can choose a very large value.
daily specifies how often to rotate while rotate specifies number of log files kept.
If the log does not contain sensitive information (others can read), it should be fine.
No, you don't need to restart anything.


Deny Static IP Assignments

Question: I am currently using a SonicWall Pro 2040 with DHCP enabled, but only with static IPs mapped to specific MAC addresses. There is no dynamic IP scope being used. Currently, no DHCP request will resolve an IP, as desired.
However, if a server or workstation network interface is still configured with a user-defined, static IP address, it works fine. I would like potential users who assign their machines with static IPs to not work.
Is there a way to deny these types of connections whose MAC address is not approved?
Answer: There is no way to configure a DHCP server to deny static IP addresses. If you think about it, there is a direct path between hosts on the network that simply does not go through the SonicWall:
            SonicWall
            LAN Port
                ^
                |
                v
HostA <----> Switch <----> HostB

So if you want to filter on MAC address to stop HostA from advertising an IP address on the network, you need to do so at the switch. As an example, if your switch is a Cisco, the command to use is switchport port-security.
Comment: Thanks for the answer, Tom. I figured I was just missing something in my understanding here. I'm looking to get new hardware soon, but I'll see if my current switch supports this.
Answer: Short answer (on the switch):

Disable all ports that aren't connected device receiving a reserved IP
Setup port-security using sticky macs

The following commands will set an interface to allow only currently connected device to use that switchport:
switchport mode access
switchport port-security
switchport port-security maximum 1
switchport port-security mac-address sticky

source: Cisco port security config documentation
Long answer:
This is a 2 part problem.  Part one assigning addresses, part to disallowing access to computers not assigned a "reserved address" in DHCP from the sonic wall.  I think you've got part one solved on your sonicwall.  The second part needs to be solved on your switch.
Like @Tom Shaw said using port-security command on cisco switchgear is a good idea. However it's incomplete you need to specify the max number of devices MAC addresses that can come from that port switchport port-security maximum 1 (provided there isn't another a switch or hub hanging off that port).
To save yourself from having to type in MAC addresses you can use switchport port-security mac-address sticky after you've turned on port security. to add the MAC address of the currently connected device to the approved list without having to type it in by hand.
Comment: Do we need this much detail when the original poster hasn't even mentioned Cisco? I mentioned it in my answer just to show that it could be done.
Comment: Thanks for the great response. Although my switch is not currently Cisco, I am actually looking to acquire new hardware soon and this advice will help in any case.

Deny Static IP Assignments


How to show the solution of $\dot{x} = Ax $ is an invariant subspace?

Question: Consider the linear dynamical system $\dot{x} = Ax $ in $V$ a finite dimensional vector space.
The definition of an invariant subspace $U$ is as follows:
For all $x_0 \in U$, (initial condition), the solution $x(t)$ with initial condition $x(0)=x_0$ lies in $U$, i.e. $x(t) \in U$ for all $t \in \mathbb{R}$.
Intuitively, it means if we start in a subspace we never leave that subspace.
Prove that a subspace $U \subset V$ is an invariant subspace if and only if $AU \subset U$.
Comment: I think you want to say $AU \subset U$, since $AU \subset V$ is *always* true.  What do you think?  Cheers?
Comment: I added the "ordinary-differential-equations" tag to your post.
Comment: I fact I edited your post in accord with my first comment.  Hope that's OK.  If I'm wrong, change it back.
Answer: Given the linear, constant coefficient differential equation
$\dot x = Ax, \tag 1$
it is well-known that the solution with
$x(0) = x_0 \tag 2$
is
$x(t) = e^{At}x_0; \tag 3$
now if
$AU \subset U, \tag 4$
and
$x_0 \in U, \tag 5$
then
$Ax_0 \in U, \tag 6$
and indeed, for any non-negative $k \in \Bbb Z$,
$A^kx_0 \in U \tag 7$
as well; thus
$e^{At}x_0 = \left (\displaystyle \sum_0^\infty \dfrac{A^k t^k}{k!} \right ) x_0 = \displaystyle \sum_0^\infty \dfrac{t^k A^k x_0}{k!} \in U, \; \forall t \in \Bbb R, \tag 8$
since the series on the right converges to an element in $U$, a closed sub-space, being finite dimensional.
Now suppose every solution initialized at $x_0 \in U$ remains in $U$ for all $t$; then
$x(t) = e^{At}x_0 \in U, \; \forall t \in \Bbb R; \tag 9$
since $U$ is a linear subspace of $V$, the tangent vector to any curve in $U$ is also in $U$, whence
$\dot x(t) = Ae^{At}x_0 \in U, \forall t \in \Bbb R; \tag{10}$
setting $t = 0$ yields
$Ax_0 = Ae^{A(0)}x_0 = \dot x(0) \in U; \tag{11}$
but since $x_0 \in U$ is arbitrary, we have
$AU \subset U, \tag{12}$
and we are done.  $OE\Delta$.
Comment: @Darthsid1995:  if $Ax = \mu x$ then $e^{At}x = e^{\mu t}x$, so yes, these solutions don't *become* invariant, they *are* invariant.
Comment: does that mean if the solutions evolves in the eigenvector direction, it will become invariant ?
Comment: @Robert Lewis: Is there any other way to get $(12)$ other than tangent vector notion?
Comment: @Saeed:  not sure, but whatever notion works, it will mos' likely be related to the tangent vectors of solution curves I think; you have to somehow get from $e^{At}x_0 \subset U$ to $AU \subset U$, and that's the only way I know . ..


Should co-variates be included in a post-match GLM to estimate treatment effect?

Question: After conducting the following genetic match using the MatchIt package, I would like to conduct a post-match analysis of treatment effect:
df_match <- MatchIt::matchit(hxcopd ~ sex + race + age + diabetes + smoke + dyspnea + fnstatus2 + ascites + hxchf + hxmi + hxangina + hypermed + renafail + dialysis + steroid + bleeddis + wtloss, data = df_m, method = "genetic", pop.size = 1000)

summary(df_match)

Summary of balance for matched data:
                                        Means Treated Means Control SD Control Mean Diff eQQ Med eQQ Mean eQQ Max
distance                                       0.1637        0.1607     0.1847    0.0030  0.0559   0.1136  0.3928
sexFALSE                                       0.2409        0.2409     0.4280    0.0000  0.0000   0.1168  1.0000
sexTRUE                                        0.7591        0.7591     0.4280    0.0000  0.0000   0.1168  1.0000
raceAsian                                      0.0073        0.0073     0.0852    0.0000  0.0000   0.0000  0.0000
raceBlack                                      0.0876        0.0949     0.2933   -0.0073  0.0000   0.0657  1.0000
raceNative Hawaiian or Pacific islander        0.0000        0.0000     0.0000    0.0000  0.0000   0.0000  0.0000
raceWhite                                      0.8905        0.8905     0.3125    0.0000  0.0000   0.0803  1.0000
age                                           68.2555       68.1509    12.7220    0.1046  1.0000   1.2409  5.0000
diabetesTRUE                                   0.0949        0.0876     0.2829    0.0073  0.0000   0.0730  1.0000
smokeTRUE                                      0.3869        0.3869     0.4874    0.0000  0.0000   0.2044  1.0000
dyspneaTRUE                                    0.3212        0.3212     0.4673    0.0000  0.0000   0.2701  1.0000
fnstatus2Partially dependent                   0.0146        0.0146     0.1200    0.0000  0.0000   0.0073  1.0000
fnstatus2Totally dependent                     0.0000        0.0000     0.0000    0.0000  0.0000   0.0000  0.0000
ascitesTRUE                                    0.0146        0.0146     0.1200    0.0000  0.0000   0.0073  1.0000
hxchfTRUE                                      0.0073        0.0073     0.0852    0.0000  0.0000   0.0000  0.0000
hxmiTRUE                                       0.0000        0.0000     0.0000    0.0000  0.0000   0.0000  0.0000
hxanginaTRUE                                   0.0219        0.0219     0.1465    0.0000  0.0000   0.0146  1.0000
hypermedTRUE                                   0.5766        0.5766     0.4945    0.0000  0.0000   0.0292  1.0000
renafailTRUE                                   0.0073        0.0073     0.0852    0.0000  0.0000   0.0000  0.0000
dialysisTRUE                                   0.0146        0.0073     0.0852    0.0073  0.0000   0.0073  1.0000
steroidTRUE                                    0.1022        0.1022     0.3031    0.0000  0.0000   0.0803  1.0000
bleeddisTRUE                                   0.0657        0.0657     0.2479    0.0000  0.0000   0.0511  1.0000
wtlossTRUE                                     0.0073        0.0073     0.0852    0.0000  0.0000   0.0000  0.0000

Percent Balance Improvement:
                                        Mean Diff. eQQ Med  eQQ Mean  eQQ Max
distance                                   97.8044 19.5737   15.4411  18.9160
sexFALSE                                  100.0000  0.0000 -100.0000   0.0000
sexTRUE                                   100.0000  0.0000 -100.0000   0.0000
raceAsian                                 100.0000  0.0000  100.0000 100.0000
raceBlack                                  52.3051  0.0000 -350.0000   0.0000
raceNative Hawaiian or Pacific islander   100.0000  0.0000  100.0000 100.0000
raceWhite                                 100.0000  0.0000  -83.3333   0.0000
age                                        99.0985 91.6667   89.3149  78.2609
diabetesTRUE                               71.5156  0.0000 -233.3333   0.0000
smokeTRUE                                 100.0000  0.0000    0.0000   0.0000
dyspneaTRUE                               100.0000  0.0000    7.5000   0.0000
fnstatus2Partially dependent              100.0000  0.0000    0.0000   0.0000
fnstatus2Totally dependent                100.0000  0.0000  100.0000 100.0000
ascitesTRUE                               100.0000  0.0000    0.0000   0.0000
hxchfTRUE                                 100.0000  0.0000    0.0000   0.0000
hxmiTRUE                                  100.0000  0.0000  100.0000 100.0000
hxanginaTRUE                              100.0000  0.0000    0.0000   0.0000
hypermedTRUE                              100.0000  0.0000   87.0968   0.0000
renafailTRUE                              100.0000  0.0000    0.0000   0.0000
dialysisTRUE                               17.1281  0.0000    0.0000   0.0000
steroidTRUE                               100.0000  0.0000    0.0000   0.0000
bleeddisTRUE                              100.0000  0.0000  -40.0000   0.0000
wtlossTRUE                                100.0000  0.0000    0.0000   0.0000

Sample sizes:
          Control Treated
All          4490     137
Matched       611     137
Unmatched    3879       0
Discarded       0       0

I can accomplish that with the following:
df_m2 <- MatchIt::match.data(df_match)
fit <- glm(outcome ~ hxcopd, data = df_m2, weights = weights)
lmtest::coeftest(fit, vcov. = sandwich::vcovHC)
lmtest::coefci(fit, vcov. = sandwich::vcovHC)

The weights = weights are weights produced by by the genetic match and appropriately account for the fact that multiple control units are matched to the same treated unit and that each treated unit might have multiple controls.
I have a few questions regarding the post match analysis, feel free to answer as few as you would like:

If my outcome is binary, can I simply adjust my model by adding family = "binomial"?

    df_m2 <- MatchIt::match.data(df_match)
    fit <- glm(outcome ~ hxcopd, data = df_m2, weights = weights, family = "binomial")`
    lmtest::coeftest(fit, vcov. = sandwich::vcovHC)
    lmtest::coefci(fit, vcov. = sandwich::vcovHC)

Do I need to include any additional covariates in the formula outcome ~ hxcopd? My outcome is binomial but I am also interested to hear if the answer to this question depends on if my outcome is either binomial or continuous. Additionally, the explanatory variables are a mix of binomial, categorial, and continuous.

This cross-validated answer suggests that they do not need to be included if balance is obtained by pre-processing - but how can I validate that in my own data quantitatively to say that I am "balanced enough" not to include the covariates. The user then goes on to say doing it anyway is not a bad idea because it is "doubly robust", so does that make this discussion a moot point?

If I set replace = FALSE and ties = FALSE so that my genetic matching is essentially nearest neighbor, would I even need the use of glm? Would it be valid just to directly compare the sample populations via hypothesis testing the outcome of interest (such as chi-square)?
Answer: The key with binary outcomes is deciding on the causal estimand. First, you need to decide on your effect measure. The most commonly reported effect measures are the risk difference (RD; $P(Y^1=1)-P(Y^0=1)$), the risk ratio (RR; $P(Y^1=1)/P(Y^0=1)$) and the odds ratio (OR; $\frac{P(Y^1=1)}{1-P(Y^1=1)}/\frac{P(Y^0=1)}{1-P(Y^0=1)}$). Second, you need to decide whether you want a conditional effect or a marginal effect. A conditional effect is the effect for a given subclass of units (i.e., defined by a combination of covariate values). A marginal effect is the effect for a population. Conditional effects are useful when deciding whether to treat an individual patient. Marginal effects are useful when deciding whether to implement a rule or policy.
For the RD, the marginal effect is the average of the conditional effects. In this way, the RD is collapsible. The RR and OR are not collapsible, meaning if there is any heterogeneity in individual causal effects, the average of the conditional effects is not the marginal effect (unless the marginal and conditional effects are all zero). This has led to many debates about the best choice of estimand.
Conditional Effects
There will certainly be effect heterogeneity in the conditional RD, which is to say that the RD of the treatment is not the same for all units. For example, if the average RD of for treatment was .4, what would happen to units who had a baseline risk (i.e., under control) of .7? Clearly their risk under treatment cannot be 1.1. If you want to estimate conditional RDs, you have to model the effect heterogeneity, which is sure to be nonlinear. This is true of the RR as well (i.e., how would an average RR of 2 apply to someone with a baseline risk of .55?).
The OR does not face this problem; it is possible for the OR to be the same for all units. No matter what the baseline risk is, the expected risk under treatment is a valid number even if the OR is constant for all units. This is a reason to prefer the OR; you don't need to model effect heterogeneity, and it's possible to provide a single number that represents the conditional effect for all units. This is not possible with the RD or RR. Estimating the conditional OR is also straightforward; it is the coefficient on the treatment in a logistic regression of the outcome on the covariates and treatment, and the Wald or likelihood ratio test for the coefficient is a test of the null hypothesis of no treatment effect.
An often-cited problem with the conditional OR is that its interpretation depends on the variables conditioned on in the model. The results of two studies examining the same treatment and outcome but conditioning on different covariates are not comparable to each other, even in the absence of confounding. In addition, the OR itself is challenging for people to interpret; most people tend to think about risks rather than odds, and people often misinterpret the OR as a RR (which is actually sometimes, but not often, valid).
Marginal Effects
A marginal effect integrates over possible effect heterogeneity in the population and cannot be said to apply to any individual unit when effect heterogeneity is present. That is, an estimated marginal RD of .2 doesn't actually mean the RD is .2 for any individual, which means you can't use a marginal effect to make clinical decisions. Even if there is no effect heterogeneity as measured by the OR, the marginal OR is not equal to the OR for an individual. When there is effect heterogeneity, the marginal effect integrates across the population, but the specific characteristics of the population (which are often unmeasured) determine the marginal effect, so different populations will generally have different marginal effects, and it's rare that a study sample is actually a simple random sample from the population to which the effect is to generalize to. (There are methods of generalizing an effect to a specific population, though, but those are beyond simply estimating an effect from that data you have.)
In a randomized trial, the unadjusted contrast of the outcome under treatment and control is a marginal effect estimate. Because propensity score methods seek to emulate the conditions of a randomized trial (i.e., by removing confounding due to the measured covariates assuming strong ignorability), the unadjusted contrast of the outcome under treatment and control is a marginal effect estimate. Estimating the unadjusted marginal effect in a matched sample is as simple as regressing the outcome on just the treatment using the appropriate link function. (It's possible to estimate a covariate-adjusted marginal effect as well; this is called g-computation and involves average across model-predicted values of the outcome under treatment and control.)

Now let's get into the practical aspect of this for you and your analysis. You need to decide what kind of estimand you want. Given that your treatment is COPD, which is not something about which policy can be made, it is likely you are interested in estimating a conditional effect (i.e., the effect of COPD for each unit). If you want a single number that can plausibly represent the conditional effect for all units, you want a conditional OR. If you just want to test the null hypothesis that there is no effect (i.e., what you would do with a $\chi^2$ test), it doesn't matter what method you use, but a covariate-adjusted conditional OR will have the most power to detect the effect.
To get a conditional OR estimate, you need to regress the outcome on the treatment and conditioning covariates using a logistic regression model. We can do this in R with the following code:
fit <- glm(outcome ~ hxcopd + sex + race + age + diabetes +
              smoke + dyspnea + fnstatus2 + ascites +
              hxchf + hxmi + hxangina + hypermed + renafail +
              dialysis + steroid + bleeddis + wtloss,
           data = df_m2, weights = weights,
           family = quasibinomial(link = "logit"))

lmtest::coeftest(fit, vcov. = sandwich::vcovHC)[1:2,]

The coefficient on treatment is the conditional OR and can be reported as such in the results write-up, but it's important to report the specific covariates you conditioned on to provide context for interpreting the results.
You may ask, if you were going to perform regression on the covariates anyway, why do matching? Matching makes it more likely that a logistic regression model on main effects will be a good fit to your data and prevents minor additional confounding due to unmodelled nonlinearities. That said, some argue that if you are going to perform logistic regression, matching is not necessary as long as work is done to ensure the regression is robust. Frank Harrell's RMS book and course explain this in detail, and his rms package provides tools to do this.
The code you ran estimates a marginal effect (and without family = quasibinomial(link = "logit")), that effect is on the RD scale. While such an analysis would be appropriate for some research questions, it doesn't seem like it would be appropriate for yours.
Comment: Thank you, you really went above and beyond with this answer and have really clarified a lot for me. I will check out the RMS book. Additionally, when you say "the code you ran" which estimates marginal effect on the RD scale, you're referring to `fit <- glm(outcome ~ hxcopd, data = df_m2, weights = weights)`, yes? Thanks for the comprehensive answer, I'll keep an eye on your GitHub and `MatchIt` updates!
Comment: Glad this was helpful, and yes, by "the code you ran" I meant that line.
Comment: Also would you be able to clarify where in your answer (if you did answer it) is the answer to my question #3? If you did answer it, I'm sorry, I'm probably just not understanding. Thanks!
Comment: It was implicit, so sorry about that. If you just want to test whether there is any effect at all, a chi-square test is sufficient but severely underpowered compared to a test of the conditional effect using a logistic regression model with covariates. A chi-square test also doesn't tell you the size of the effect (and the phi coefficient only corresponds to the marginal effect, which I presume you don't care about). An unadjusted logistic regression should give (approximately) the same p-values as a chi-square test, so you might as well use that.
Comment: Awesome thanks! It's hard to know where to start learning about this kind of hypothesis testing and modeling when you have really no formal training in mathematics or stats beyond basic undergrad requirements.
Comment: I understand. I mean, I had to get a PhD in it to understand it, and it might be worthwhile hiring or collaborating with someone with formal training in biostatistics to do these analyses with you. These are advanced statistical methods that should not be performed without extensive formal training. They're not meant to be accessible to those without training because it does require specific technical knowledge to perform and interpret the analyses correctly.
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/113603/discussion-between-dylan-russell-and-noah).


scatter chart high number of points

Question: I am using following code to insert points in the scatter chart:
series: [
 {
  name: 'Machine 1',color: '#0000FF',marker: {radius: 3, symbol: 'circle'},
   data: [
     {x: Date.UTC(2020,10,07,00,00,00), y: 0, prod_type: 'Production', number_of_cycles: '10',},
     {x: Date.UTC(2020,10,07,00,01,00), y: 0, prod_type: 'Production', number_of_cycles: '11',}
    ]
 },
 {
  name: 'Machine 2',color: '#00FF00',marker: {radius: 3, symbol: 'circle'},
   data: [
     {x: Date.UTC(2020,10,07,00,00,00), y: 1, prod_type: 'Setup', number_of_cycles: '1',},
     {x: Date.UTC(2020,10,07,00,01,00), y: 1, prod_type: 'Setup', number_of_cycles: '2',}
    ]
 }
]

This works but when I put a high number of points (more than 1,000) into one section data[], the chart shows nothing.
Thanks for your help
Jan
Answer: That is caused by the turboThreshold option:

turboThreshold: number
When a series contains a data array that is longer than this, only one
dimensional arrays of numbers, or two dimensional arrays with x and y
values are allowed. Also, only the first point is tested, and the rest
are assumed to be the same format. This saves expensive data checking
and indexing in long series. Set it to 0 disable.
Note: In boost mode turbo threshold is forced. Only array of numbers
or two dimensional arrays are allowed. Defaults to 1000.

You can map your data to a simpler format, like: [x, y] or change the value of turbo threshold.

API Reference: https://api.highcharts.com/highcharts/series.scatter.turboThreshold

scatter chart high number of points