Derivative of a linear model

Question: Question
Is there such concept in econometrics/statistics as a derivative of parameter $\hat{b_{p}}$ in a linear model with respect to some observation $X_{ij}$?
By derivative I mean $\frac{\partial \hat{b_{p}}}{\partial X_{ij}}$ - how would change parameter $\hat{b_{p}}$ if we changed $X_{ij}$?
Motivation
I was thinking about a situation when we have some uncertainty in data (e.g. results from a survey) and we have enough money to obtain precise results only in a single observation, which observation should we choose?
My intuition is saying that we should choose the observation that might change parameters the most, which is equivalent to the highest value of the derivative. If there are any other concepts feel free to write about them.
Comment: Hey @tomek, do you mean parameter $b_p$ or parameter estimate $\hat{b_p}$?
Comment: @suncoolsu: I menat parameter estimate $\hat{b_{p}}$
Answer: I guess this would come under the heading of regression diagnostics. I haven't seen this precise statistic before, but something that comes fairly close is DFBETAij, which is the the change in regression coefficient i when the jth observation is omitted divided by the estimated standard error of coefficient i.
The book that defined this and many other regression diagnostics (perhaps too many) is:
Belsley, D. A., E. Kuh, and R. E. Welsch. (1980). Regression Diagnostics: Identifying Influential Data and Sources of Collinearity.  New York: Wiley. ISBN 0471691178
Comment: @onestep: This is very good answer to the problem described in the motivation, I haven't heard about it earlier and it seems to be really useful. Thanks!
Answer: @onestop points in the right direction.  Belsley, Kuh, and Welsch describe this approach on pp. 24-26 of their book.  To differentiate with respect to an observation (and not just one of its attributes), they introduce a weight, perform weighted least squares, and differentiate with respect to the weight.
Specifically, let $\mathbb{X} = X_{ij}$ be the design matrix, let $\mathbf{x}_i$ be the $i$th observation, let $e_i$ be its residual, let $w_i$ be the weight, and define $h_i$ (the $i$th diagonal entry in the hat matrix) to be $\mathbf{x}_i (\mathbb{X}^T \mathbb{X})^{-1} \mathbf{x}_i^T$.  They compute
$$\frac{\partial b(w_i)}{\partial w_i} = \frac{(\mathbb{X}^T\mathbb{X})^{-1} \mathbf{x}_i^T e_i}{\left[1 - (1 - w_i)h_i\right]^2},$$
whence
$$\frac{\partial b(w_i)}{\partial w_i}\Bigg|_{w_i=1} = (\mathbb{X}^T\mathbb{X})^{-1} \mathbf{x}_i^T e_i.$$
This is interpreted as a way to "identify influential observations, ... provid[ing] a means for examining the sensitivity of the regression coefficients to a slight change in the weight given to the ith observation.  Large values of this derivative indicate observations that have large influence on the calculated coefficients."  They suggest it can be used as an alternative to the DFBETA diagnostic.  (DFBETA measures the change in $b$ when observation $i$ is completely deleted.)  The relationship between the influence and DFBETA is that DFBETA equals the influence divided by $1 - h_i$ [equation 2.1 p. 13].
Comment: As usual great answer, I was looking exactly for this.
Comment: The book that You mentioned is available for free in google books, well at least most of it. Nevertheless, have You heard about differentiation with respect to a single attribute? I'm asking because I was 'playing' with matrices lately and I have found that it's quite simple to calculate and I was courious if that is anything new...
Comment: @Tomek Differentiation w.r.t. a single attribute ought to give its coefficient (when there are no interactions).  Of course, since the solutions are functions of all the data, you could differentiate w.r.t any particular $x_{ij}$.  It should be straightforward to work out.  (In fact, finding the solution for OLS would be sufficiently general.)  I haven't read B,K,& W in 20 years, so my memory is dim, but I don't recall that they went down to this level of detail and they definitely don't do this computation in the section I quoted.

Derivative of a linear model


Calculating the probability of a sum of Poisson-distributed random variables

Question: In Java, I have a set of users, each of which has a Poisson-distributed demand with a known mean:
import org.apache.commons.math3.distribution.PoissonDistribution;

public class User {
  private int mean;
  private PoissonDistribution dist;
  public void setMean(int mean) {
      this.mean = mean;
      this.poissonDistribution = new PoissonDistribution(mean);
  }
}

...
User u1 = new User(); u1.setMean(20);
User u2 = new User(); u2.setMean(30);
User u3 = new User(); u3.setMean(40);

Now I'd like to calculate the probability that all Users have a cumulated demand below a certain value:
double probabilityBelowX = calculateCumulatedProbability(50, u1, u2, u3); // <- what must this method look like?

I am stuck at the question, how to solve this problem in Java. Am I missing something in the math package? I know that the demands of the customers are independent, so according to my knowledge about the Poisson distribution I can simply add up the values. But I only have cumulativeProbability(int x) for every single one of the user demands, but not for several at once.
Comment: If the demands are $X_1$,$X_2$, & $X_3$, & your certain value is $k$, do you want to calculate (1) the probability that the total demand is less than a certain value $\mathrm{P}(X_1+X_2+X_3<k)$, or (2) the joint probability that demand for each user is less than a certain value $\mathrm{P}(X_1<k,X_2<k,X_3<k)$?
Comment: @Scortchi the first one: I want to know the probability that the aggregated demand is below a given value: $P(X_1 + X_2 + X_3 < k)$
Answer: If you assume that customers demands are independant and Poisson, the total demand will be a Poisson distribution as well with parameter equal to the sum of the individual Poisson parameters (see wikipedia for example).
A C++ code example (I am not so familiar with Java):
double cumulative(double x, User * users, int nusers)
{
    double lambda = 0.;
    for (unsigned i = 0; i < nusers; ++i) lambda += users[i].mean();
    return PoissonDistribution(lambda).cumulativeProbability(x);
}

Comment: Thank you, I will try this in my Java code and give a feedback once I got it implemented. I just didn't know or wasn't sure, that I could simply add up the single means - it just seemed to be too simple!
Comment: OK, it works. Here is the Java code: `double totalDemand = 0; for (User u : tour.getUsers()) {totalDemand += u.getMean();} double cumulativeProbability = new PoissonDistribution(totalDemand).cumulativeProbability(50);`


Ball contained in the union of disjoint open sets

Question: Let $(M, d)$ be a metric space, and let $B(x, r) := \{y \in M : d(x, y) \leq r\}$ be the closed ball of radius $r$ centered at $x$. Let $\{U_i\}_{i \in I}$ be an arbitrary family of disjoint and open subsets of $M$. Suppose that $$B(x, r) \subseteq \bigcup_{i \in I} U_i,$$ for some $x \in M$ and $r > 0$. Can we then conclude that $B(x, r) \subseteq U_i$ for some fixed $i \in I$? My intuition is telling me that this result is true, but I'm struggling to find an elegant proof. Also, if the result is not true, then maybe it's only true for finite or countable unions?
Answer: No. $B(0,\sqrt3)$ is a closed ball in $\Bbb Q$ that is contained in the union of the disjoint open sets $(\leftarrow,\sqrt2)$ and $(\sqrt2,\to)$ but not contained in either of them. It will be true if $B(x,r)$ is connected, however.
Comment: Thanks! Clearly, my assumptions were too general. I posted a new question under more restrictive assumptions [here](https://math.stackexchange.com/q/3845695/829063)
Comment: @dualspace89: You're welcome; I'll take a look.
Answer: Take $M=[0,1] \bigcup [2,3]$ endowed with the usual metric from $\mathbb{R}$, $U_1=[0,1]$ and $U_2=[2,3]$. Then $B(1,2) \subset U_1 \bigcup U_2$ but $B(1,2)$ is neither in $U_1$ nor in $U_2$.
(Note that $B(1,2)=[0,1] \bigcup [2,3]$ here)
Comment: Thanks! Clearly, my assumptions were too general. I posted a new question under more restrictive assumptions [here](https://math.stackexchange.com/q/3845695/829063)


AWS SDK and NODE JS --> UnknownEndpoint

Question: I have followed this guide: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/ec2-example-creating-an-instance.html
When I run the code below - I get the UnknowEndPoint error.

I have created an AWS IAM user and added the user to a group with the policy name "IAMFullAccess" - I don't know which other policy would fit (I can see in the AWS dashboard that my credentials has been used programmatically - So I guess this part works well...)?
I have placed my credentials in the .aws folder in a file named credentials.
I am not sure what actually to put inside the 'KeyName' below - currently I have entered the Key Pair Name I find by clicking on one of my existing AWS EC2 instances in the dashboard. Is this correct?

Is there some security group or similar I need to edit - to able to connect NodeJS to my AWS account? Otherwise I have no clue what to do?
In start.js file:
process.env.AWS_SDK_LOAD_CONFIG=1;
var AWS_SDK = require('./aws_sdk');
var aws_sdk = new AWS_SDK();

aws_sdk.CopyInstance();

In aws_sdk.js file:
function AWS_SDK() {

    this.CopyInstance = function() {

        try {

            // Load the AWS SDK for Node.js
            var AWS = require('aws-sdk');

            //Set the region
            AWS.config.update({region: 'us-east-2a'});

            var instanceParams = {
                ImageId: 'ami-0...',
                InstanceType: 't1.micro',
                KeyName: '<Key_name>',
                MinCount: 1,
                MaxCount: 1
            };

            // Create a promise on an EC2 service object
            var instancePromise = new AWS.EC2({apiVersion: '2016-11-15'}).runInstances(instanceParams).promise();

            // Handle promise's fulfilled/rejected states
            instancePromise.then(
                function (data) {
                    console.log(data);
                    var instanceId = data.Instances[0].InstanceId;
                    console.log("Created instance", instanceId);
                    // Add tags to the instance
                    var tagParams = {
                        Resources: [instanceId], Tags: [
                            {
                                Key: 'Name',
                                Value: 'SDK Sample'
                            }
                        ]
                    };

                    // Create a promise on an EC2 service object
                    var tagPromise = new AWS.EC2({apiVersion: '2016-11-15'}).createTags(tagParams).promise();
                    // Handle promise's fulfilled/rejected states
                    tagPromise.then(
                        function (data) {
                            console.log("Instance tagged");
                        }).catch(
                        function (err) {
                            console.error(err, err.stack);
                        });
                }).catch(
                function (err) {
                    console.error(err, err.stack);
                });
        }
        catch(e){
            wl.info('Error: ' + e);
        }
    }

function create() {

    if(globalAWS === null)
        globalAWS = new AWS_SDK();

    return globalAWS;
}

module.exports = create;

ERROR:

{ UnknownEndpoint: Inaccessible host: ec2.us-east-2a.amazonaws.com'.
  This service may not be available in theus-east-2a' region.
      at Request.ENOTFOUND_ERROR (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\event_listeners.js:486:46)
      at Request.callListeners (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
      at Request.emit (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
      at Request.emit (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\request.js:683:14)
      at ClientRequest.error (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\event_listeners.js:325:22)
      at ClientRequest. (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\http\node.js:93:19)
      at ClientRequest.emit (events.js:182:13)
      at ClientRequest.EventEmitter.emit (domain.js:442:20)
      at TLSSocket.socketErrorListener (_http_client.js:391:9)
      at TLSSocket.emit (events.js:182:13)   message:    'Inaccessible host: ec2.us-east-2a.amazonaws.com\'. This service may not be
  available in theus-east-2a\' region.',   code: 'UnknownEndpoint',
  region: 'us-east-2a',   hostname: 'ec2.us-east-2a.amazonaws.com',
  retryable: true,   originalError:    { Error: getaddrinfo ENOTFOUND
  ec2.us-east-2a.amazonaws.com ec2.us-east-2a.amazonaws.com:443
         at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
       message:
        'getaddrinfo ENOTFOUND ec2.us-east-2a.amazonaws.com ec2.us-east-2a.amazonaws.com:443',
       errno: 'ENOTFOUND',
       code: 'NetworkingError',
       syscall: 'getaddrinfo',
       hostname: 'ec2.us-east-2a.amazonaws.com',
       host: 'ec2.us-east-2a.amazonaws.com',
       port: 443,
       region: 'us-east-2a',
       retryable: true,
       time: 2019-01-14T20:03:42.177Z },   time: 2019-01-14T20:03:42.177Z } 'UnknownEndpoint: Inaccessible host:
  ec2.us-east-2a.amazonaws.com\'. This service may not be available in
  theus-east-2a\' region.\n    at Request.ENOTFOUND_ERROR
  (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\event_listeners.js:486:46)\n
  at Request.callListeners
  (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\sequential_executor.js:106:20)\n
  at Request.emit
  (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\sequential_executor.js:78:10)\n
  at Request.emit
  (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\request.js:683:14)\n
  at ClientRequest.error
  (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\event_listeners.js:325:22)\n
  at ClientRequest.
  (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\http\node.js:93:19)\n
  at ClientRequest.emit (events.js:182:13)\n    at
  ClientRequest.EventEmitter.emit (domain.js:442:20)\n    at
  TLSSocket.socketErrorListener (_http_client.js:391:9)\n    at
  TLSSocket.emit (events.js:182:13)'
Process finished with exit code 0

If I change the REGION to "us-east-2" (delete the 'a' at the end) the error changes to this:
{ Unsupported: The requested configuration is currently not supported. Please check the documentation for supported configurations.
    at Request.extractError (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\services\ec2.js:50:35)
    at Request.callListeners (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at Request.emit (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
    at Request.emit (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\request.js:683:14)
    at Request.transition (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\request.js:22:10)
    at AcceptorStateMachine.runTo (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\state_machine.js:14:12)
    at D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\state_machine.js:26:10
    at Request.<anonymous> (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\request.js:38:9)
    at Request.<anonymous> (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\request.js:685:12)
    at Request.callListeners (D:\Workspace\BitBucket\Test\node_modules\aws-sdk\lib\sequential_executor.js:116:18)
  message:
   'The requested configuration is currently not supported. Please check the documentation for supported configurations.',
  code: 'Unsupported',
  time: 2019-01-14T20:31:55.954Z,
  requestId: '815a44e2-5d0d-453e-a4ff-6faac2695064',
  statusCode: 400,
  retryable: false,
  retryDelay: 51.269952198296934 } 'Unsupported: The requested configuration is currently not supported. Please check the documentation for supported configurations.\n    at Request.extractError (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\services\\ec2.js:50:35)\n    at Request.callListeners (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\sequential_executor.js:106:20)\n    at Request.emit (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\sequential_executor.js:78:10)\n    at Request.emit (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\request.js:683:14)\n    at Request.transition (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\request.js:22:10)\n    at AcceptorStateMachine.runTo (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\state_machine.js:14:12)\n    at D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\state_machine.js:26:10\n    at Request.<anonymous> (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\request.js:38:9)\n    at Request.<anonymous> (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\request.js:685:12)\n    at Request.callListeners (D:\\Workspace\\BitBucket\\Test\\node_modules\\aws-sdk\\lib\\sequential_executor.js:116:18)'

Update 1:
I changed the REGION from to 'us-est-2' and now also updated my ~/.aws/config file (before the file only contained 1 line/row: 'region=us-west-2'):
[default]
region=us-west-2
output=json

Now I get this error (when I try to decode it in the CMD) - I still don't understand what extra roles I have to give/grant my IAM-AWS user to be able read the error message?
An error occurred (AccessDenied) when calling the DecodeAuthorizationMessage operation: User: arn:aws:iam::0046xxxxxxx:user/user_name is not authorized to perform: sts:DecodeAuthorizationMessage

Comment: The doc for runInstances explains that KeyName is "The name of the key pair."
Comment: I suspect the 'Unsupported' error is because the instance type you are launching is not available in that region/AZ. Note that the t1.micro is a previous generation instance and it has been replaced by t2.micro, so please use that.
Comment: Thank you - Then it should be correct :)
Answer: You have configured your AWS region as us-east-2a. That isn't a region, that is an availability zone. Your region should be configured as us-east-2.
Comment: Thank you. But then I get the second error message instead? What it is which it doesn't support?
Answer: After I added the IAM role: "AdministratorAccess" all issues were gone :) The question is now - which role(s) are actually needed to be granted for it to work..."AdministratorAccess" was just for testing purpose.

AWS SDK and NODE JS --> UnknownEndpoint


Help text blocks show up randomly in node add/edit form with Drupal Bootstrap theme

Question: I can't get rid of this display bug.
I set a Help text for all my content-type's fields to guide my users in the node add/edit forms.
However, these text blocks seem to show up randomly in the form page. Illustration:

Yes, I can assure you that all the fields have a help text set up in the administration interface.
There are no similarities between the field's types. There are text, select lists, taxonomy term reference, etc...
I tried without the field groups (we never know). But no luck.
My config is Drupal 7.32 and my theme is based on Bootstrap 7.x-3.0.
Any idea?
EDIT:
No, they not appear in HTML source at all.
Here is a part of the source code showing one field without help text and one field with help text:

      <input id="edit-field-function-title-und-0-value" class="text-full form-control form-text required" type="text" maxlength="255" size="60" value="" name="field_function_title[und][0][value]">

      <input id="autocomplete-deluxe-input--2" class="autocomplete-deluxe-form form-control form-text jquery-once-2-processed ui-autocomplete-input autocomplete-deluxe-form-single" type="text" size="60" value="" name="field_organisme[und][textfield]" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true">

Ce champ est destiné à l'organisme, structure, société, entreprise, cause, fédération, etc. à laquelle la personne physique ou l'entité appartient et pour laquelle elle est référencée dans Grape.

Comment: I don't know the Bootstrap theme too well but the responsible party is probably `bootstrap_form_element()` in theme/system/form-element.func.php, round about line 70
Comment: If you view source is the markup for the help text there (and being hidden via css possibly), or is it missing from the markup altogether?
Comment: They not appear in the markup at all. Sorry to forget to mention that of course!!! @Clive
Comment: Indeed, if I comment some lines in this function, all my help texts appear. That's embarrassing because I don't see yet how I am going to fix that... Well, thank you very much for pointing me the source of this issue!
Answer: The solution was very simple actually. It was a config in theme administration that set all text blocks having less than 200 chars as tooltips and not as help blocks. All I had to do is disabling that config:


PHP and MySQL Loop help?

Question: Let's say I have a user that entered 12 links into the database but when they are displayed I want to break up the links into groups of 3 and repeat the groups of three until all 12 kinks are displayed for example.

Link 1

Link 2

Link 3

Here is part of the code I'm working with. How should my code look like?
while ($row = mysqli_fetch_assoc($dbc)) {
        if (!empty($row['category']) && !empty($row['url'])) {
                echo '
';
                echo '
' . $row['category'] . '
';
                echo '
';
    }

}

Answer: Something like this gets you three links per div.  We add the extra conditional echo at the end for the case that there's not a multiple of 3 links
$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
    if (!empty($row['category']) && !empty($row['url'])) {
        if ($ctr%3 == 0) {
            echo '
';
        }
        $ctr ++;
        echo '
' . $row['category'] . '</p?';
        if ($ctr%3 == 0) {
            echo '
';
        }
    }

}
if ($ctr%3 != 0) {
  echo '
';
}

Answer: if you want to avoid incrementing/resetting i every time you reach three, you can just use the modulus operator:
http://php.net/manual/en/language.operators.arithmetic.php
Something like:
if( $i%3 == 0 ){
  // divisible by 3...
}

Answer: An easier method would be more like:
while(true) {
    $c = 0;
    echo "
";
    while ($row = mysqli_fetch_assoc($dbc) && $c++ < 3) {
        if (!empty($row['category']) && !empty($row['url'])) {
            echo '
' . $row['category'] . '
';
        }
    }
    echo "
";
    if(empty($row))
        break;
}

PHP and MySQL Loop help?


Best way to handle products with hundreds of different properties

Question: I've started building a large Drupal Commerce shop for my customer. The customer has about 30000 products: clothes, electronics, decor, candies, etc. That means that in the shop there might be hundreds of different properties along all products. The products will be imported from manufacturers product feeds.
The properties are, for example:

Description
Country of origin
Materials
Product care instructions
Certificates
Composition
Paper gramage

It's quite a big job to add a new custom field for every property. Is there any better way to handle those properties?
A requirement is that most of the properties should also be filters in product category pages.
Comment: Taxonomy (tags)?
Comment: Another option is [JSON Field](https://www.drupal.org/project/json_field).  Depending on your database and search backend you may be able to include all the minor (or even major) properties in that field, and then index it with Solr, etc. to get the facets.
Comment: Interesting suggestion Patrick. I'll check it out!
Answer: It's already been said in a comment, but yes, if this information is going to be synchronized from a third party and not managed on site, you're free to using something a little less structured like taxonomy terms to import the data. Since one taxonomy term reference can reference terms from a variety of vocabularies, you can still use different vocabularies for the various groups of product attributes but then just add a single, unlimited value reference field on the product type called "Properties" that you use to import all of the properties.
You might just want to check to ensure you can build the search interface your client requires - e.g. that you can show facet blocks by vocabulary. I think you can, just make sure you test it out first before going whole hog.
Comment: Thanks for your answer Ryan! Maybe I was not so clear or then I don't understand that taxonomy solution thinking :D All the properties are in key = value form. There will be maybe 200 different property keys along all products and values differ per product.
Comment: Right, so in that case, I think the idea would be keys = the vocabulary and value = the term. If "Country of origin" is a key and "Mexico" is a value, you'd want to add the "Mexico" term to the product. My proposal would see you use a single "Product properties" term reference field on the product type that includes the "Country of origin" vocabulary in its list of allowed vocabularies. Repeat for all of the other properties.
Comment: This isn't without its display challenges, though ... for example, you may need a custom View or something to format the terms to include the vocabulary in the display, because a typical term display will just list out the terms themselves. You also may want to override the term pages if you don't want the default taxonomy term routes that show all content tagged with the term.


Calculate single absolute standardized difference across levels of a categorical treatment variable cobalt::bal.tab

Question: Can I calculate a single absolute standardized difference (ASD) across the levels of my categorical treatment variable? For reporting in a balance table.
I performed an inverse probability of treatment weighting analysis to estimate the average treatment effect of provider experience on complications after surgery. I used cobalt::bal.tab (R) to generate my balance table reporting ASD before and after adjustment. The output provides an unadjusted and adjusted difference for each level of each categorical variable.
A peer reviewer requests I report one ASD per categorical variable (not one for each level). Is there a way to calculate this by hand or does cobalt::bal.tab provide a solution I have not discovered yet?
An example of a section of my output: ('Age' is continuous and 'ASA status' categorical with 4 levels.)
Unweighted Population                      IPTW Population
Basic               Advanced        ASD    Basic            Advanced            ASD
n   100             200                    200              300
Age 34 (24.3-52.0)  38 (27.0-57.8)  0.22   34.0 (25.0-55.9) 36.2 (27.0-57.0)    0.08
ASA status
 1  70 (70)         100 (50)        0.20   120 (60)         150 (50)            0.10
 2  20 (20)         80  (40)        0.10   60  (30)         100 (33)            0.03
 3  8  (8)          40  (20)        0.12   30  (10)         50  (17)            0.07
 4  0  (0)          8   (4)         0.04   0   (0)          9   (3)             0.03

etc...
So for 'ASA status' can I calculate a single ASD across the four levels? One unadjusted and one adjusted?
Thank you for reading my question :-)
Answer: Author of cobalt here. What the reviewer is requesting doesn't really make a lot of sense. The bias in an effect estimate is a function of the mean difference of each level of the categorical variable. You could create a one-dimensional summary of balance for that categorical variable, e.g., as the maximum SMD for that variable, and then just mention the interpretation of that summary in the caption of your table. There isn't a way to do this automatically in cobalt, and it seems to me to lose important information.
I will note that there has been a value proposed as an equivalent to the SMD for categorical variables. This was proposed by Yang and Dalton (2012). It doesn't have an intuitive interpretation except vaguely as the equivalent of the SMD. It is calculated as the Mahalanobis distance between the two samples based on the categorical variable. A nice aspect of its interpretation is that for two-level variables, the formula reduces to the SMD. The bias in the effect estimate is not a function of this value, though, at it seems to me that it would be possible to have an extreme imbalance in one level of the variable that is masked by the other levels.
It is not in cobalt because the current framework wouldn't work with it (cobalt turns the supplied dataset into a numerical matrix, losing the relationship among levels of the categorical variable by turning them into dummy variables). There is another package that can be used to assess balance called tableone, which does compute this modified SMD for categorical variables. It also produces beautiful tables, much nicer than those from cobalt, the latter of which are mainly for use in balance assessment rather than reporting. You can see examples of such a table using the modified SMD on the package vignette.
This is such a minute detail for a reviewer to focus on. SMDs themselves are an arbitrary method to assess balance, so it's not clear to me why a specific method of computing a summary SMD should be preferred over another. Given that this value the reviewer wants you to compute is just a summary of the balance for each categorical variable, you could choose any summary and explain it in a caption. No one summary is superior to another. As long as you can convincingly demonstrate balance, you should be okay.
Comment: Thank you, Noah! This is fantastic feedback. In our response to reviewer, I will improve the description of the method we used to assess balance. I just tried out SMD in Tableone, but I find the differences in proportions for categorical variables reported in the cobalt package more easily interpretable. Also, that is what we actually used when we assessed balance, not the modified SMD.


When I use getRawSignature() to get the comment which has " in it, it is writing some improper string """

Question: When I use getRawSignature() to get the comment which has " in it, it is writing some improper string """. How to resolve this to get correct output? is there any alternate funtion WE have from IASTNode?
Answer: " is mojibake - almost always the result of having the unicode symbol " (U+201C, the left double quotation mark - that is not a normal quote! It is slanted), which you then convert into bytes by using UTF_8 encoding, and then you read those bytes back into a string but using some ISO-8859-X encoding. That's how you get mojibake: Take text, save in one encoding, read in another: Most non_ASCII has now turned into mojibake. You can't generally unbake this stuff, you have to get your encodings right, and read data in with the same encoding you wrote it with.
However, that is probably not the root cause here.
You've likely corrupted your source file and you pasted some source code into word, and then from word saved it, and then tried to read it with your parser. This borks your code, as word converts stuff. Such as converting "Hello" to "Hello". Which you definitely do not want. You'll have to go in and undo all the damage done by hand, get a backup, or, if you're actually writing source code in MSWord, stop doing that right away - it is not a code editor and cannot be used to write code. Use notepad++, atom, eclipse, intellij, etc.
TL;DR:

Real fix: Stop using MSWord to edit source. It mangled it beyond suitable recognition.
If somehow you really wanted this (doubtful), find all places where you convert strings implicitly to bytes or vice versa and stop ever using those - you always want the explicit ones, where you specify charset. Then, specify StandardCharsets.UTF_8. There are many such methods and you have no code pasted here so I can't tell you where you call one of these. An example is new String(byteArr) - that method is forbidden and must never be called. Call new String(byteArr, StandardCharsets.UTF_8) instead. You've got something like this earlier in your code, and that made a ticking time bomb. It went off when you invoked .getRawSignature(), but you're just seeing the bomb go off, you need to fix it where you created it.
Comment: I had updated my code with the suggestion, for UTF_8 resolved " .. but failed to convert " (closing inverted comma).. X" coming as X??
Comment: Your source file is already messed up, as it uses unicode slanted quotes instead of actual quotes. Fix it there.

When I use getRawSignature() to get the comment which has " in it, it is writing some improper string "â€œ"


Change site name programmatically

Question: We have a requirement that when we save a node that nodes title should be the site name. We used to do it in Drupal 7. But I was not able to find the proper method for it in Drupal 8.
This is what we tried:
<code>$config = \Drupal::service('config.factory')->getEditable('system.site');
$config->set('system.site.name', "Demo Site");
$config->save();
</code>
Got it from here: https://www.drupal.org/docs/8/api/configuration-api/simple-configuration-api
Any suggestions?
Comment: Where, in what hook, are you executing that code? It needs to be just `name` in  `$config->set('name', "Demo Site");`.
Answer: Nearly :
<code>$config = \Drupal::service('config.factory')->getEditable('system.site');
$config->set('name', "XXXXXXX");
$config->save();
</code>
Just have to implement hook_node_update to fit your needs.
Comment: Please don't comment your own answer. Please just update it with a little bit of explanation.
Comment: hook_node_presave would be better as hook_node_update does not run when a new node is added.

Change site name programmatically


Existe alguma forma de diminuir a quantidade de else e if?

Question: Eu deveria criar as condições para cadastro de alunos baseado na idade deles, eu queria saber se existe alguma forma de diminuir a quantidade de if nesse trecho do código:
int idade = Convert.ToInt32(txtAnoUltimoAniversario.Text) - Convert.ToInt32(txtAnoNascimento.Text);
            if (idade > 17)
            {
                lblCategoria.Text = "Adulto";
            }
            else if (idade < 13)
            {
                lblCategoria.Text = "Juvenil B";
            }
            else if (idade < 10)
            {
                lblCategoria.Text = "Juvenil A";
            }
            else if (idade < 7)
            {
                lblCategoria.Text = "Infantil B";
            }
            else if (idade <= 5)
            {
                lblCategoria.Text = "Infatil A";
            }
            else
            {
                lblCategoria.Text = "Não existe categoria";
            }

        }

Comment: Mesmo que vc possa diminuir a quantidade de `if`s, qual seria o benefício? Provavelmente a maior parte das "soluções" vai ficar pior tecnicamente.
Comment: Caro Filipe, concordo totalmente com o @Bacco , quero dizer, se realmente você tivesse umas 20 `if`s até seria razoavel tentar um `Dictionary` (ou um enumerador), porque realmente teria um problema (já que 20 é um exagero), mas com toda certeza teria uma forma uma ou mais formas **especificas** de resolver (**que vai depender** de cada código, **NÃO** tem solução global mágica), mas encurtar IFs de nada vai ajudar aqui, aliais vai diminuir até a facilidade de leitura, o que deveria se preocupar. Tem gente que pensa em diminuir ifs e criar classes megazords, o que é pior ainda.
Comment: Não tem erro aí? Porque tem idades no meio que ficam sem ter categoria. Primeiro precisa ter a lógica correta, porque isso pode mudar otimizações.
Comment: Já pensou em usar `switch/case`?
Comment: tem erro também na lógica, se a idade for 5 entra logo no `if` do `< 13`
Comment: Caro Filipe, não tem haver com o assunto, mas a ordem das ifs com certeza esta errada, qualquer valor menor que 13, mesmo que idade seja `1`, vai sempre cair em `else if (idade < 13)`, pode fazer o teste sentado o valor manualmente, sempre vai ser `Juvenil B`, mesmo que colocasse 5 anos. Veja o teste online da sua IF: https://ideone.com/cMZgoO
Comment: é verdade, a lógica está toda bagunçada, agora que parei para olhar, vou tentar consertar isso primeiro.
Comment: @Filipe.C Alguma das respostas resolveu sua dúvida? Acha que pode aceitar uma delas? Veja o [tour] como fazer isso, se ainda não o fez. Você ajudaria a comunidade identificando qual foi a melhor solução para você. Pode aceitar apenas uma delas. Mas pode votar em qualquer pergunta ou resposta que achar útil no site todo
Comment: @Filipe.C Com o C# 9.0, você, pode reduzir os ifs, com o novo formato de switch statements. https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/operators/switch-expression
Answer: Até antes da versão 8 em essência não dá (veja abaixo que fica bem melhor na versões mais novas), pelo menos não de uma forma que valha a pena. É possível encurtar o código e é possível consertar um problema quando o dado é digitado errado que não está contemplado aí. Também acho que tem um problema de lógica nesta comparação toda, mas não pode afirmar.
if (!int.TryParse(txtAnoUltimoAniversario.Text, out var aniversario) || !int.TryParse(txtAnoNascimento.Text, out var ano) {
    //faz alguma coisa aqui para indicar que os dados foram digitados errados
    //Pode ser que seja dizer que não tem categoria, precisa pensar na UX
}
var idade = aniversario - ano;
if (idade < 6) lblCategoria.Text = "Infatil A";
else if (idade < 7) lblCategoria.Text = "Infantil B";
else if (idade < 10) lblCategoria.Text = "Juvenil A";
else if (idade < 13) lblCategoria.Text = "Juvenil B";
else if (idade > 17) lblCategoria.Text = "Adulto"; //e os que estão entre 13 e 17 não existem?
else lblCategoria.Text = "Não existe categoria";

Eu pensei em usar um dicionário, mas eu não ia prejudicar a performance para fazer algo que teria uma quantidade de linhas maior que a solução direta. Se ela tivesse reduzido bastante a quantidade de linhas talvez eu faria, mas não neste caso, inclusive porque pode haver exceções no futuro que o dicionário nem funcione.
O que dá para fazer é encurtar um pouco mais o código colocando uma variável de nome curto com as strings e aí atribuí-la na label.
Fiz um código ligeiramente diferente para testar:
using static System.Console;

public class Program {
    public static void Main() {
        Compara(20, 15);
        Compara(20, 14);
        Compara(20, 11);
        Compara(20, 8);
        Compara(20, 2);
    }
    private static void Compara(int aniversario, int ano) {
        var idade = aniversario - ano;
        string texto;
        if (idade < 6) texto = "Infatil A";
        else if (idade < 7) texto = "Infantil B";
        else if (idade < 10) texto = "Juvenil A";
        else if (idade < 13) texto = "Juvenil B";
        else if (idade > 17) texto = "Adulto"; //e os que estão entre 13 e 17 não existem?
        else texto = "Não existe categoria";
        WriteLine(texto);
    }
}

Veja funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.
Um switch deixaria até maior, mas em C# 8 tem uma sintaxe que pode ser útil usar o switch, com 9 melhor ainda:
using static System.Console;

public class Program {
    public static void Main() {
        Compara(20, 15);
        Compara(20, 14);
        Compara(20, 11);
        Compara(20, 8);
        Compara(20, 2);
    }
    private static void Compara(int aniversario, int ano) => WriteLine((aniversario - ano) switch {
            < 6 => "Infatil A",
            < 7 => "Infantil B",
            < 10 => "Juvenil A",
            < 13 => "Juvenil B",
            > 17 => "Adulto",
            _ =>  "Não existe categoria",
        });
}

Veja funcionando no .NET Fiddle. Também coloquei no GitHub para referência futura.
Mais curto e mais eficiente que a opção de dicionário.
Answer: Pode usar um dicionario como alternativa:
var idade = 12;

var dic = new Dictionary<int, string>
{
    { 6, "Infatil A" },
    { 7, "Infatil B" },
    { 10, "Juvenil A" },
    { 13, "Juvenil B" },
    { 18, "Não existe categoria" },
    { int.MaxValue, "Adulto" },
};

var result = dic.OrderBy(k => k.Key).FirstOrDefault(e => idade < e.Key).Value;


message.author.dmChannel.awaitMessages not working

Question: I have a setup command where the setup takes place in DMs. Sending the first message works but whenever I dm the bot it doesn't detect my messages.
Code:
const filter = m => m.content;
message.author.dmChannel.awaitMessages(filter, {
  max: 1,
  time: 60000,
  errors: ["time"]
}).then(collected => {
  collected.reply("message collected message");
}.catch(() => message.author.send("ran out of time message")

When the time runs out it dms me the ran out of time message
I also tried this but it also didn't work
const filter = m => m.content;
message.author.dmChannel.awaitMessages({
  filter,
  max: 1,
  time: 60000,
  errors: ["time"]
}).then(collected => {
  collected.reply("message collected message");
}.catch(() => message.author.send("ran out of time message")

Comment: What version of discord.js are you using?
Comment: I use discord.js v13 and Node 16.1.0
Answer: TextChannel.awaitMessages() now only takes 1 argument. This includes the filter property in the object. Changing it to this will work:
const filter = m => m.content;
message.author.dmChannel.awaitMessages({
  filter,
  max: 1,
  time: 60000,
  errors: ["time"]
})

Comment: I already tried that, it doesn't work


What are the implications of not having explicit static private IP for Windows Server 2012?

Question: In more details:
We have 2 Windows Servers 2012 running SQL Server Web on Amazon EC2 in the same VPC.
Network adapters are configured with static private IP-s via AWS console.
At Widows level, however when you go to Network Adapter Properties/IPV4/Properties it shows:
Obtain an IP address automatically
Obtain DNS server automatically
So what happens is that each time network adapters receive the same IP dynamically (for instance, 10.0.0.20 for one server and 10.0.0.21 for another).
Are there any implications of having Windows Server (or SQL Server) configured that way?
Should I in addition to assignment via Amazon AWS also set the same static IP for network adapter at Windows level?
Or should I set it to dynamic as AWS level and assign static addresses at Windows level?
Answer: You always want to use DHCP in EC2. If you use the <code>--private-ip</code> option when creating your instances, your instances will always get the IP address.
Comment: To clarify: you recommend leave everything at Windows OS level as-is ("Obtain an IP address automatically" and "Obtain DNS server automatically"), correct?
Comment: Yes, that's correct.


In .Net does "using" do what we're led to believe it does?

Question: I was reading a forum recently, and saw this comment:

So, you see you've been duped into
  believing that the 'using' syntax is
  going to help clean up your resources
  for you.  Oh well, welcome to .NET
  development.

This has really freaked me out!  I use using almost exclusively over .Dispose() in nearly all cases (streams especially).  Is there some weird thing with "using" that no one has told me?  Is it good or bad or indifferent to use?
Comment: Never assume someone is correct just because they think they are.
Answer: From MSDN:
using (Font font1 = new Font("Arial", 10.0f))
{
    byte charset = font1.GdiCharSet;
}

is translated into the following by the compiler:
{
  Font font1 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet;
  }
  finally
  {
    if (font1 != null)
      ((IDisposable)font1).Dispose();
  }
}

so not sure what you're worrying about.
Edit: Be warned though, if you are using the using pattern on your own types, the pattern will only be as effective as your Dispose() method. Some of the comments and other answers this.
Comment: How could the cast fail?  C# won't let you compile a using() statement wrapping a variable that doesn't implement IDisposable.
Comment: Yes, but using is only as good as your Dispose.  If your object allocates non-managed resources, but does not handle them properly in Dispose, you can still have a problem.
Comment: I agree, I think it does exactly what we're lead to believe it does.
Comment: Huh, I thought they'd changed the 'finally' code in the pattern, because that could could throw an exception if the cast failed, and that it looked more like this: var disp = font1 as IDisposable; if (disp != null) disp.Dispose();
Comment: Yeah, that's excellent - I was very sure it was a-ok - I guess I was just frightened by the comment - I guess I overreacted due to not feeling too well today.  Thanks for clearing this up guys.
Comment: JP: You bring up an incredibly important point. I edited the answer to reflect this.
Answer: There are some cases where using will not clean up as you might expect, such as:
using (var myObject = new DisposableObject().AnotherDisposable())
{
    //Do things to myObject
}

In this example you have two objects, DisposableObject and AnotherDisposable and the using will only clean up AnotherDisposable and leave DisposableObject leaking.
Comment: Eh: I take that as a code smell, in that you shouldn't really create a disposable object without disposing it.  At best, it's misleading.  This would be better, because it will get both: using (var parent = new DisposableObject(), myObject = parent.AnotherDisposable()) {}
Comment: Interesting example.  Technically the using statement is still perfoming as expected -- although it's pretty clear it's not what the desired.

I'm not sure how they could have accounted for that in the design of the using statemen.
Simple rule: one using block one Dispose method call no more no less.
Comment: I agree, using is working as specified and there is no way to really account for it, but it is something to be aware of!
Comment: I agree with Joel....that's a desperate example and should be avoided by its code smell, else one deserves the result.
Answer: It would help if you could provide us with a link to the context; but, generally, that kind of mostly-attitude opinion without backup should be taken with a healthy grain of salt.
Answer: Yes, it does do what you'd expect it to do. If you have a look at the IL of a "using" statement this is what you get:
L_0007: newobj instance void Demo.SomeClass::.ctor()
L_000c: stloc.0
L_000d: nop
...
L_0040: nop
L_0041: nop
L_0042: leave.s L_0054
L_0044: ldloc.0
L_0045: ldnull
L_0046: ceq
L_0048: stloc.2
L_0049: ldloc.2
L_004a: brtrue.s L_0053
L_004c: ldloc.0
L_004d: callvirt instance void [mscorlib]System.IDisposable::Dispose()

(I stipped out the stuff from within my using statement to condense a bit, but you get the idea)
Answer: 'Using' is a pattern.  Sometimes your code flows with the pattern and sometimes it doesn't.  Meaning that sometimes you have to actually call dispose because your object doesn't fit the 'using' pattern's scope.
As an example, if you instance an object upon application startup and then need to dispose of it in the middle of the application and instance a new one, chances are you can't use 'using.'
Answer: It will clean up any resources that the Dispose() method cleans up, because the using(){} construct is just syntactic sugar for calling the Dispose() method.
Answer: I take it you mean this thread? (thank you google):
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/4b63dc82-c7c0-4495-97ca-d92502a33d15
The using statement will call .Dispose() on any object that implements IDisposable.  If the using statement failed to clean up the resources, a call to Dispose() directly would have failed as well.  In the specific case that I linked to, it sounds like a mis-behaving virus checker was the problem.
Answer: Using is just another way of ensuring the Dispose method is called on your object.  As far as any resources not handled by the Dispose method, they will remain for garbage collection so you are still at the mercy of the object you declare in your using statement.  I am obviously reading into your comment since you did not post a link to the article you are referring to.
Answer: The end of the using block calls Dispose.  Dispose provides the opportunity for the caller to explicitly free unmanaged resources that your object is using.  The garbage collector will implicitly free managed resources using the object's Finalizer.  But the GC knows nothing about unmanaged resources that you have allocated (file handles, window handles).  So, if you allocate unmanaged resources and don't free them properly in your Dispose correctly, you will leak.  Using is only as good as your Dispose.
There's much more on garbage collection here and more on Finalize v.s. Dispose in this SO thread.
Answer: I have seen times where a file was still left open when the application crashed because the using construct was used.  It seems that closing connections is not always done when using this.  So, when I open files or database connections I explicitly use the try..finally myself.
Comment: If the application crashed in the middle of the using() block, a try...finally around the same area wouldn't have caught it either.
Comment: mquander is right. Again, once compiled, using(){} becomes a try...finally. A crash is a crash; if the crash is severe enough, your finally clause won't execute either. Remember: Microsoft only "guarantees" that finally will execute when there aren't severe conditions (EG, out of memory, hard system failure, etc).
Comment: When I had the same condition with the try..finally the problem went away. I don't remember what the error was, I was doing this under WindowsCE 5.0.


Acessar state do React Native Scrollable Tab View

Question: Tenho uma aplicação React Native com a biblioteca react-native-scrollable-tab-view, onde essa tab view contém 3 nós-filhos:
App.js
<ScrollableTabView
          style={{marginTop: 0, backgroundColor: '#1C4747', }}
          tabBarUnderlineStyle={{backgroundColor: "#fff"}}
          tabBarActiveTextColor={"#fff"}
          tabBarInactiveTextColor={"#ccc"}
          initialPage={0}
          renderTabBar={() => <DefaultTabBar />}
        >
          <RotaEntrega tabLabel='ROTA' />
          <ChildOne tabLabel='ORDENS' text='ORDENS' size={17} weight="500" />
          <ChildTwo tabLabel='HISTÓRICO' text='HISTÓRICO' size={17} weight="500" />

</ScrollableTabView>

Em RotaEntrega.js há uma requisição HTTP que retorna um json do servidor. Porém, me dei conta que essa mesma requisição é usada nos outros dois nós (ChildOne e ChildTwo), então eu teria que migrar a requisição para o App.js e usar o resultado nos tabs do ScrollableTabView. Posso passar esses dados usando props, mas os tabs filhos teriam que modificar o state em App.js...
Em resumo:
App.js faz requisição HTTP e obtém JSON.
Salva JSON no state.
Passa o state para os tabs RotaEntrega, ChildOne, ChildTwo.
Os tabs lêem e modificam o state de App.js.
Atualmente a requisição está RotaEntrega.js:
_route = async(codDriver) => {
    const apiCall = await fetch(`http://urlaqui.com.br/testes/api.php?driver=${codDriver}`);
    const response = await apiCall.json();

    let latLongObj = {
      latitude: parseFloat(response.os[0].address.latitude),
      longitude: parseFloat(response.os[0].address.longitude)
    };

    this.setState({
      route: response.success ? [ response.os[0] ] : [], //only first route in the list
      routeFound: response.success,
      orders: response.success ? response.os : [], //and put the all orders to this array
      destination: latLongObj
    });
  };

  componentDidMount(){
    this._route("51ds515");
  }

Logo o código acima passará para App.js.
Como posso implementar isso?
Answer: Uma resolução seria trabalhar com funções vinculadas ao Objeto Instanciado( O Detentor do Estado do App.js: Bind ou Arrow Functions ).
Você implementa funções em AppJS que alterem o estado do nó pai e passa
essas funções para os nós filhos.
Exemplo:
App.js
 //...
 class App exetends React.Component {

   _request(){ // ... }

   updateState = (args) => {if(args){this.setState(args);}}

   render(){
     return (
       <ScrollableTabView
         style={{marginTop: 0, backgroundColor: '#1C4747', }}
         tabBarUnderlineStyle={{backgroundColor: "#fff"}}
         tabBarActiveTextColor={"#fff"}
         tabBarInactiveTextColor={"#ccc"}
         initialPage={0}
         renderTabBar={() => <DefaultTabBar />}
       >
         <RotaEntrega tabLabel='ROTA' updateState={this.updateState} />
         <ChildOne tabLabel='ORDENS' text='ORDENS' size={17} weight="500"  updateState={this.updateState}/>
         <ChildTwo tabLabel='HISTÓRICO' text='HISTÓRICO' size={17} weight="500" updateState={this.updateState} />

       </ScrollableTabView>
     );

 }

Nessa pegada, toda vida que for chamado o updateState nos nós filhos, você atualiza o estado do nó pai a partir dos filhos.
Em Resumo:
Implemente métodos para alteração do estado e passe a função por props.
Lembre-se de vincular a função a instância, usando bind ou arrow-functions para alterar o estado do nó pai.
Comment: Show, vou testar daqui a pouco, obrigado pela ajuda!


R - Plot multiple regression line with confidence intervals with ggplot2

Question: Somewhat newbie question here. I'd like to use ggplot2 to plot my multiple regression model, but I ran into an error. I did some research on the forum before deciding to ask the community.
I understand how to plot a simple linear regression:
ggplot(data, aes(x=X, y=Y))+geom_point()+
  geom_smooth(method='lm',formula=Y~X)

But when I tried to do it for multiple regression:
Model<-lm(Y~x1*x2*x3*x4*x5, data, na.action=na.omit)

ggplot(data, aes(x=X, y=Model))+geom_point()+
  geom_smooth(method='lm',formula=Y~x1*x2*x3*x4*x5)

I get this error:
Don't know how to automatically pick scale for object of type lm. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (209): x, y

Comment: Questions about *programming, debugging, or performing routine operations within a statistical computing platform* are [off-topic](http://stats.stackexchange.com/help/on-topic) here. You can find several online tutorials/courses about how to use`ggplot2` package. Here is one example: http://r-statistics.co/ggplot2-Tutorial-With-R.html . You might also want to check *ggplot2: Elegant Graphics for Data Analysis* by Hadley Wickham. If you check these sources you can see that in `aes()` you need to use variables.
Comment: If you want to learn about plotting multiple regression model, check this question: http://stats.stackexchange.com/questions/73320/how-to-visualize-a-fitted-multiple-regression-model and this one: http://stats.stackexchange.com/questions/89747/how-to-describe-or-visualize-a-multiple-linear-regression-model
Comment: You can't pass a model to ggplot2 like this.
Answer: As the comments state, there are various answers to this, but two points of a more statistical or statistical thought nature:

This is probably not the model you want to use. y~x1*x2*x3*x4 is like a model which translates to a quadruple-interaction with all possible sub-interaction (triple and double) which is hardly legible at the best of times. So unless you want a non-interaction model, in which case the * should be replaced with a +, you might want to think if the theoretical reasoning behind this kind of interaction is warranted. Sometime we want to cover our bases and create the most complex model we can with the data, but it will be difficult to interpret.
I can't begin to imagine how a model with five predictors can be plotted, let alone with multiple interactions. Simple regression? Two dimensional plot. Two predictors? 3D plot (which in social sciences is rare to see). Three, four, five predictors? No idea how to plot together, and probably neither does ggplot. In ggplot, geom_smooth() is a line parameter, and hence needs to work with axis. Y is Y, X1 is X etc.. it just doesn't know what to do.
Generally speaking, a plot is used for simplifying results. If it can't do that, it does not have much use.


Bulk replacing div's content for hundreds of html files before upload

Question: I have a website with hundreds of static html files. I need to replace the facebook comments with disqus comments.
Here is what the divs that hold the comments look like now:

I need to replace every div of the class "fb-comments" with another div of the form:

Please enable JavaScript to view the comments powered by Disqus.

with PAGE_URL being the url that was listed in the "fb-comments" div.
Anybody have an idea of how to do this?
Most text editors I've looked at only allow you to find and replace, which won't work since the url in the fb-comments div are different for each html page.
Answer:
It is frowned upon to do that with regular expression.

If there was no option, this expression might do that with a preg_replace in PHP for instance:
$re = '/<div class="fb-comments".*?<\/div>/s';
$str = '

';
$subst = '

Please enable JavaScript to view the comments powered by Disqus.';

echo preg_replace($re, $subst, $str);

Demo


What is the difference between univariate and multivariate time series?

Question: I have the following time series data:
index              Time        value_1  value_2  value_3
 0        2016-04-01 06:00:10     1       5         2
 1        2016-04-01 06:00:20     2       9         8
 2        2016-04-01 06:00:30     3       5         1
 3        2016-04-01 06:00:40     4       4         4
 4        2016-04-01 06:00:50     3       4         5
 6        2016-04-01 06:01:00     4       3         2

Is this a univariate or a multivariate time series? What is the difference between univariate and multivariate time series?
Answer: Univariate time series: Only one variable is varying over time. For example, data collected from a sensor measuring the temperature of a room every second. Therefore, each second, you will only have a one-dimensional value, which is the temperature.
Multivariate time series: Multiple variables are varying over time. For example, a tri-axial accelerometer. There are three accelerations, one for each axis (x,y,z) and they vary simultaneously over time.
Considering the data you showed in the question, you are dealing with a multivariate time series, where value_1, value_2 andvalue_3 are three variables changing simultaneously over time.


Direct Upload to Cloudinary from Browser Bad Request

Question: i'm getting a 400 bad request error when trying to upload to cloudinary with the following code:
$("input.cloudinary-fileupload[type=file]").cloudinary_fileupload();
$.cloudinary.config({"api_key":"6586856745648955","cloud_name":"some-cloud"});

  $http.get("http://localhost:3000/story/secret")
  .then(function(res){
    var CLOUD_API_SECRET = res.data.CLOUD_API_SECRET;
    var obj =
    {
      "timestamp":  Date.now(),
      "callback": "http://localhost:3000/cloudinary_cors",
      "signature": CLOUD_API_SECRET,
      "api_key": "6586856745648955"
    };
    // var data = JSON.stringify(obj);
    $("input[type='file']").attr('data-form-data', obj);
  })
  .catch(function(err){
    console.log("error: ", err);
  });

with my front containing the following:
<input name="file" type="file"
    class="cloudinary-fileupload" data-cloudinary-field="image_upload"
    data-form-data=" ... html-escaped JSON data ... " >
</input>

i've also tried to stringify, then encode the "obj" variable and plug that in like so:
var data = JSON.stringify(obj);
$("input[type='file']").attr('data-form-data', encodeURI(data));

i get the same error.
i'd appreciate any help or suggestions. thanks a bunch.
Answer: There are a few issues here:

Your account's api_secret should never be revealed in your client-side code.
The signature isn't the api_secret, you should generate a signature which is based on both your api_secret and the upload options. For more information: http://cloudinary.com/documentation/upload_images#request_authentication.
The signature generation must be done on a server-side.
Cloudinary expects the signature to be based on the timestamp by seconds and not milliseconds, so the last 3 digits are not necessary. Therefore, I'd recommend you to divide the timestamp by 1000 to omit the last 3 digits (e.g., Date.now()/1000)
Comment: ohhhh. i see. wow. i was doing that all wrong. thanks for clarifying. i just wound up going with the node implementation, after all. it was a lot easier for me to understand. i appreciate your answer!


Steps after installing vCenter Server?

Question: I'm working with:

Two new ESX servers that I'm configuring
A new Server 2008 R2 machine that I'm using for vCenter.

I took the following steps:

Installed the Hypervisor on the 2 ESX machines
Checked their setup/connectivity (appears to be fine; can ping, etc.)
Installed vCenter Server on the Win2k8R2 box. This included the install of a SQL Express database (we're a small shop)

FYI, I changed some of the ports (443 --> 8443, 80 -->8080, etc.)

Installed vCenter Web Client Server on the Win2k8R2 box

Problems

my vSphere Client on my Desktop fails to connect. Part of this is that it asks me for a username and password, but I don't recall specifying one when I set up the install.

I receive the error "vSphere Client could not connect to [machinename]. An unknown connection error occurred. (The request failed because of a connection failure. (Unable to connect to the remote server))"
I have also tried to use local machine admin credentials, including the format machinename\localuseracct.
I have also tried using my domain credentials which are an admin for that box.
I have also checked and the service is running.
I also tried to connect via vSphere client locally installed on the server. It translates "localhost" to the correct name but gives the same error.

I cannot register the vCenter server from the vCenter Web Client Server. I'm not sure if this is necessary, as they're both on the same machine, but it seems like the logical next step. I also receive a "failed to connect" error in this case as well.

FYI, both the vCenter server and the vCenter Web Client Server are installed on the same Win2k8R2 server.

What am I missing here? What is the best way to test in this case?
Comment: Have you had any training? Most courses cover these topics straight away and would prove an invaluable basis for evaluating and implementing VMware products.
Comment: well if you've changed the default ports then those ports need to be unblocked...
Comment: you've checked that the windows firewall isn't blocking the connections, right?
Comment: @Chopper3, Unfortunately no, other than beginning to pour over the guides just now.
Comment: @RobM, Windows Firewall is turned off but we've got Symantec Endpoint Protection on there and that's a fair point. I'm in the process of getting the domain admins to check that out (nothing in the logs but can't be sure).
Comment: Rob, as far as I can tell the only ports I've changed were on the server from 80 -->8080 and 443 --> 8443.  I can access those via a web browser on my client machine fine.
Answer: Well, this is embarrassing. Answering here so that someone else may find an answer, but I'm not proud of this one. :)
The solution was a simple one: Due to changing the port, neglected to also append the port to the login box in the vSphere Client.
So, updating from [machinename] to [machinename]:8443 worked just fine.
Thanks to RobM for starting me down the path of realizing I'd overlooked something basic.
Comment: Don't be embarrassed, these sorts of errors can catch anyone!

Steps after installing vCenter Server?


How can I resolve this iCloud Error?

Question: Hi I am trying to get my core data app syncing to iCloud so that multiple devices can be used.
I just started a new iCloud account for testing and installed my app on both devices. I have enabled my app for iCloud using instructions at this link Using CoreData with icloud and by also comparing my app to apple's coreDataRecipes example code.
I was not getting any syncing occurring so I did some logs to find out what was occurring. After adding an object to core data from iPad A i received the following log on iPad B which was connected to the computer.
2012-01-20 17:35:35.979 My-App[407:22d7] AppDelegate Merge Changes From iCloud Notifications
2012-01-20 17:35:35.980 My-App[407:22d7] AppDelegate managed Object Context
2012-01-20 17:35:35.981 My-App[407:707] Block redirecting to method merge icloud changes for context
2012-01-20 17:35:35.981 My-App[407:707] AppDelegate Merge icloud changes for context

Then I get this error
2012-01-20 17:10:48.030 MyApp[407:4c0b] -[PFUbiquityBaseline loadBaselineArchiveWithError:](347): CoreData: Ubiquity:  <PFUbiquityBaseline: 0x387e80>
ubiquityRootLocation: (null)
storeName: (null)
modelVersionHash: (null)
baselineArchiveLocation: (null)

Coordinated read of baseline archive file: (null) did not succed: (null)
2012-01-20 17:10:48.030 MyApp[407:4c0b] -[PFUbiquityBaselineRecoveryOperation resolveConflictsForBaseline:withError:](186): CoreData: Ubiquity:  Baseline conflict resolution had trouble initializing a conflicting baseline version: <PFUbiquityBaseline: 0x387e80>
ubiquityRootLocation: (null)
storeName: (null)
modelVersionHash: (null)
baselineArchiveLocation: (null)

Error: (null)
2012-01-20 17:10:48.031 MyApp[407:4c0b] -[PFUbiquityBaselineRecoveryOperation main](363): CoreData: Ubiquity:  Unable to resolve conflicts for baseline: <PFUbiquityBaseline: 0x1e35a0>
ubiquityRootLocation: <PFUbiquityLocation: 0x1f2570>: /private/var/mobile/Library/Mobile Documents/MYINDIVIDUALID~MyCompany~My_App/invoices
storeName: My-App.store
modelVersionHash: ~4YoLQdnNoouIPvgFerSqmhFoHPk97Clf3AK_rtWHiY=
baselineArchiveLocation: <PFUbiquityLocation: 0x1e1470>: /private/var/mobile/Library/Mobile Documents/MYINDIVIDUALID~MyCompany~My_App/invoices/.baseline/My-App.store/~4YoLQdnNoouIPvgFerSqmhFoHPk97Clf3AK_rtWHiY=/baseline.zip
, the baseline will not be adopted.

It would be greatly appreciated if someone could let me know why I am getting a conflict due to the (null) values and where I can look to resolve this.
Thanks
ps. I have changed the seen in the log app name, unique identifier and company name just for this post.
EDIT
Syncing is occurring however it seems to be delayed.
Comment: have u found the ans , if yes then plz update
Comment: some news on this issue, i have it on iOS5.1
Answer: @coder007 I submitted a question to apple about this. They wrote back and said that I should submit a bug report and they credited my question back to me. To be honest I haven't yet had the time submit the bug report, so if you can then it may be the way to go. It must be a problem with iCloud that needs ironing out
Update
I Don't have this error anymore, I think apple is gradually improving and eliminating issues with iCloud. I don't think it is a problem on the developers end in terms of coding from my perspective.

How can I resolve this iCloud Error?


/usr/local and subfolders are not recognized by my Java program

Question: I have got a 64-bit Linux Mint 20.1 OS and I realized that my arbitrary Java program does not recognize "/usr/local" or anything in it as an existing entity. To clarify what I mean, I have stated a couple of examples below:
System.out.println(new File("/usr").exists()); //prints true

System.out.println(new File("/usr/local").exists()); //prints false

System.out.println(new File("/usr/local/bin").exists()); //prints false

System.out.println(new File("/usr/local/random-file-1").exists());
//prints false, regardless of whether that file exists

I have checked the permissions of /usr/local, supposing that something might be amiss there, but oddly, I haven't found anything wrong:

Could you give your two cents on what could go pear-shaped?
Answer: Has nothing to do with java and everything to do with your linux installation:
Option 1: No Unix UGO x rights on this dir.
The process is not capable of seeing into the /usr directory. Most likely, the process runs as user X, which is in groups [Y] (can be in multiple!).
The /usr directory is surely not owned by X (on all relevant unix distros, it is owned by root). It's also likely that /usr's group owner is anything in the [Y] set either; on most linuxen it is group root, and no users are part of this group except root itself.
Thus, the last 3 letters of the access rights apply (it's user/group/others). So, run ls -lah / and check the access rights of /usr. I expect:
drwxr-xr--  10 root root 4.0K Jan 1   2021 usr

Note that r-- at the end. no x means no rights to even look in it. That would explain what you're witnessing.
Note that this has nothing to do with the access rights of /usr/local! To read dirs you need access to the entire 'stack', and in general the dir that blocks you is still, itself, visible to you - it's just that you can't delete or move it, can't run or open anything in it, and can't even ls it. Thus, the fact that you can't even 'see' /usr/local suggests that, if it's UGO access at all, it's not /usr/local, but /usr or /.
Option 2: ACL overlay rights
Some distros (notably, mac OS X) add a layer of access rights on top of the standard unixy user/group + UGO rights system. For example, in mac OS, you need to go to the system preferences pane and go to 'privacy', and add some disk acces rights here. I'm not sure if mint has a similar system. Probably not, especially for /usr which is a rather important one.
Option 3: Weird stuff
You're using outdated java APIs that you shouldn't be using anymore. One of the downsides of this API is that it cannot give you information about stuff, it can only return booleans. So, try with the new API and if you get an error, because if you do, it'll help A LOT more: It'll have a lot of text to explain it to you:
(First check that /usr/bin/man exists; it should. If not, pick any other existing thing you find there).
import java.nio.file.*;

class Test {
    public static void main(String[] args) throws Exception {
        Path p = Paths.get("/usr/bin/man"); // I assume this exists.
        byte[] b = Files.readAllBytes(p);
        System.out.println("size of man exe: " + b.length);
    }
}

This will throw something (or if it does not, hey, something else is going wrong because this test app is having no problem with it!) - the text of that exception will tell you something useful.
Option 4: Uhoh
Your disk is corrupt. Let's just hope this isn't it :P


What is C++ preamble, and pre-post condition for get function

Question: I am beginner at C++, the homework ask us to document our code (preamble, and with the function's pre-post conditions). I am wondering how do they apply to my Student class?
In particular, what exactly is preamble mean? And for my get methods, what are the pre and post conditions? I have a feeling they are really for more advance's function (these who changes the value of the data)?
class Student:
{
public:
    // Constructor for the student class, require 4 parameters
    // In the order of string (first name), string (last name),
    // integer (student ID), string (major)
    Student(string myFirstName, string myLastName, int myID, string myMajor)
    {
        firstName = myFirstName;
        lastName = myLastName;
        uid = myID;
        major = myMajor;
    }

    // Get function for the student's first name
    // Return the student's first name
    string getFirstName()
    {
        return firstName;
    }
    .....

private:
    string firstName; // Student's first name
    string lastName; // Student's last name
    int uid; // Student's ID number in a number format
    string major; // Student's major
};

Comment: Preamble is understoud as the constructor initializer list and means written out "Preliminary Material", my personal suggestion to homework, ask the guys who teach it, there is nothing better than letting them explain it directly to you, they might have a more specific description and only from them you can source the expectations for delivering results
Comment: That's a sign of a bad teacher. Solution: Get a different teacher.
Comment: @Casey, I think that a teacher is entitled to some vacation. Are you happy when your boss complains that you are not responding to e-mail when on vacation?
Comment: About your other question about the post conditions and pre conditions of your "get" methods, pre condition is that they have been initialized, if you add another "Constructor" with no arguments but blank initialization you can eliminate the pre condition, i have found a good link for you while googling for it, [Some Random PPT](http://www.cs.colorado.edu/~main/supplements/chapt01.ppt)
Comment: It is spring break, the teacher doesn't want to answer my emails :( (he is off I suppose)...
Comment: Currently the Class description is missing, just as side note, if you become a coder i recommend to you to read alot, it is really the key to "not waste time" some of our old school programmers call this the "RTFM" ;-)
Comment: @Hiemstra: It's a sign if the teacher assigns work over spring break and then refuses to do any work themselves, e.g. answer questions, grade papers, etc.
Answer: preamble is a source code comment appearing before the function that documents its behaviour, normally in a way that a tool like doxygen can automatically create useful documentation from it.
A good policy for documentation is to document everything that isn't obvious and nothing that is.  On that basis, I'd say your getXXX() have no pre- or post-conditions worth documenting.  Something like "must be called on a properly constructed object" is just a waste of the reader's time, as is a post-condition like "the caller will have received a copy of the student-specific XXX data".  Don't do it!  The question of whether the values can legally be empty strings is one for the constructor (or may even be a class invariant - something the class maintains as true, for instance by having the constructor throw if a value is empty and by not providing functions that wipe out the values), not for the get function documentation.
Similarly, your comments for firstName, lastName and major add absolutely no value, but your comment for uid does add value - documenting a requirement for a string format!  From that I can tell you've made a mistake, as the field is an int, and there's something to investigate and correct.  But more generally, say you had a string type - a comment might usefully communicate some restrictions on the format of the id, say that these ids relate to some other API or source, give an example etc.
More generally, pre-conditions aren't only for mutator (data changing) functions.  For example, a function day_for_date(int year, int month, int day) that tells you what day of the week a particular date falls on may have a pre-condition that year/month/day does actually describe say a valid date between 1000 and 3000 A.D..  That's nasty, but it means if you've already checked that somewhere the function call doesn't waste time re-verifying it.  In Defensive Programming style (which IMHO tends to make for more robust code) you would tend not to make that a precondition; instead, accept redundant verification as a net win (though you may provide explicit options to disable it if the caller needs the performance), and document an exception or error outcome should the date not meet expectations.  The difference is that a precondition is something the caller must guarantee for the function to operate as documented, where-as in defensive programming you generally let the caller try something and have a defined/documented manageable behaviour - a change in state, return value or error reported in a predictable way - for as broad a range of prior states and inputs as is practical.
Post-conditions only make sense for mutators: whatever could be asserted about the state after the call to a non-mutating accessor must have been true beforehand - it might be a class invariant.
Comment: OliverStutz: I agree the teacher may expect that, but definitely it's not a good habit and has no place in professional standard code.  George: you're welcome!  @Ed: when someone comes back years later, it's even more important that they can quickly understand the functionality - 100% redundant comments just waste the time they could be spending on the tricky bits.  As George says, picking good variable names goes a long way to making code self documenting; of course good function and type names are equally important.
Comment: I would recommend to also make note of the obvious, at the beginning teachers tend to get the scope of the student, excluding stuff will give the impression that he/she is overlooking the simple things but your answer is pretty good, better than serving him everything on a plate
Comment: It is meant to be an int, I actually wrote the wrong comment :( Really careless. But I sort of agree with you, since the variable name is about as clear they can get. I don't know what is there to really write (as comment).
Comment: I think I sort of get what you mean in the pre-condition and post-condition meaning. Thank you.
Comment: I would recommend documenting every class and method. They add comments to give a commentary on the rest of the code.That way when you come back in a years time, something that you now find obvious might not appear to be obvious later on to to different people.
Comment: @OliverStutz - I agree that chosing good variable names is a very good idea. But in the future a little commentary with the code helps. It does not take long and I find it helps the thought process as well.
Comment: @JamesKanze - I write both at the same time. Perhaps my english could have been a little better.
Answer: If this is homework, I strongly suggest you ask this question in class as most teachers have some particular requirements that an online community is unlikely to know about (unless you tell us ;-).  That being said, we might be able to get you started in the right direction.
The preamble is usually a (short) description of what a particular file, class or function does.
The pre-conditions are the requirements on the arguments passed by the caller, as well as the program state prior to the call.  They establish under what conditions the function will produce the correct results.  For instance, you may request that the first and last names in your example class constructor are non-empty.
A post-condition usually documents side-effects applied by a function.  For instance,
class Person
{
public:
    std::string firstName () const;

    // post-condition: firstName() returns 'name'.
    void setFirstName (std::string name);
};

The wikipedia articles on pre-conditions and post-conditions provide more elaborate descriptions.
Comment: In general, the preamble to something is just a descriptive piece of text that precedes the described item.  In the case of a C++ function, that would be a comment that describes the function's role or purpose.
Comment: The thing is that I don't recall she said anything about preamble, at first I thought it is the initialization of the variable on the same line as the constructor. I am going to take a deeper read on the link!


Existence of Hamel basis, choice and regularity

Question: Blass (1984) shows that the existence of Hamel basis for arbitrary vector space over any field implies the axiom of choice. However such implication needs the axiom of regularity. As in Blass' article, existence of basis just implies axiom of multiple choice, strictly weaker than AC when without assuming regularity.
In the article he says whether the existence of basis implies the axiom of choice in $\mathsf{ZF - regularity}$ remains open. However it has been 31 years since the paper published and I wonder there is a progress about it. I would appreciate your answer.
Comment: I'm not aware of any result removing the need for regularity here.
Comment: @Gerry: I found every question tagged with the tag [hamel-basis] now. And since I already removed the tag from *all* the questions earlier today, it only made sense to do that again today (and if you'll check, you'll see that all these questions had the tag added about the same time, so none of these were my bumps).
Comment: So, @Asaf, have you found every question with "Hamel basis" in it now?
Comment: @Asaf, indeed. Gives tag-team wrestling a whole different meaning.
Answer: As far as I know, no real progress has been made.
Two papers worth mentioning, however, are these:

Paul Howard, If vector spaces are projective modules then multiple choice holds, MLQ Math. Log. Q. 51 (2005), no. 2, 187--190.
Marianne Morillon, Linear forms and the axiom of choice,  Comment. Math. Univ. Carolin. 50 (2009), no. 3, 421--431.

Neither proves anything significant regarding the question whether or not the existence of bases implies choice in $\sf ZFA$ (equivalently, $\sf ZF-Reg$). But both make something which can be considered progress towards such answer, even if that progress is not a big step forward.


How to dynamically define struct elements in C

Question: I want to create elements from the struct that I created as many as the number to be found in the program. Here is my code:
struct students {
    int studentNumber;
    char studentName[50];
    char studentSurname[50];
};

while (fgets(readed, MAXCHAR, fstudent) != NULL) {
        number_of_students++;                                           //Number of students number has been founded!
    }

When I find my student count in the "While" structure, how do I generate that number of elements?
Comment: Why have you got a student struct and a students struct with arrays of student names, that makes zero sense
Comment: @pm100 sample students.txt file : https://imgur.com/a/KKGWKdC
Comment: As noted above, you can use malloc like `struct students *the_students = malloc(number_of_students * sizeof(struct students))` (don't forget to `free` it when you're done with it). If you're using C99, you can also do `struct students the_students[number_of_students]`
Comment: @RohanKumar I know how to use malloc but when I try it it gives me errors that I didint understand. Can u write an example depending on my structure and number_of_students ?
Comment: Please post the actual thing you tried and the actual error you got.
Comment: @EdmCoff Hell yea! I tried it but it didn't worked before. But now everything is ok. Thank u man!
Comment: @GençYazılımcı I'm glad it worked, but I hope you realize if you had posted what you actually tried and the error you had, you probably would have got the answer quicker (and probably would have received an explanation of what went wrong with your attempt).
Comment: @EdmCoff Ok dude ty for all. Next time I'll do it like that :)
Comment: You don't. You should define a struct for a student and then create an array or list of those structs
Comment: @pm100 I already defined a  struct for student. My question is how can I create its elements depending on number_of_students
Comment: You mean you want to increase the array of structs by one element each time you are in the loop? Or you mean you want to assign it after the loop?
Comment: Use malloc? https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
Comment: @pm100 I think you are confused. Despite the plural name, the students struct appears to contain one name and is for one student.
Comment: @EdmCoff Ok let me try again. I have a file with student information in it. Inside While I find how many students are in this file. Later, I want to create them as struct elements so that I can do operations on these students. As an example, let's say there are 3 students in the file. After running the while, we got number_of_students = 3. After that I want to create 3 students from struct students as s1, s2, s3. Here's how do I do that?
Comment: @pm100 Ok let me try again. I have a file with student information in it. Inside While I find how many students are in this file. Later, I want to create them as struct elements so that I can do operations on these students. As an example, let's say there are 3 students in the file. After running the while, we got number_of_students = 3. After that I want to create 3 students from struct students as s1, s2, s3. Here's how do I do that?
Answer: Use malloc for dynamic memory allocation. struct students *the_students = malloc(number_of_students * sizeof(struct students))


Como manter ícone alinhado a esquerda ao redimensionar página?

Question: Possuo uma página que ao ser redimensionada a imagem do facebook fica quebrado e não estou conseguindo deixa-lo alinhado a esquerda junto ao campo da newsletter, já tentei diminuir o tamanho do input e não ficou legal, fiz isso:

#newsletter-form input#newsletter {
   float: left;
   width: 300px; /* ALTEREI DE 360 PARA 300 */
   margin-right: 10px;
   display: inline;
}

Além do input ficar menor ao redimensionar o imagem continuo quebrada.
A página pode ser vista aqui, sem a alteração: Site em desenvolvimento
Comment: Percebi na sua página um css atrelado a imagem `#newsletter-wrap img`, que está com float: right; no caso é sua imagem do facebook (ícone), alterei ela para left, e ficou melhor mas não sei se é o resultado que você precisa.... Ou você também pode tirar o float; Diminua de 300px para 280px o input que já fica melhor também; Veja se é o resultado que você quer...
Comment: Olá @RafaelWithoeft, obrigado pela dica, a imagem agora ficou colada ao input e mesmo assim ainda está quebrada.
Comment: Quebrada em que sentido? Qual o resultado que você espera da página ao ser redimensionada? Assim talvez ficaria mais fácil de ajudar;
Comment: Digo, jogou a imagem para a linha de baixo, como posso dar um espaço entre o o botão  e a imagem?
Comment: Pode usar um padding-left de uns 15px para deixar um pouco mais longe, em caso de quebrar... caso não esteja usando bootstrap, acredito que teria que usar css medias queries: http://tableless.com.br/introducao-sobre-media-queries/ e assim adaptar o seu input para diferentes "medidas"
Comment: Você pode tirar o float e o display do #newsletter-wrap img que o efeito continuará o mesmo, só deixe o padding-left; e veja sobre medias queries para adaptar seu input a diferentes resoluções; ouu... você pode usar width: 70%, por exemplo; Faça um teste e veja o que melhor se adapta para você;
Answer: Primeiramente remova os display: inline já que você esta usando float: left. Depois remova width e height do img e coloca no css para ficar melhor.
Código
#newsletter-form input#newsletter {
    float: left;
    margin-bottom: 0;
    margin-right: 10px;
    width: 360px;
}
#newsletter-form input.button {
    float: left;
    margin-bottom: 0;
    margin-right: 3px;
}
#newsletter-wrap img {
    height: 29px;
    margin: 0;
    width: 29px;
}


What is expectation of $X$?

Question: Let $A[1:n]$ be an array such that $A[i]=i$ for $i=1,\dots,n$.
An algorithm randomly permutes the elements of $A$, and call the resulting array $A'$.
Let $X$ denote the number of locations such that $A'[i]=i$.

What is expectation of $X$?
Comment: What have you tried? Where do you get stuck? Hint: the expectation is linear, so $E[X + Y] = E[X] + E[Y]$, even when $X, Y$ are not independent.
Comment: like E[X] = 1* (1/n) + 2 *(1/n)......... (since E[X]= summation (x)*P(x))..
Answer: Let $X_i$ take value $1$ if $A'[i]=i$ and let it take value $0$ otherwise.
Then: $$X=\sum_{i=1}^nX_i$$
Apply linearity of expectation as suggested in comment of Mees.
Comment: Can you please give complete solution,.. i am not completely clear
Comment: Can you find $\mathbb EX_1$? That is the expectation of a random variable that only takes values in $\{0,1\}$, so that $\mathbb EX_1=0\times P(X_1=0)+1\times P(X_1=1)=P(X_1=1)$.
Comment: E[X1] = 0* ((n-1)/n) + 1 *(1/n) ?
Comment: Yes, and of course $\mathbb EX_i=\mathbb EX_1=\frac1{n}$ for every $i$. Now apply that $\mathbb EX=\sum_{i=1}^n\mathbb EX_i$ (linearity of expectation).
Comment: 1/n + 1/n +...... n times =1 ?
Comment: Indeed. That's all.

What is expectation of $X$?


Adjust linear regression penalty for under/over-estimations

Question: Basically I have a case where under-predictions are worse than over-predictions. Is there a way to penalize the linear regression model during training according to some predefined ratio?
E.g. I want to define that, for an actual value 10, predicting 9 and predicting 12 has equivalent penalty. (And not 9 and 11 as per default).
I guess my question is, is this something that is acceptable to do in the first place, and how would one best go about it?
PS. Maybe there is a reasonable approximation to solving this - without meddling with the least squares function. E.g. I've tried increasing the y (output) values by 1-2% and that moves me in the right direction, though requiring to incrementally test for the best % increase (not the worst thing..).
Comment: If you do training as in *training and validation*, you have to specify the loss function. Just specify one that suits you. Is that what you mean by *to penalize the linear regression model*?
Comment: Sorry my terminology is so dodgy. Yea so basically I would like to add a factor to the loss function during least squares data fitting. Unfortunately in scikit-learn doing this means having to meddle with the underlying scipy and then C code.. so I'm wondering if there's some alternative solution, but I guess not.
Comment: OK, that's not what I was thinking of. That means you would not choose an OLS-fitted model based on a validation set using a skewed loss function but would rather want a model that uses a skewed loss function for fitting itself. I do not have an idea how to implement that.
Comment: *Expectiles* might be a relevant keyword.
Answer: My first thought is to do something like the quantile regression loss.
$$
l_{\tau}(y_i, \hat y_i) = \begin{cases}
      \tau\vert y_i - \hat y_i\vert, & y_i - \hat y_i \ge 0 \\
      (1 - \tau)\vert y_i - \hat y_i\vert, & y_i - \hat y_i < 0
   \end{cases}
$$
Then we add up each individual $l$ to get a loss $L$ for the whole model.
$$
L_{\tau}(y, \hat y) = \sum_{i=1}^n l_{\tau}(y_i, \hat y_i)
$$
You can figure out $\tau$ to balance the costs of high misses and low misses. I will demonstrate using your example where missing high by $2$ and missing low by $1$ should give equivalent penalties.
$$
(1-\tau)\vert 10 - 12\vert = \tau\vert 10 - 9\vert\\
(1-\tau)\vert -2\vert = \tau\vert1\vert\\
(1-\tau)\times 2 = \tau\times 1\\
1-2\tau = \tau\\
1 = 3\tau\\
\implies\\
\tau = 1/3
$$


How do you contribute patches to Drupal core?

Question: What is the proper process to contribute patches to Drupal core?
Let's say I find a bug in Drupal core, fix it, and create a patch.  How do I submit it?
If the bug is in Drupal 7, do I have to also fix it in Drupal 8, and submit two patches?  Does this also apply to documentation bugs/improvements?
Do all patches also require a unit test?
Comment: For the time being, I am looking for answers assuming that I already have an account on drupal.org, know how to use git, know how to check out core, and know how to create git patches.  Once we have good answers, we can annotate or add an answer to outline this process.
Comment: As this is a response to an issue a core maintainer raised at http://meta.drupal.stackexchange.com/questions/2325, the post notice will be enforced.  We need a good summary of the process, particularly about the questions above.  Answers that just link out to other pages will be downvoted and/or deleted.  I am hoping this question will serve as a resource that we can point DA users to when they find a problem with core.
Answer:
If the bug is in Drupal 7, do I have to also fix it in Drupal 8, and submit two patches?

If the bug is present in the version being developed (in this case Drupal 8), and the previous version (Drupal 7), then the bug should be first fixed the currently developed version, and then on the previous version(s).
If the bug is not present in the latest developed version, for example because the bug is for a function that has been removed, or that has been already changed, then the patch should be provided for the version before the currently developed one.
The workflow that is followed is:

First the patch is created, and submitted for reviews, for the most recent version, even if still under development (such as in the case of Drupal 8)
Once that patch is applied to Drupal, a patch that applies for the previous version is created, and submitted for reviews

You can create patches for two Drupal versions at the same time, but since the patch for the latest Drupal version could require changes, it is better to work on the patch for the currently developed version, than working on two different patches that both require to be changed.
See also: Backport Policy.

Does this also apply to documentation bugs/improvements?

Yes, it does. Also in this case, if the currently developed version removed the function/method to which the documentation is referring, then the patch needs to be provided for the previous version.

Do all patches also require a unit test?

If the patch is for the documentation, it doesn't require tests. The test bot running on http://qa.drupal.org checks the Drupal code after the patch is applied; if the patch introduces syntax errors (for example because the comment is closed before the needed), then the test bot will report an error about the patch before tests are run.
If the patch is for a new feature, then the tests are required.
If the patch is to fix a bug, then the tests could be required from the maintainers, if there isn't already a test checking a specific feature. The test is generally necessary to avoid re-introducing the same bug when changing in future the same code. If the bug is merely a variable that is initialized, but never used from a function/method, then chances are the tests are not required.
Comment: If the issue applies to more than one version, it can contain patches for every interested version.
Comment: Can the same issue contain patches for multiple core versions?
Comment: Is it always the case where the D8 patch is submitted first, then the D7 version?  Or are there cases where submitting both at the same time are appropriate?
Comment: You can submit patches for two different versions, but the patch that is first applies is for the most recent version. It is better to create first the patch for the more recent version, as the patch could require changes.
Answer: Every project page on drupal.org has a "Version control" tab at the top. It gives not only a great overview of all the code repository options for the specific project, all the git command examples are also tailored for the project. You can even interactively change it to a specific version too.
For Drupal core you find that page on http://drupal.org/project/drupal/git-instructions
Then when you have created your patch, you simply just attach the patch file to an issue that explains what you patch does.
Two important things to point out is to make sure the patch is ending with ".patch" and to set the status to "Needs review". This so that the testbot will notice it and automatically run its test.
New code always needs to be accompanied by tests.
Note that this is possible to turn off by projects and some do. For Drupal core it is on though so all patches will be tested.
You can find more at http://drupal.org/contribute/development .
Comment: This is a good start.


Counting rows where a column may different values

Question: I have a table like the following:
id letter number
1       A      1
2       A      2
3       A      3
4       B      1
5       C      1
6       C      2

I need to count the number of id's where the letter A has a number 1 OR 2.  That's the easy one, and I got it.  But now I need to count id's where the letter A has a number 1 AND a number 2, etc.   Then the same for each letter.  So I would get:
letter count
A          3
C          2

I dont care about B since the count would be 1.  Thanks.
Comment: Why is count of A = 3? Shouldn't it only count if 1 or 2 is present?
Comment: What language and where's your code sample?
Comment: My fault.  It should be counting the number of different values the letter has.  Depending on what the user selects, this could be a combination of ORs and ANDs.  So,it could be:
Comment: Ex 1: where A = 1 or A = 2 or A = 3 Ex 2: where A = 1 or A = 2 or B = 1 or C = 2 Ex 3: where A = 1 and A = 2 or A = 3 I am using Oracle 11g, with plain SQL I can do Ex 3 using Intersects, but that would get complicated and long, once more letters and values are selected.
Answer: Use a combination of analytical functions (PARTITION BY) and GROUP BY:
with v_data as (
select 1 id, 'A' letter, 1 num from dual union all
select 2 id, 'A' letter, 2 num from dual union all
select 3 id, 'A' letter, 3 num from dual union all
select 4 id, 'B' letter, 1 num from dual union all
select 5 id, 'C' letter, 1 num from dual union all
select 6 id, 'C' letter, 2 num from dual
)
select letter, sum(cnt1) as cnt1, sum(cnt2) as cnt2, count(*) cnt_overall from (
select v1.*,
  sum(case when num = 1 then 1 else 0 end) over (partition by letter) as cnt1,
  sum(case when num = 2 then 1 else 0 end) over (partition by letter) as cnt2
 from v_data v1
)
group by letter
having sum(cnt1) > 0 and sum(cnt2) > 0

Explanation:

the inner query returns alls rows and computes the number of occurrences of 1 / 2 for the row's id
the outer query computes the number of rows for a given id and get rids of the ids that don't have both values 1 and 2
Comment: This is correct, it should be marked as accepted.  Looks like you might get unlucky with Nelson Canas being a new user and this is his first question and may never come back and accept it:(