Find maximum volume of carry on when the total length of the sides is 45 inches

Question: My current problem is finding a formula to put the equation for volume, $V=xyz$, where $x+y+z=45$ in terms of just $x$ and $y$. So far, I have come up with
$V=45xy-x^2y-xy^2$
but when I try to solve for the critical points of the function using the partial derivatives of the function that I found,
$\frac{\delta V}{\delta x}=45y-2xy-y^2$
and
$\frac{\delta V}{\delta y}=45x-x^2-2yx$
all I get are the points $(0,45)$ and $(45,0)$. These are obviously the minimums of the function as they both cause $V=0$. How should I go about this instead of my original equation? Or am I just screwing up when solving for 0 with the partials I'm getting?
Comment: Do you know Lagrange multipliers?
Comment: I do not, and the chapter on them is the chapter after the one this exercise is under so I figured I wouldn't have to use them
Answer: I think it remains to solve $$45-2x-y=0\\45-x-2y=0$$ which imply $x=y=15$ and consequently $z=15$.


How to listen to a keep-alive HttpResponseMessage until it's closed?

Question: I'm using HttpClient to send an asynchronous POST request to a remote web server. That remote web server responds with the Connection header set to keep-alive. Eventually, it will close the connection.
What I can't figure out how to do is keep receiving data until the connection is set to close.
For example, consider the following code:
HttpClient client = new HttpClient();
string requestUri = ...; //My GET uri
string content = ...; //url-encoded contents of the request

HttpResponseMessage response = await client.PostAsync(requestUri, new StringContent(content));

In the code above, response will have the Connection header set to keep-alive. I couldn't find any members on HttpResponseMessage that seem to give me any means of continuing to receive information after the first keep-alive is received.
I can't figure out how to continue receiving responses until the Connection closes.
I was able to find all sorts of resources that dealt with sending keep-alive requests. That's not my issue. My issue is in receiving/handling a keep-alive response from the server.
Please let me know if there is anymore information that I could provide to improve this question.
Comment: Not sure if I'm misunderstanding your question, but once you post the request and you get the response, the cycle (request/response) is complete, there is no more data to be received from the server.
Comment: The server will not send more responses as there's nothing for it to respond to, once it's satisfied your request.

What makes you so worried about the connection being opened at the server?
Comment: @ManyRootsofAllEvil What you say is true. If the server responds with `Connection: keep-alive` that means the Connection from server to client is not closed yet. I need to continue receiving responses until the Connection is closed.
Comment: @crush Are you saying that you receive partial response to your original request? Or are you asking how to receive response from successive requests on the same connection?
Comment: @LB2 How can I continue receiving responses from the server until the server indicates the connection is closed? I would expect this functionality would actually lie on the `HttpClient`, but I cannot find a mechanism.
Comment: @crush The connection might not be closed but you have already the response. What else are you expecting to receive? You would need to send a new request to get more data. Keep-Alive is about multiple requests with the same connection. If you make another request you will not incurr the cost of a new connection. I don't understand what you are expecting to receive
Comment: @ManyRootsofAllEvil So should I send a GET request to the same URI until I get a response with a `Connection: close`? I was under the impression that the server would continue sending responses until it sent it's final response with `Connection: close`.
Comment: @ManyRootsofAllEvil I guess I misunderstood the purpose of Keep-Alive. I was expecting more responses from the server, but I now see that the server was expecting me to send a request as follow-up to its response. Thanks for your patience. I learned something new. I wish I could give you some reputation for your help!
Answer: Connection: keep-alive purpose is not to enable server to send multiple responses for the same single request - this would fundamentally violate how HTTP works.
Connection: keep-alive is server's way of signaling to the client that client can submit next request using the same, already established, TCP connection, thus help avoid the costs of establishing new connection (and possibly TLS handshake as well).
Reuse of the connection is (typically) handled by underlying library, and is abstracted away such that user (as in developer) of the library need not worry about handling.
All you should be doing is keep making as many client.Post/Get/etc requests, and the HttpClient library will take care of managing the underlying TCP connection and its reuse.
There is one area in HTTP that may look counter to the above and thus confusing, but is not.  The case is when server returns HTTP Status 100 Continue.  In this case you will see another response come from the server for still the same original request (and by "see" I mean not from HttpClient, but rather on the wire if you were to snoop with a tool like WireShark or a proxy).  What is happening in this case is if a client is making a large request, the server simply signals to the client that it is continuing to accept and read request, and for the client to continue sending that original request.  Once full request is received, the server will process and respond with a final response code and message.  And again, HttpClient will abstract away this interim 100 Continue response, so as developer you need not worry about it (in most if not all cases).
Comment: Thanks for the expanded explanation. This question was born out of a misunderstanding of Keep-Alive and an oversight on my part.
Comment: @crush Thanks and I updated answer with additional quirk about status code `100` which may appear as opposite to what the answer says, but in actuality is not.
Comment: Very cool. Thanks for that tidbit of additional information. I always like to know what's going on under the hood.
Answer: If all you want is receive the response for a given request you don't need to deal with keep-alive at all. Keep-alive is purely a performance optimization. The framework transparently manages connections.
All you do is send a request and receive a response. There is exactly one response per request.
One side will eventually close the connection which is not of any concern to you. You don't notice any effect of that.
Comment: Thanks. This helps clear up the misunderstanding I had about Keep-Alive's significance.


X11 is not available - Centos

Question: I have installed R 3.3.0 on a Centos server.  I have installed multiple packages including shiny and further installed shiny server.  I have loaded my app files and the app is running.  I have some ggplot which is not rendering and I am getting a error message:
X11 is not available
I googled and did the following:
Upon running capabilities() in R console I am getting the following:
jpeg    png tiff    tcltk   X11 aqua
FALSE   FALSE   FALSE   FALSE   FALSE   FALSE
http/ftp    sockets libxml  fifo    cledit  iconv
TRUE    TRUE    TRUE    TRUE    FALSE   TRUE
NLS profmem cairo   ICU long.double libcurl
TRUE    FALSE   FALSE   FALSE   TRUE    TRUE

I installed :
yum -y groupinstall "X Window System" "Desktop" "Fonts" "General Purpose Desktop"
yum install cairo-devel
yum install libXt-devel
install.packages("Cairo")
yum install xorg-x11-xauth
yum install xorg-x11-apps

All the above got installed but still I am getting the same error even after loading the library(Cairo).
When i run Sys.getenv("DISPLAY") in R i am getting a blank ""
One of the solutions here indicates that I should recompile R with ./configure --with-x=no.  while another blog here indicates why I shouldn't do this.  What I want to know is:

Is there any solution I missed which could possibly help me?
Can I remove R and reinstall from the beginning...(scares me, as I am sure what would transpire later and also have spent a lot of time setting up shiny-server)
If I should recompile how do I go about this...will
yum install r-base-dev./configure --with-x=no help?
Comment: How do you use the server, do you have it on your desk with a monitor attached to it or do you use some remote desktop software?
Comment: @Joni I use a remote server
Comment: So how do you connect to it
Comment: If i have to say install...through ssh else through my browser...to access my shiny application
Comment: Possible answer in this: http://stackoverflow.com/questions/36582594/a-virtual-display-for-ubuntu-server/36608933#36608933
Answer: Although this is an old question and it has some workarounds when you google it, I found a post about building R from source that makes this process easier.
First take a look at Building R from source
The commands you need to run in this post can be summarized as
yum install epel-release
yum install yum-utils  # to make yum-builddep command available

yum-builddep R # this command will install all the dependencies for building R from source
# Then under your R source folder run (I'm using R version 3.2.3, so you will want to change the version number in these commands):
./configure --prefix=/opt/R/3.2.3 --enable-R-shlib --with-blas --with-lapack # you can substitute your own target directory for /opt/R/3.2.3

make
make install

This will install R on your CentOS machine successfully. However, instead of having yum-builddep to install the dependence libraries for you, you can manually install these libraries, therefore you can skip those you already have.
If you re-run yum-builddep R, you will get all the dependencies installed, they are:
--> Already installed : zlib-devel-1.2.7-17.el7.x86_64
--> Already installed : xz-devel-5.2.2-1.el7.x86_64
--> Already installed : 1:valgrind-devel-3.11.0-24.el7.x86_64
--> Already installed : tre-devel-0.8.0-18.20140228gitc2f5d13.el7.x86_64
--> Already installed : 1:tk-devel-8.5.13-6.el7.x86_64
--> Already installed : texinfo-tex-5.1-4.el7.x86_64
--> Already installed : 2:texlive-collection-latexrecommended-svn25795.0-38.20130427_r30134.el7.noarch
--> Already installed : 1:tcl-devel-8.5.13-8.el7.x86_64
--> Already installed : readline-devel-6.2-9.el7.x86_64
--> Already installed : pcre-devel-8.32-15.el7_2.1.x86_64
--> Already installed : pango-devel-1.36.8-2.el7.x86_64
--> Already installed : openblas-devel-0.2.19-4.el7.x86_64
--> Already installed : ncurses-devel-5.9-13.20130511.el7.x86_64
--> Already installed : libtool-2.4.2-22.el7_3.x86_64
--> Already installed : libtiff-devel-4.0.3-27.el7_3.x86_64
--> Already installed : 2:libpng-devel-1.5.13-7.el7_2.x86_64
--> Already installed : libjpeg-turbo-devel-1.2.90-5.el7.x86_64
--> Already installed : libicu-devel-50.1.2-15.el7.x86_64
--> Already installed : libcurl-devel-7.29.0-35.el7.centos.x86_64
--> Already installed : libXt-devel-1.1.4-6.1.el7.x86_64
--> Already installed : libXmu-devel-1.1.2-2.el7.x86_64
--> Already installed : libX11-devel-1.6.3-3.el7.x86_64
--> Already installed : libSM-devel-1.2.2-2.el7.x86_64
--> Already installed : libICE-devel-1.0.9-2.el7.x86_64
--> Already installed : less-458-9.el7.x86_64
--> Already installed : 1:java-1.8.0-openjdk-headless-126.96.36.199-2.b11.el7_3.x86_64
--> Already installed : gcc-objc-4.8.5-11.el7.x86_64
--> Already installed : gcc-gfortran-4.8.5-11.el7.x86_64
--> Already installed : gcc-c++-4.8.5-11.el7.x86_64
--> Already installed : cairo-devel-1.14.2-1.el7.x86_64
--> Already installed : bzip2-devel-1.0.6-13.el7.x86_64
--> Already installed : automake-1.13.4-3.el7.noarch
--> Already installed : autoconf-2.69-11.el7.noarch

As a result you can choose to just install some of these libraries according to what your current libraries are especially if you already installed 'Development Tools' or some other packages.
In my case, I have Oracle JDK installed so I don't need open-jdk installed by yum-builddep, so I removed the open-jdk and the compilation passed without any errors when I set JAVA_HOME correctly.
Answer: Even after installed X11 on the system I still had issues with getting it to work. Make sure that Cairo is also installed. Note this is working on the assumption that if you open R in the terminal and type x11() you no longer get the "X11 is not available error".

ensure that Cairo is installed: yum install cairo

can test with the below command (thanks to https://github.com/IRkernel/IRkernel/issues/388)
library(datasets)
png("test.png", width=1200, height=1200, units="px", type="cairo")
boxplot(mpg~cyl,data=mtcars, main="Car Milage Data", xlab="Number of Cylinders", ylab="Miles Per Gallon")
dev.off()

install Cairo within R itself: install.packages("Cairo")

X11 is not available - Centos


How can I remove width and height from the  tag?

Question: Does anybody know how to remove width and height from this HTML markup?

Answer: In your theme open file THEMENAME.theme, example in my case THEMENAME is bartik and file is bartik.theme add code below:
function bartik_preprocess_image(&$variables) {
  if (isset($variables['attributes']['width']) && isset($variables['attributes']['height'])) {
    unset($variables['attributes']['width']);
    unset($variables['attributes']['height']);
  }
}

Clear all cache and see result.
Answer: /**
 * Implements template_preprocess_image().
 */
function THEMENAME_preprocess_image(&$vars) {
  if ($vars['style_name'] == 'IMAGE_STYLE_NAME') {
    // Remove explicit width & height declaration.
    unset($vars['width'], $vars['height'], $vars['attributes']['width'], $vars['attributes']['height']);
  }
}

Comment: `hook_preprocess_image()` doesn't normally get the style name, in its array parameter. That is a value `hook_preprocess_image_style()` gets and passes to `hook_preprocess_image()`. Since the first isn't necessarily invoked from the latter, this is the less generic code.
Answer: If you wish to solve this inside a twig template you can use the following:

This should be available since Drupal 8, see following issue: https://www.drupal.org/project/drupal/issues/2325517


How to override properties file using plugin in Jenkins?

Question: In my Selenium-TestNG-Maven automation framework, I am have properties file for storing 'application-url'. The Jenkins job is configured for this. I have to run this test suite on QA/Stage/Production server based on need. But not sure how can I dynamically provide or override the 'application-url' property used in the code. (Environment Injector plugin? How?)
As per code, Selenium WebDriver instance in created, then java code reads the properties file and same URL is used by Selenium instance to open a webpage.
Comment: http://stackoverflow.com/a/31241715/4280359
Answer: I dynamically provide URLs by using the choice parameter for my Jenkins job.
First select this project is parameter as pictured below:

Then select the Add Parameter > Choice Parameter as pictured below:

Then save.
Now when you are building you will be able to select the url to use for that particular build, like this:

And when you need to reference the url, you can pass it through to maven by using the $URL in the Maven Goals.
ADDED: Here is an example of how I am using the choice parameter in the Maven goals

I am passing the ${browserName} parameter that is defined using the Choice Parameter through maven which will be referenced using the system property "browser". So when I want to get the value in my code, I just use
System.getProperty("browser");

And I am able to access that passed parameter.
Comment: Thanks Jasmine for your reply. Could you please explain a bit more about how to use this choice parameter to pass to Maven Goals? Also, currently this URL is defined in one of the java properties file and read in code. I believe that also need some changes since Jenkins job is now passing the URL.
Answer: You can configure your jenkins to pull the latest code from any of the repository (Github, SVN) and in the Execute Shell build step, you can write a shell script to achieve the same.


Which diploma, ATCL or dipABRSM

Question: I've just completed my grade 8 piano ABRSM (Associated Board of the Royal Schools of Music in the United Kingdom) exam with distinction. Which diploma should I take next, Trinity's ATCL (Associate, Trinity College, London) or dipABRSM (Diploma of the Associated Board of the Royal Schools of Music)? What's the difference and what do people mostly take?

Trinity: https://www.trinitycollege.com/qualifications/music/diplomas
ABRSM: http://abrsm.org/en/our-exams/diplomas/

See also: What are the ABRSM graded examinations, and why would one want to take them?
Comment: http://music.stackexchange.com/questions/5215/instrument-grade-exams - the grade system is widely recognised in many countries. Not including, apparently, the USA. Where they are recognised, they are so endemic to formal music education (as opposed to the no-sheet-music kind) that we find it hard to imagine having to explain.
Comment: Congrats on your Grade 8! Don't forget that there's a third option - don't do a diploma! Are you doing a diploma for a reason? If not, and you're just doing it for "fun" then just go with the one which has the most interesting syllabus in your opinion. If you're doing it as a stepping stone to something even higher (a Masters degree? a job?) then look closely at the requirements and choose the one that fits best.
Comment: Could you please provide some details about what all these long acronyms mean? I've never heard of any of them. Are they particular to some education system in a particular country?
Comment: Thank you, slim. But please realize that many users of this site are not in the UK or the Commonwealth and we don't know what you are talking about. I have tried to edit the question to explain what these bewildering acronyms actually stand for.
Answer: You need to think about why you want the qualification.

Do you want it as a pre-requisite for entry into an educational establishment?
Do you want it in order to get professional work?
Do you just like collecting qualifications?

In all of these cases, I honestly believe the qualifications are pretty much equivalent. There aren't colleges or employers who would accept one qualification, but not the other. You might yourself think of one as having more prestige than the other - but that's a gut-feel Pepsi/Coke distinction that only you can evaluate.
However, don't take my word for it. Ask the employers and colleges that you're interested in what qualifications they look for.
Alternatively, you might not care for the certificate at the end, only being interested in the actual skills you learn in getting there. In that case you need to look at each syllabus in detail (they should be openly available), and see which one involves learning the skills you want to develop.
One big factor, is your teacher -- assuming you are using one. Your teacher may well have a preferred qualification, for which they know the syllabus, and with which they have experience teaching.
Answer: ATCL and dipABRSM  are considered to be the same level.  Another one like this is ALCM.  Many people regard them as a kind of grade 9.
In terms of music qualifications the dip ABRSM and ATCL are not considered to be "real" diplomas as they are too easy.  I took ALCM 35 years ago as a practise run for a real diploma.  This is how my teacher described it then.
These days professional players do not take diplomas, so it is difficult to say which most people take, as you don't tend to see them on concert programmes or in other public documents.  Also dip ABRSM and ATCL are not considered to be "real" diplomas because they are too easy so most people don't have them as their top music qualification and so don't mention them on their list of music qualifications.
If someone asked me which one to take I would suggest the cheapest, because the levels are the same.
Answer: They're both pretty equivalent in terms of standard and application - however I believe the more common of the two is the ABRSM one, at least in the UK everyone I know personally has taken that route rather than Trinity.
Take that information how you will, I don't think there's a bad route, but going with the more popular alternative often (not always) equates to the more recognised alternative.
Answer: Well done on grade 8 pno !!I passed mine about 45 yrs ago, and didn't continue with dips, etc. I felt that the exam system (and still feel) is good but doesn't broaden one's horizons much past 'formal' playing.It restricted my playing to set pieces, because the next exam had to be tackled, leaving me no time to play other sorts of music - there's only 24 hrs in a day !
If you have ONLY been through the exam courses, take a break and learn - some jazz, swing, whatever other kind of music denied you by ABRSM !!
If you feel you need the qualification to become a teacher, think again.Even if one is a brilliant player, it won't necessarily make them a brilliant teacher.Some of the best teachers have no paper qualifications, although if they wanted to work for an authority as in schools, etc., it would slow them down.

Which diploma, ATCL or dipABRSM


Importing content from non-drupal database - help with SQL please

Question: I have a website with guitar lessons that I am converting to drupal. I am new to drupal and have started getting familiar over the past few days. I created a content type for lessons and exercises, established categories via taxonomy, used Entity Reference to relate exercises and lessons (lessons can contain exercises or exercises can stand on their own) and created fields for exercises such as audio, video.
I now see that in the database there is no way to view lesson and exercise content as a whole. Everything is done via the node and field tables, and entity relations are through additional tables that I haven't discerned yet.
This database structure is making it hard for me to import all my lesson and exercise data, since in my previous site all fields associated with a lesson or exercise are in a corresponding lesson or exercise table.
I understand the drupal structure allows more power, and from reading this post about content types and database seems I would be crazy to expect to create a database table for my lessons and exercises. So then, how would I create SQL queries in drupal to perform the following task?
"Insert into the drupal structure a lesson content node, that is from a given category, that contains various fields I created in drupal, which have one-to-one relation to the fields in my source database table?"
Likewise, to get exercises squared away, how would I
"Insert into the drupal structure an exercise content node, that is from a given category, that is tied to a given lesson, that contains various fields, mapped one-to-one from source database to fields I created in drupal?"
I am really at a loss for how to compose the SQL. Any tips are vastly appreciated!
Regards
EDIT: here is the script I wrote, which seems to be populating everything as needed, but something is still missing...
EDIT2: I was missing some tables, so below is everything I did to import. Things seem to work but I have no idea if there is some other obscure setting I missed...
EDIT3: while the below worked, I ended up following the advice in the answers and used the API according to this post about adding nodes programmatically and this one which specifically talks about adding nodes with Entity Reference field. I then manually updated the node_counter table because I wasn't sure how to do that via the API.
<code>$pnuke = db_query("SELECT * FROM nuke_guitarlessons_guitarlessons WHERE 1");

$cids = array(8,10,11,13,14); // ignore these lesson categories
$catmap=array(); // this maps old cat ids to the taxonomy term ids
$catmap[] = 7;$catmap[] = 0;$catmap[] = 1;$catmap[] = 9;$catmap[] = 6;$catmap[] = 2;$catmap[] = 8;$catmap[] = 0;$catmap[] = 0;$catmap[] = 0;$catmap[] = 0;$catmap[] = 10;

db_set_active();

$lastid=4; // this just ensures that node ids are incremented as I want
while ($n = $pnuke->fetchAssoc()) {
    if (!in_array($n['cid'], $cids)) {
    $datets = strtotime($n['date']);
    $datenow = new DateTime();

    $title = $n['title'];

    db_insert('node')
    ->fields(array('vid' => $lastid+1, 'type' => 'lesson', 'language' => 'und', 'title' => $title, 'uid' => 1, 'status' => 1, 'created' => $datets, 'changed' => $datets, 'comment' => 0, 'promote' => 1, 'sticky' => 0, 'tnid' => 0, 'translate' => 0))
    ->execute();

    db_insert('node_revision')
    ->fields(array('nid' => $lastid+1, 'vid' => $lastid+1, 'uid' => 1, 'title' => $title, 'log' => '', 'timestamp' => $datenow->getTimestamp(), 'status' => 1, 'comment' => 0, 'promote' => 1, 'sticky' => 0))
    ->execute();

    db_insert('field_data_body')
    ->fields(array('entity_type' => 'node', 'bundle' => 'lesson', 'deleted' => 0, 'entity_id' => $lastid+1, 'revision_id' => $lastid+1, 'language' => 'und', 'delta' => 0, 'body_value' => $n['introtext'], 'body_summary' => '', 'body_format' => 'full_html'))
    ->execute();

    db_insert('field_revision_body')
    ->fields(array('entity_type' => 'node', 'bundle' => 'lesson', 'deleted' => 0, 'entity_id' => $lastid+1, 'revision_id' => $lastid+1, 'language' => 'und', 'delta' => 0, 'body_value' => $n['introtext'], 'body_summary' => '', 'body_format' => 'full_html'))
     ->execute();

    db_insert('field_data_field_lessonintro')
    ->fields(array('entity_type' => 'node', 'bundle' => 'lesson', 'deleted' => 0, 'entity_id' => $lastid+1, 'revision_id' => $lastid+1, 'language' => 'und', 'delta' => 0, 'field_lessonintro_value' => $n['description'], 'field_lessonintro_format' => 'full_html'))
    ->execute();

    db_insert('field_revision_field_lessonintro')
    ->fields(array('entity_type' => 'node', 'bundle' => 'lesson', 'deleted' => 0, 'entity_id' => $lastid+1, 'revision_id' => $lastid+1, 'language' => 'und', 'delta' => 0, 'field_lessonintro_value' => $n['description'], 'field_lessonintro_format' => 'full_html'))
    ->execute();

    db_insert('node_counter')
    ->fields(array('nid' => $lastid+1, 'totalcount' => (int)$n['hits'], 'daycount' => 1, 'timestamp' => $datenow->getTimestamp()))
    ->execute();

    db_insert('taxonomy_index')
    ->fields(array('nid' => $lastid+1, 'tid' => $catmap[(int)($n['cid']-1)], 'sticky' => 0, 'created' => $datets))
    ->execute();

    db_insert('field_data_field_category')
    ->fields(array('entity_type' => 'node', 'bundle' => 'lesson', 'deleted' => 0, 'entity_id' => $lastid+1, 'revision_id' => $lastid+1, 'language' => 'und', 'delta' => 0, 'field_category_tid' => $catmap[(int)($n['cid']-1)]))
    ->execute();

    db_insert('field_revision_field_category')
    ->fields(array('entity_type' => 'node', 'bundle' => 'lesson', 'deleted' => 0, 'entity_id' => $lastid+1, 'revision_id' => $lastid+1, 'language' => 'und', 'delta' => 0, 'field_category_tid' => $catmap[(int)($n['cid']-1)]))
    ->execute();

 $lastid++;
  }
}
</code>
Comment: This is a ~large~ topic probably well beyond the scope of a single question. Do not seriously consider grinding the SQL directly. It's probably do'able; but you have to touch many tables beyond the mere content in order to keep drupal happy with node ids and whatnot. Start by searching for "drupal import."
Comment: You obviously know how to write code. If you really want to write a custom import, I would consider writing a `drush` script that actually uses the `drupal` API to create nodes and populate them.
Comment: Lets take a look from another angle - in which ways can you export your lessons/exercises. So, mb you can export data that can be imported with Feeds module + Feeds extensible parsers
Comment: Well, I did manage to import the data, and I felt I took care of the taxonomy and other matters. But the content is not appearing in drupal. The only thing I didn't do what populate the corresponding field revision tables.  But I really feel like I did this carefully, and am not sure what I am missing...
Comment: Ahhh, ok, that would probably be the way to go! Not sure what drush is but will look into it. Thanks!
Answer: I would recommend trying the Migrate module. It's a bit harder to implement compared to a direct approach but much cleaner, too.

The migrate module provides a flexible framework for migrating content into Drupal from other sources.

To get started you can check the documentation and more specifically the examples that the module comes with. I think the "beer.inc" one mostly covers migrating SQL tables to Drupal.
The concept of the module is that you define a mapping between a source and a Drupal destination and then you can import the data or rollback them in case something didn't work. There is also a UI to help you keep track of your Migration classes/settings.
Answer: You could use the Feeds module to import the content from that other database, similar to what is shown in this video tutorial, which shows how to import data as Drupal nodes.


Custom Fields Unsorted

Question: I have created custom fields that have specific positions. If I go into custom field sets in admin panel, they are correctly sorted. However, if I enter the product entities where they are displayed, they are no longer properly arranged. The SW version is 184.108.40.206
product custom fields
custom fields set definition
<code>{"customFieldPosition":3,"label":{"de-DE":"Anzahl der Mahlzeiten 1","en-GB":"Number of meals 1","pl-PL":"Ilość posiłków 1"},"placeholder":{"de-DE":"Geben Sie die Anzahl der Mahlzeiten ein","en-GB":"Enter the number of meals","pl-PL":"Wprowadź ilość posiłków"}}
</code>
Answer: Looks like this got fixed with version 188.8.131.52. Here is the corresponding commit.
Comment: I checked that. It looks also good:
{"customFieldPosition":3,"label":{"de-DE":"Anzahl der Mahlzeiten 1","en-GB":"Number of meals 1","pl-PL":"Ilość posiłków 1"},"placeholder":{"de-DE":"Geben Sie die Anzahl der Mahlzeiten ein","en-GB":"Enter the number of meals","pl-PL":"Wprowadź ilość posiłków"}}
Comment: Turns out it was a more recent fix introduced with `184.108.40.206`. I updated my answer with the references.

Custom Fields Unsorted


Proof Help : $A \in \mathbb{R}^{n \times n}, \sigma > 0$ if and only if the next matrix is singular.

Question: I've been stuck with the next proof. I've tried to approach the first implication using that if $\sigma$ is singular value of A, then is solution of $ P(\lambda) = \det(A^t A - \lambda I)$, but i don't know how to continue.
Problem:
$A \in \mathbb{R}^{n \times n}, \sigma > 0$ singular value of A if and only if $\Big(\begin{matrix} A && -\sigma I \\ -\sigma I && A^t \end{matrix} \Big)$ is singular (non invertible).
Comment: How do you define a singular value?
Comment: My bad for not specify, I'm talking about Singular Value Decomposition, this is better explained here [link](https://en.wikipedia.org/wiki/Singular_value_decomposition)
Comment: I understand, but there are various ways of defining the singular values, one way is https://en.wikipedia.org/wiki/Singular_value_decomposition#Singular_values,_singular_vectors,_and_their_relation_to_the_SVD, I was asking how you define a singular value for the purposes of this exercise.
Comment: Tell me if it's wrong, but we (my class) define singular value $\sigma_i = \sqrt{\lambda_i}$, the i-th eigenvalue of $A^t A$. Sorry for the confusion.
Let me know if it's not possible to continue the excersie with that definition, and would be great if you could tell me which definition I should take to try to solve the problem. Thanks!
Answer: Suppose $\sigma>0$  satisfies $\det (A^TA -\sigma^2I) = 0$ then there is some $v\neq 0$ such that $A^Tv = \sigma^2 v$. Let $u ={1 \over \sigma} Av$, then $Av-\sigma u = 0$ and $\sigma v - A^T u=0$ hence the above matrix is singular.
Conversely, if $Av = \sigma u$ and $A^T u = \sigma v$ wth $(u,v) \neq 0$ then $(A^TA -\sigma^TI) v = 0$ and so $\det (A^TA -\sigma^2I) = 0$.


Auto-complete show everything by default

Question: I have an field of "User reference" on a content type -- using an auto-complete control. This works but our user list is in the order of 300+ and it's difficult to remember who all should be selected for various tasks.
Auto-complete by default shows a list only once you have entered a single character -- is it possible to have the list show everyone the minute you click the text field???
Comment: Why not use a view for the entity selection. Set up a entity reference page in a view and include other fields about the user such as department etc. Then set to search on all of these extra fields in views and now you can at least search by other grouping factors and not just users.
Answer: Mabye you can use the Chosen module :

Install, enable and configure the Chosen module
change the widget type of your field to "Select list"
in the field settings screen, check "apply chosen on this field" :

Now in your content edit form, you should see something like this, where the entire list appears in the dropdown, but where you can also type an autocomplete request :
Comment: yes, Chosen does not create a field type or widget... You must use a select list as the widget for your field, and then in the settings of the field, check "apply chosen on this field". And you can disable chosen for other selects in the module settings page, by chosing "never apply" on the 3 first settings.
Comment: I tried the module...unfortunately it doesn't seem to register as a field type or control type...but rather applies to all select's -- in which is won't work in my case.
Comment: Ya I figured that out...it's not quite going to work though. I have a user field (ie: called assigned) and that originally was a multi-select listbox -- then converted to auto-complete text box...I need the multi-select capability.


invalidating session across all application in weblogic 11g

Question: I am new to weblogic and am porting over a series of web applications from jboss. I have multiple war's that I deploy to the same weblogic server. All of them are configured for Form based authentication backed by active directory.
I'm having issues logging out of the applications however. I have one of the applications that serves as my login application that contains the actual login.jsp form. It also has a logout servlet that calls session.invalidate(). After calling this, i can't access protected pages in the main application, but can access protected pages in the other applications.
From what I've been reading, weblogic 11g shares the same session across all applications, which makes sense with what I am seeing. I'm finding references to using the following to invalidate a session across all applications:
invalidateAll(HttpServletRequest req)

in package
weblogic.servlet.security.ServletAuthentication

However, I can not find this or anything similar in weblogic 11g. Where is this utility class or how should I be doing this?
Thanks
UPDATE:
Based on Josek's answer below, I had to add the following to my weblogic.xml file for all of the applications that I am deploying:
 <session-descriptor>
     <sharing-enabled>true</sharing-enabled>
 </session-descriptor>

The jar that I was looking for is called wls-api.jar that contains the ServletAuthentication class. I was using the zip based 10.3.3.0 version of the web logic server. I found the jar in the installation of the wlserver_10.3 server installation as part of the Weblogic Fusion installation. I just took the jar and added it to our Nexus maven repository.
Answer: invalidateAll is documented as part of WLS 10.3 (aka 11g) so it should work in your logout servlet. Have you tried it and faced any error?

It also has a logout servlet that
  calls session.invalidate(). After
  calling this, i can't access protected
  pages in the main application, but can
  access protected pages in the other
  applications

This indicates to me, that the sessions are not shared across web applications, which is the default behaviour. If the sessions were shared the user would be logged out of all the applications. For the sessions to be shared, you would have to set sharing-enabled
to true
As the docs state,

By default, Web applications do not
  share the same session. If you would
  like Web applications to share the
  same session, you can configure the
  session descriptor at the application
  level in the weblogic-application.xml
  deployment descriptor. To enable Web
  applications to share the same
  session, set the sharing-enabled
  attribute in the session descriptor to
  true in the weblogic-application.xml
  deployment descriptor.
Comment: Hi thanks for the information. The problem with invalidateAll is that I can't find the jar file that contains that method. I will try setting the sharing enabled attribute. Thanks!
Comment: Ok, everything is working. See above for solution based on your answer.
Comment: this might work with multiple `war` deployed under weblogic app server. What if we have multiple `ear` deployed in weblogic.
those are accessed as `hostname.com/webapp1` and `hostname.com/app2`

As usual oracle doc hass not clearly mentioned `weblogic-application.xml` or `weblogic.xml`

or may be I am not clear with difference among them.


Gaussian Mixture and K-Means ?! a big challenge?

Question: This is taken from Tom. Mitche Material as Old-Exam.
I think the (2) is true and not (3). Who can verify me?
Answer: (3) is correct. In k-means, the distance is computed independently of the cluster with the same function for all clusters. Thus, if a datapoint x is equally distant to two centers, it probability to belonging to either cluster is 0.5
Yet, if you are using - as explained in (2) - a different covariance matrix for each cluster, the probability for the datapoint x is higher to belong to the cluster with the higher $r_i$ (a bigger covariance matrix).
Further great input and explanation can be found in the answers to this question:
How to understand the drawbacks of K-means


How is AJAX attached to exposed filters?

Question: I am currently rendering a view manually like so:
$content = \Drupal::service('renderer')->renderPlain(views_embed_view('user_admin_people', 'page_1'));

Then I'm sending the html to the page and inserting it into a div with javascript.  I needed to use renderPlain() rather than render() because when I was doing that, I was getting this error:

LogicException: Render context is empty, because render() was called     outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead.

Now, you're probably thinking, why don't you just use render arrays normally and let Drupal handle it?  Unfortunately, I can't do that, I need to render the view's html and send it to the page, which I have already done.  BUT, when I do this, clicking on 'Filter' brings me to the /admin/people page of the Drupal site (on which it will do ajax filters properly, because it rendered from a normal render array).
How could I attach the javascript to this view html so that it reloads its data via ajax instead of linking me to the view's page?  I have already tried Drupal.attachBehaviors(), thinking that it would run something from ajax_view.js from views (which is a confusing & large file).
Or, where would I look in core to find out more about this, or where is some good documentation to read?  Am I looking in the right place in core, looking at ajax_views.js?
Answer: Still early rendering should be avoided whenever possible. Did you try to return the unrendered view in an ajax command?
  $view = views_embed_view('user_admin_people', 'page_1');
  $ajax_response->addCommand(new HtmlCommand('#wrapper', $view));

If you use the unrendered build array all the things you've described in the question like reattaching drupal behaviors, js, and css are handled by the ajax implementation in drupal, on the server side when assembling the response and on the client side when processing the incoming json data.
For this to work you also need to enable the option "Use AJAX" in the view user_admin_people, otherwise the "Filter" button rebuilds the form on a new page, which then happens to be the admin page.
Comment: Yes, I have enabled "Use AJAX", but no, I can't use that method in my project unfortunately.  I need to learn as much as possible about how it works in core.  I've gathered that it puts the view information in drupalSettings including the dom_id and what-not, but I haven't had enough time to try it out.


Accessing Microsoft SQL Server remotely

Question: I am very naive to Microsoft SQL Server and SQL in general so excuse my ignorance.
We have this Microsoft SQL Server where we store our data from monitoring software. I need to run some queries and need to take a look at some data. Is there any way I don't have to remote into SQL Server to run these select queries?
Some sort of portal or light software I can download which connects to SQL Server and lets me do this.
Thanks in advance.
Answer: Use the SQL Server Management Studio. It can be downloaded separately.
https://msdn.microsoft.com/en-us/library/ms174173.aspx
Answer: All you need is SQL Management Studio. You can download it using this link: SQL Server 2014 Express. Please be sure to check the details to know the packages to select. Make sure to get the management studio checked.
If you need help using the management studio, this can help you: SQL Management Studio Tutorial

Accessing Microsoft SQL Server remotely


Calculo em consulta no Banco de dados

Question: Galera estou com um certo problema se fazer uma consulta no BD, o SELECT faz um calculo de algumas colunas no banco e gera um novo campo com o nome lucro porém se tem numero com virgula ele ignora a virgula e não mostra nesse novo campo gerado, se alguém me der uma luz de como fazer ele calcular a virgula junto agradeço
código da consulta:
 SELECT ID_Pacote,produto.Nome,
 peca,FK_ID_Produto,Quantidade,Data_entrada,Data_saida
 ,CONCAT(ROUND(pacote.Quantidade * produto.Preco  ), ' R$ ')
 AS 'Lucro'  FROM Pacote INNER JOIN Produto on pacote.FK_ID_Produto =
 produto.ID_Produto

Answer: No teu SQL, o uso do ROUND está incorreto, ele está arredondando sem casa decimal.
Utilize dessa forma:
ROUND(pacote.Quantidade * produto.Preco, 2) -- o 2 significa 2 casas decimais :)

No caso seu SQL ficaria:
SELECT ID_Pacote,produto.Nome,
 peca,FK_ID_Produto,Quantidade,Data_entrada,Data_saida
 ,CONCAT(ROUND(pacote.Quantidade * produto.Preco, 2  ), ' R$ ')
 AS 'Lucro'  FROM Pacote INNER JOIN Produto on pacote.FK_ID_Produto =
 produto.ID_Produto

Mais informações de como usar, acesse esse link
Espero ter ajudado! :)
Comment: Usa o concat(), tipo CONCAT('R$ ', '10,00');
Dá um curte em: https://www.w3schools.com/sql/func_mysql_concat.asp
Comment: Valeu Man :)...
Comment: Funcionou, Valeu, eu aprendi a fazer da maneira errada, obrigado pela dica
Comment: Opa, que ótimo... Não esqueça de marcar como resposta correta, pois outras pessoas podem passar pelo mesmo problema e já encontram a solução.. :)
Comment: Marco sim, só precisa esperar 5 min pra marcar sksk
Comment: cara me tira uma duvida, eu queria colocar o `R$` antes do valor, alguma sugestão ?

Calculo em consulta no Banco de dados


Help Understanding Inverse Laplace Transforms???

Question: I've been looking over some examples regarding inverse laplace transforms, and my textbook doesn't really go into much depth regarding examples such as this:
F(s) = (2(s-1)e^(-2s))/(s^2 -2s + 2)
I know that e^-2s is the unit step function, so that my answer will involve that, but does that mean I transform it from F(s) to F(s-2) and take the ILT of that?
Also, I know that the (s-1)/((s-1)^2 + 1) takes the form of e^(at)cosbt, so I know that will take part in the eventual answer, but I can't seem to get it to match with the book's answer.  It's as though I understand about 50% of it.
As you can see, I'm kind of confused, but would really like to understand this material better.
Thanks!
Answer: We have:
$$F(s) = \dfrac{2(s-1)e^{-2s}}{s^2 -2s + 2}$$
So we do this part first:
$$\mathscr{L}^{-1}\left(\dfrac{2(s-1)}{s^2 -2s + 2}\right) = \mathscr{L}^{-1}\left(\dfrac{2(s-1)}{(s-1)^2 + 1^2}\right) = 2e^t \cos t$$
Now the $e^{-2s}$ is a time shift by two units, including a unit step function, so we shift each term that has $t$ with $t-2$, which gives:
$$\mathscr{L}^{-1} (F(s)) = 2 e^{t-2} u (t-2) \cos (2-t)$$
Would recommed reviewing the first and second shift theorems in Section 4.3
Comment: I added a link I would strongly recommend reviewing for the first and second shift theorems! Regards
Comment: I see, so we can separate the unit step function from the rest, then evaluate it as if it weren't there, but then bring it back into the fold?
Answer: The inverse LT is given by
$$\frac1{i 2 \pi} \int_{c-i \infty}^{c+i \infty} ds \, F(s) \, e^{s t}$$
where $c$ is greater than the real part of any pole of $F$.  This has consequences for when, in $t$, we begin to have a nonzero $f$.
For the specific $F$ you show, we have
$$f(t) = \frac{2}{i 2 \pi} \int_{c-i \infty}^{c+i \infty} ds \, \frac{s-1}{s^2-2 s+2} \, e^{s (t-2)}$$
To compute, consider the contour integral
$$2 \oint_C dz \frac{z-1}{z^2-2 z+2} \, e^{z (t-2)}$$
where $C$ is the contour consisting of the intervals $[c-i R,c+i R]$, $[c+i R,iR]$, and $[-i R, c-i R]$, as well as the semicircle of radius $R$ centered at the origin in the left-half plane.  Thus, the contour integral is equal to
$$2 \int_{c-i \infty}^{c+i \infty} ds \, \frac{s-1}{s^2-2 s+2} \, e^{s (t-2)} +\int_c^0 dx \frac{x+i R-1}{(x+i R)^2-2 (x+i R)+2} e^{(x+i R)(t-2)} \\ + i R \int_{\pi/2}^{3 \pi/2} d\theta \, e^{i \theta} \frac{R e^{i \theta}-1}{R^2 e^{i 2 \theta}-2 R e^{i \theta}+2} e^{(t-2) R e^{i \theta}}\\ + \int_0^c dx \frac{x-i R-1}{(x-i R)^2-2 (x-i R)+2} e^{(x-i R)(t-2)} $$
Clearly, the second and fourth integrals vanish as $(e^{c (t-2)}-1)/(R (t-2))$ as $R \to \infty$.  The third integral has magnitude bounded by
$$2 \int_{\pi/2}^{3 \pi/2} d\theta \, e^{R (t-2) \cos{\theta}}$$
Note that, when $\theta \in [\pi/2, 3 \pi/2]$, $\cos{\theta} \le 0$, so that the integral vanishes as $R \to \infty$ only when $t-2 \gt 0$.  When $t-2 \lt 0$, on the other hand, it makes no sense to close the contour to the left as we have done here.  Rather, we will close to the right.  But we are getting a little ahead of ourselves; for now, assume $t-2 \gt 0$.
Thus the contour integral is simply equal to the first integral as $R \to \infty$.  On the other hand, the contour integral is $i 2 \pi$ times the sum of the residues at the poles of $F$ inside $C$.  Since by definition, $C$ encloses all of the poles of $F$, i.e. $z_{\pm}=1 \pm i$, we have that, for $t-2 \gt 0$,
$$f(t) = e^{(1+i)(t-2)} + e^{(1-i)(t-2)} = 2 e^{t-2} \cos{(t-2)}$$
When $t-2 \lt 0$, on the other hand, we close the contour to the right, and we get that the contour integral is equal to $i 2 \pi$ times the ILT, which by the residue theorem is zero because $C$ now encloses no poles.  Therefore,
$$f(t) = 2 e^{t-2} \cos{(t-2)}\, \theta(t-2)$$
Comment: Hi Ron, I feel like there would be a problem in the case $t-2<0.$ What do you mean by "close the contour to the right"? Do you mean, we take the semicircle of radius $R$ centered at the origin in the right-half plane instead? That will cause a problem. Because $c$ is fixed and $R$ will go to $\infty$, our contours will intersect.


Are SFP compatible with mini-GBIC

Question: I am shopping around to get a Synology NAS. Particularly the Synology DS2015XS which has 2 SFP+ 10GB port. I currently own a Cisco SG200-26 with 2 mini-GBIC. I have no idea how SFP+ or mini-GBIC work, so can someone tell me if these two are compatible? Is the mini-GBIC faster than the regular gigabite port thats on the SG200?
Answer: 10Gb SPF+ sockets normally backward compatible with SFP Rev.C, but you should double check with Synology.
The CISCO SG200-26?? should be mini-GBIC / SFP, that means it can only support 10/100/1000 Mbps with copper (100m), multi-mode fiber (up to 2km), and single-mode fiber (over 10km) connection.
The remaining choice was using 100 / 1000 Mbps SX / LX / LHX / ZX or BX, buy the SFP that is compatible with the equipment that talks with the same physical type, fiber type, number of fiber(s), wavelength, to setup the communication channel.
In my experience using the copper port and a copper based mini-GBIC performs the same. SFP normally used with fiber for long distance communication or the communication path need to passes through high EM signal areas (with high EM noise).
Comment: In terms of bandwidth, no you didn't have improvement. Furthermore, unlink running on copper port if you use SFP on one side and SFP+ on the other side, they may not really talk with each other.
Comment: Thank you. So that means the 10Gbp from the Synology isn't really going to improve anything since the bottle neck is on my Cisco SG200 because the mini-GBIC is only 1Gbps. I would not gain anything (throughput) beside longer distance from using the mini-GBIC?

Are SFP compatible with mini-GBIC


Get element name with GSON

Question: I have JSON like this:
{"foos":[{"id":1}, {"id":2}]}

I can turn it into List<Foo> pretty simply with GSON, like this:
Type t = new TypeToken<List<Foo>>(){}.getType();
JsonObject resp = new Gson().fromJson(
    new JsonParser().parse(json).getAsJsonObject().get("foos",t);

But let's assume that I also have another JSON, where the name of the array and type changes
{"bars":[{"id":3},{"id":9}]}

Of course I could just swap the "foos" parameter for "bars", but if it's possible, I'd like my software to do it for me.
Is there a way to extract the name of the array child with the GSON library?
Answer: I'm not sure if I understood what you want correctly, but aren't you referring to the use of generics? I mean you could write a method that returns you a List of your relevant class? Something along the lines of
Type type = new TypeToken<List<MyClass>>() {}.getType();
List<MyClass> myObjects = getMyObjects(new JsonParser().parse(json).getAsJsonObject().get("foos"), type);

public static List<T> getMyObjects(String jsonString, Type type) {
    Gson gson = new Gson();
    List<T> myList = gson.fromJson(jsonString, type);

    return myList;
}

Comment: I can, but I'd like to extract the key name first
Comment: When you say "key", the only thing that comes to mind is a HashMap. You use a string key such as "foo" or "bar" and retrieve the list of objects against it, something like Map> myObjectMap = new HashMap>. And after that you could use myObjectMap.get("foo") and retrieve the list of MyClass objects against it.
Answer: Looking at your JSON examples, I assume that the name of the list element can change, but not the content of the list. If this is correct, you could parse your JSON response just like this:
Type mapType = new TypeToken<Map<String, List<Foo>>>() {}.getType();
Map<String, List<Foo>> map = gson.fromJson(jsonString, mapType);

And then you can access the name of the list with:
String listName = map.keySet().iterator().next();

If the content of the list could also change, things get a bit more complicated...
Comment: The type of the items in the list might change (a bad example), but maybe this approach will work too, if I use a TypeToken for Map (or sufficiently generic second type parameter)

Get element name with GSON


Maximum occurrence of string from a character variable in R

Question: I have a data frame which has two columns(hospital name, type).Both the variables are character variables. The data is as showcased below :-
hospital_name  type
ABC            rural
ABC            rural
ABC            urban
XYZ            urban
XYZ            urban
EFG            rural

I am working on a code that will group by hospital name and take a count of each type within that group. Next, create a new column called type2 which will have the value of highest occurrence in type column. The desired out put should be :-
hospital_name  type  type2
ABC            rural rural
XYZ            urban urban
EFG            rural rural

I approached this problem using dplyr but I am getting error. Here is my solution :-
library("dplyr")
df<-df%>%group_by(hospital_name)%>%mutate(type2=names(which.max(table(type))))

The error is  :-
Error: incompatible types, expecting a character vector

Comment: Detached plyr and my dplyr version is 0.4.2. But still get the same error.
Comment: You could use `count(df, hospital_name, type) %>% slice(which.max(n)) %>% ungroup %>% select(-n)`. And for your own attempt, try it like this: `df %>%group_by(hospital_name)%>%dplyr::mutate(type2=names(which.max(table(type))))`
Comment: May you add a `dput()` of your exemple dataframe ? Isn't there a factor somewhere ?
Comment: @brittenb Its character in both the cases.
Comment: @brittenb Updated to 0.4.3 but the error remains - Error: incompatible types, expecting a character vector
Comment: @brittenb The type column has other values as well like "sub-urban". Is there an issue because of this?
Comment: structure(list(NET_PARENT = c("COMMUNITY HEALTH SYSTEMS (CHS)",
"JEFFERSON HEALTH", "JEFFERSON HEALTH", "MEMORIAL HEALTH SYSTEM (SPRINGFIELD IL)",
"TENET HEALTHCARE", "TENET HEALTHCARE", "TENET HEALTHCARE", "TENET HEALTHCARE",
"LIFEPOINT HEALTH (FKA: LIFEPOINT HOSPITALS)", "INDIAN HEALTH SERVICES"
), OWNERSHIP = c("for_profit", "non-profit", "non-profit", "non-profit",
"for_profit", "for_profit", "for_profit", "for_profit", "for_profit",
"public")), .Names = c("NET_PARENT", "OWNERSHIP"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 13L), class = "data.frame")
Comment: @brittenb I had changed the column names and contents in the data frame. The dput() output is from the original data.
Comment: @brittenb The issue is - there are couple of NA`s in type column which is leading to this problem. Is there a way to handle it? If I delete the rows with NA and run the code, your code works fine.
Comment: @x1carbon Please see the updated answer. I was able to solve your problem. In the future, please make sure that your minimal, reproducible example actually encompasses the issue at hand.
Answer: Given that your code above runs without error, but doesn't produce the desired output, I'd tweak it only slightly to get what you want:
dat <- dplyr::data_frame(hospital_name = c("ABC", "ABC", "ABC", "XYZ", "XYZ", "EFG"),
                         type = c("rural", "rural", "urban", "urban", "urban", "rural"))

dat %>% group_by(hospital_name) %>%
  mutate(type2 = names(which.max(table(type)))) %>%
  filter(type == type2) %>%
  distinct()

dat
# Source: local data frame [3 x 3]
# Groups: hospital_name [3]
#
#   hospital_name  type type2
#           (chr) (chr) (chr)
# 1           ABC rural rural
# 2           XYZ urban urban
# 3           EFG rural rural

Update
The comments above indicate that the data has NA in the type column, which is what seems to be throwing the error. However, this doesn't seem to be an issue on my machine.
dat <- data.frame(hospital_name = c("ABC", "ABC", "ABC", "XYZ", "XYZ", "EFG"),
                  type = c("rural", "rural", "urban", "urban", NA, "rural"))
dat
#   hospital_name  type
# 1           ABC rural
# 2           ABC rural
# 3           ABC urban
# 4           XYZ urban
# 5           XYZ  <NA>
# 6           EFG rural

sapply(dat, class)
# hospital_name          type
#      "factor"      "factor"

dat %>%
  group_by(hospital_name) %>%
  mutate(type2 = names(which.max(table(type))))

# Source: local data frame [6 x 3]
# Groups: hospital_name [3]

#   hospital_name   type type2
#          (fctr) (fctr) (chr)
# 1           ABC  rural rural
# 2           ABC  rural rural
# 3           ABC  urban rural
# 4           XYZ  urban urban
# 5           XYZ     NA urban
# 6           EFG  rural rural

Update 2
So I have finally been able to reproduce your error.
dat <- structure(list(NET_PARENT = c("COMMUNITY HEALTH SYSTEMS (CHS)",
"JEFFERSON HEALTH", "JEFFERSON HEALTH", "MEMORIAL HEALTH SYSTEM (SPRINGFIELD IL)",
"TENET HEALTHCARE", "TENET HEALTHCARE", "TENET HEALTHCARE", "TENET HEALTHCARE",
"LIFEPOINT HEALTH (FKA: LIFEPOINT HOSPITALS)", "INDIAN HEALTH SERVICES"
), OWNERSHIP = c("for_profit", "non-profit", "non-profit", "non-profit",
"for_profit", NA, NA, NA, "for_profit", NA)), .Names = c("NET_PARENT",
"OWNERSHIP"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L,
13L), class = "data.frame")

dat

#                                     NET_PARENT  OWNERSHIP
# 1               COMMUNITY HEALTH SYSTEMS (CHS) for_profit
# 2                             JEFFERSON HEALTH non-profit
# 3                             JEFFERSON HEALTH non-profit
# 4      MEMORIAL HEALTH SYSTEM (SPRINGFIELD IL) non-profit
# 5                             TENET HEALTHCARE for_profit
# 6                             TENET HEALTHCARE       <NA>
# 7                             TENET HEALTHCARE       <NA>
# 8                             TENET HEALTHCARE       <NA>
# 10 LIFEPOINT HEALTH (FKA: LIFEPOINT HOSPITALS) for_profit
# 13                      INDIAN HEALTH SERVICES       <NA>

dat %>% group_by(NET_PARENT) %>% mutate(type2 = names(which.max(table(OWNERSHIP)))
# Error: incompatible types, expecting a character vector

This is happening because the most popular option for both dat$NET_PARENT == "INDIAN HEALTH SERVICES" and dat$NET_PARENT == "TENET HEALTHCARE" is NA. This throws an error in mutate because it wants a character value and instead gets a NULL value. We can fix this with the following change.
dat %>%
  group_by(NET_PARENT) %>%
  mutate(type2 = ifelse(length(which.max(table(OWNERSHIP))) == 0,
                        "NA",
                        names(which.max(table(OWNERSHIP)))))

# Source: local data frame [10 x 3]
# Groups: NET_PARENT [6]

#                                     NET_PARENT  OWNERSHIP      type2
#                                          (chr)      (chr)      (chr)
# 1               COMMUNITY HEALTH SYSTEMS (CHS) for_profit for_profit
# 2                             JEFFERSON HEALTH non-profit non-profit
# 3                             JEFFERSON HEALTH non-profit non-profit
# 4      MEMORIAL HEALTH SYSTEM (SPRINGFIELD IL) non-profit non-profit
# 5                             TENET HEALTHCARE for_profit for_profit
# 6                             TENET HEALTHCARE         NA for_profit
# 7                             TENET HEALTHCARE         NA for_profit
# 8                             TENET HEALTHCARE         NA for_profit
# 9  LIFEPOINT HEALTH (FKA: LIFEPOINT HOSPITALS) for_profit for_profit
# 10                      INDIAN HEALTH SERVICES         NA         NA

Notice that type2 is "for_profit" for "TENET HEALTHCARE" even though the max value is NA. This is because table doesn't capture the NA and it is omitted from the values. As a result, the only value there is what is recorded as the max. But for "INDIAN HEALTH SERVICES", it is listed as "NA".
Comment: This one is a better solution. Plus one.
Comment: I still get this error - Error: incompatible types, expecting a character vector
Comment: Thanks. When I check the class using sapply. Its character in both the cases. Is this an issues? NET_PARENT   OWNERSHIP
"character" "character"


Moral justification for "sheaf=continuously variable set" and local injectivity

Question: From the topos theory perspective it is a general motto that sheaves are continuously variable sets. The internal logic of sheaf toposes justifies this motto, but I would like an additional "local" justification from the bundle picture.
It's known the category of sheaves on a topological space is equivalent to the category of local homeomorphisms to $X$, so it seems a local homeomorphism to $X$ captures the correct notion of a "set continuously varying over $X$". I don't understand "why" the variation of the fibers of a local homeomorphism deserves to be identified with "continuous variation".
Intuitively, I feel a continuously variable set might just be a continuous open map (see below) with discrete fibers (to ensure they are sets and not cohesive spaces). As this answer points out, these properties do not imply local homeomorphy since e.g $z\mapsto z^2$ as a function $\mathbb C\to \mathbb C$ satisfies them all but is not homeomorphic about zero. In addition to continuity and openness, local injectivity is needed (it implies discreteness of fibers).
As a simpler example, the function $\mathbb R\to [0,\infty)$ defined by $x\mapsto x^2$ is continuous and open with discrete fibers, and the variation of fibers seems continuous to me. I would like to understand why the "collapse of fibers" at zero should be viewed as pathological/discontinuous. Before comparing the variation of fibers at zero with that of a local homeo to $[0,\infty)$, here's my intuition for the properties involved.
So a function $f:X\to Y$ is a local homeomorphism iff it's a continuous open local injection.

Continuity means close points have close images.
Openness is equivalent to $C\mapsto \forall_f(C)= \left\{ y\in Y\mid f^{-1}(y)\subset C \right\}$ preserves closed sets. I interpret this as saying that a net of fibers always converges "onto" a limit fiber.
Local injectivity means "unramified" (in $\mathsf{Top}$ it is equivalent to having an open diagonal).

For a concrete comparison of local homeomorphism vs continuous open map with discrete fibers consider:

$x\mapsto x^2$ as a function $\begin{smallmatrix}\mathbb R\\
\downarrow\\
[0,\infty)
\end{smallmatrix}$.
Let $U\subset X$ be an open subset and consider the local homeomorphism $\begin{smallmatrix}X\amalg U\\
\downarrow\\
X
\end{smallmatrix}$ defined by component-wise inclusion. Particularly consider $\begin{smallmatrix}[0,\infty)\amalg (0,1)\\
\downarrow\\
[0,\infty)
\end{smallmatrix}$.

I have tried to draw the variation of fibers below.
Question. Why should I think of the upper drawing, where the double fibers "collapse" into a point" as "discontinuous variation"?
Comment: If we decategorify, we can ask the question "what is a continuously varying boolean?". There are actually three answers. If we put the Sierpiński topology on $\{\bot,\top\}$ then a continuously varying boolean is an open set. If we use the indiscrete topology then a continuously varying boolean is a subspace. If we use the discrete topology then a continuously varying boolean is a clopen set. So in the categorified case we should expect that there are many different "topologies on $\mathbf{Set}$" leading to many different notions of continuously variable set.
Comment: There is no moral justification:  mathematics is amoral.
Comment: Nice question! Did you find an answer, meanwhile?
Comment: Another such "topology on $\mathbf{Set}$" could say that a continuously variable set was a covering map.
Comment: @IngoBlechschmidt what do you think about the answer?
Comment: @OscarCunningham Very nice comment! Can we categorify again? A sheaf on $X$ is equivalently a geometric morphism $\mathrm{Sh}(X)\to\mathrm{Set}[\mathbb{O}]$ to the "object classifier", the classifying topos of the geometric theory with exactly one sort and no function symbols. Some of these geometric morphisms factor over subtoposes of the object classifier, for instance there is the subtopos classifying "discrete objects". Assuming LEM, these two toposes have the same category of points, namely the category of sets. @ Arrow Very interesting, will think about it!
Answer: Perhaps the "justification" is to simply avoid duplicate copies of the same variable set, where one copy is the unramified version and the other is the ramified version.
I think presheaves correspond to bundles with covering local sections, whose total space topology is generated by its sections. If so, then the second picture in the question has topologically distinct representation of the same fibers: ramify the two branches to have them both converge at the endpoints.
Restricting to local injections excludes ramified representations, so passing from presheaves to sheaves "kills duplicates".


Hide header from ToC in Asciidoc / Asciidoctor

Question: I'd like to find a way to hide certain headers from the Table of Contents in my Asciidoc document, which I am processing with Asciidoctor to HTML and PDF.
I have increased the headerlevel value to include some other sub-headers, but a side-effect of this is that some unwanted headers are now also present in the ToC of the output document.
My document is composed dynamically from multiple source files using this kind of statement:
include::deployment/topic.adoc[leveloffset=+3]

Some of these headers should not appear in the ToC - including them would lead to a lot of repetition (they have similar content under each major header).
E.g.:
Each component topic has an H1 header that these 'leveloffset' attributes demote to h2, h3 etc. as required:
= My Topic Title

So my resolved document looks like this:
= Main Title (Keep in ToC)
== H2 (Keep in ToC)
=== H3 (Keep in ToC)
== H2 (Keep in ToC)
=== H3 (Remove from ToC)
== H2 (Keep in ToC)
=== H3 (Remove from ToC)

I can't decrease headerlevel to remove the instances of === H3 (Remove from ToC) without also removing === H3 (Keep in ToC) from the ToC.
I'm trying to keep the document modular. So making some headings simple bold text (using asterisks) is not ideal. I would like to keep them as headers, but add some kind of attribute to them that would mark them as 'non-ToC'.
I'd be grateful for any hints or ideas.
Thanks
Answer: I just figured it out - d'oh!
A workable solution is to insert the [discrete] attribute above the header you want to define:
[discrete]
=== H3 (Remove from ToC)
This is the H3 content.

This section of the Asciidoctor docs shows how this attribute is normally used. However, you actually don't need to use an H2 header as it suggests - Asciidoctor respects hierarchical levels when using [discrete], so nesting still works.
Comment: It looks like that section of the asciidoctor manual was renamed; this url works: http://asciidoctor.org/docs/user-manual/#discrete-headings


Modelling Correlated Predictors

Question: Say I have three (financial) variables: $X_1, X_2$ (predictors) and $Y$ (dependent variable). I suspect that both have an effect on $Y$, but I also suspect that $X_1$ has an effect on $X_2$ and that they are therefore correlated.
With that in mind, it is probably inadvisable to put both predictors into a regression as they are correlated and this will result in unstable estimates of the regression parameters.
But, I am interested in both of their effects on $Y$ and would like to examine them.
What would be the correct way to approach this problem? I thought of the following regression:
$Y$ ~ $\alpha + \beta_1X_1 + \beta_2(X_2 - X_1)$
The logic is that I am capturing the effect of $X_1$, and the effect of $X_2$ that isn't due to $X_1$.
Answer: Simply subtracting X1 from X2 is going to give you what you are interested in.  This just creates a new variable where the associated beta coefficient will then tell you the effect that the difference in X2 and X1 gives on the expected value of the response.
Instead, try regressing X2 on the residuals left behind from a simple regression that uses only X1 as the original predictor.  That will tell you how much variance X2 explains after accounting for X1 .
This is a similar process to what drives certain step-wise variable selection methods.  A forward step-wise selection starts by regressing each individual predictor that is present against the Y variable to determine which single predictor actually captures the most variation in Y.  Call the winning predictor X1 for simplicity.  Once X1 has been selected as a feature to keep, the algorithm continues by regressing the remaining predictors against the residuals left behind from fitting X1; it finds the predictor (call it X2), that best explains the variance in these residuals.  The process of fitting to successive residuals continues until a pre-determined number of predictors are found, or when the significance of the predictor drops below a pre-defined significance level.
Comment: I haven't calculated these quantities yet - I wanted to approach it not being biased by the data. Let's assume that they are correlated (say 0.5) and that I'm still interested in both of their effects on Y. I'm not interested in variable selection techniques because I know a priori that I'm interested in these variables.
Comment: I was simply saying the methods that drive the variable selection techniques may be of use to you in your current situation.  Try regressing on the residuals after your initial simple regression (say using X1 as your predictor), and that will tell you how much variance X2 explains after accounting for X1.
Comment: Add the "Try regressing on the residuals after your initial simple regression (say using X1 as your predictor), and that will tell you how much variance X2 explains after accounting for X1." tip to your answer so i can mark as answer

Modelling Correlated Predictors


PubNub client() function not working for callbacks

Question: Currently in Swift I have a PubNub object subscribed to a specific channel. Whenever an event happens, I can see a Logger instance being done, however none of the client() functions are receiving anything as callbacks.
The three I have set up are:
<code>    func client(client: PubNub!, didReceiveStatus status: PNSubscribeStatus!)
    func client(client: PubNub!, didReceivePresenceEvent event: PNPresenceEventResult!)
    func client(client: PubNub!, didReceiveMessage message: PNMessageResult!, withStatus status: PNErrorStatus!)
</code>
Is there another way to receive the subscription event callback or is there something I am missing?
Comment: Howdy there `Min Tseng`.  This issue may be as simple as a channel or API Key mismatch.  You mentioned that the debug output shows logging events occurring.  Are those events messages being received?
Answer: You need to call addListener(self) on PubNub instance passing required instance instead of self which conform to PNObjectEventListener protocol.


View não renderiza após return - Asp.net MVC

Question: Boa tarde, pessoal!
Seguinte cenário:
Tenho um botão de pesquisa no meu Layout que usa o campo text e o select para enviar dados para o javascript, que chama uma action, que chama o método do repositório para trazer os dados que preciso.
Tudo funciona, os dados são retornados, porém a View não é exibida.
A Action tem o mesmo nome da View que recebe uma List.
Botão da chamada:

                            <input type="button" id="btnPesquisar" name="name" value="Pesquisar" />
                            @* *@

Action:
 [WebMethod]
     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public ActionResult filtrarImovel(string texto, string filtro)
    {
        List<ImovelFiltradoViewModel> model = new List<ImovelFiltradoViewModel>();

        model = repositorio.FiltrarImovel(texto, filtro);

        model.OrderBy(i => i.ID_IMOVEL);

        TempData["ImovelFiltrado"] = model;

        return View(model.ToList());
    }

View:
    @model List<MeuImovel.Domain.ViewModel.ImovelFiltradoViewModel>

Imóveis

        @{foreach (var item in Model)
            {

                        @if (item.fotos.Count > 0)
                        {

                        }
                        else
                        {

                        }

@item.ENDERECO

$@string.Format("{0:n0}", item.VALOR_ALUGUEL)

                                        Ligue Já

            }
        }

@section Scripts {
    <link href="~/Content/RepeaterImoveis.css" rel="stylesheet" />

}

JavaScript:
$(document).ready(function () {

$("#btnPesquisar").click(function () {

    var campoTexto = document.getElementById("textoFiltroHome").value;
    var campoLista = document.getElementById("dropFiltroHome").value;
    //alert(campoTexto + " - " + campoLista);

    $.ajax({
        type: "POST",
        cache: false,
        url: 'Imoveis/filtrarImovel',
        data: {texto: campoTexto, filtro:  campoLista },
        dataType: "json",
        success: function (data) { ExecutarSucesso(data.d); },
        error: function (data) { ExecutarErro(data.d); }

    })

    function ExecutarSucesso(result) { };

    function ExecutarErro(result) { };

});

});
Answer: Se eu entendi e você quer retornar uma View depois dos dados ser enviados para o Controller terá que redireccionar a para a View em caso de sucesso no seu ajax assim:
    success: function (data) {
    window.location.href = "/Controller/View"
  }


Benchmarking Nginx against Express

Question: I have Nginx set up as a reverse proxy in front of my express application.
So every request that comes to Nginx is proxied to express running on 4 ports. Both Nginx and express run on the same hosts .
After having read that all the static content should be served by Nginx and Express should be left for dynamic requests only, I gave it a shot and set up the Nginx config . It works perfectly . So now all JS / CSS and HTML assets are served by Nginx itself.
Now how do I prove that this is a better setup in terms of numbers ? Should I use some tool to simulate requests ( to both older and the newer setup ), and compare the average load times of assets ?
Answer: Open your browser => Dev tools => Networks
Here you can see the network wait time and download time for every request. So you can open your webpage and compare it with both the configs.
This can be helpful on a local env so latency has minimal effect on testing.
Other than that you can do a load test. Google load testing tools!
Comment: Developer tools would not very convincing I think. After all how many calls will I trigger ? Load test seems a better options - triggering a large no. of calls and then comparing the average load times.
Comment: @MayurArora Both serve a different perspective. Browser will give you performance improvements from a single user's view. Load test will also take into account the resource hogging parameters.
Answer: In a word, "benchmark."  You have two configurations.  You need to understand the efficiencies under each model.   To do so you need to instrument the hosts to collect data on the finite resources (CPU, DISK, MEMORY, NETWORK and related sub statistics) as well as response times.
Any performance testing tool which exercises the HTTP interface and allows for the collection and aggregation of your monitoring data while under test should do the trick.   You should be able to collect information on the most common paths through your site, the number of users on your system for any given slice of time, the average session duration (iteration interval) all from an examination of the logs.    The most common traversals then become the basis for the business processes you will need to replicate with your performance testing tool.
If you have no engaged in performance testing efforts before then this would be a good time to tag someone in your organization who does this work on an ongoing basis.  The learning curve is steep and (if you haven't done this before and you have no training or mentor) fairly long.   You can burn a lot of cycles on poor tests/benchmark executions before you get it "right" where you can genuinely compare the performance of configuration A to configuration B.

Benchmarking Nginx against Express


If $4a + b = 3$ then $20a + 5b =$?

Question: Is this solvable? I think not, I think something is missing.
Am i wrong?
<code>If 4a + b = 3 then 20a + 5 = ?
a. 15
b. 1/4
c. 4/3
d. 3/4
</code>
Comment: That problem must have a typo: it should be asking about $20a+5b$, which of course is $15$.
Comment: perhaps the edit which corrects the typo is not so helpful...
Comment: @Ronald: I think I see what you're saying. Brian's comment looks as though it's correcting an error that isn't there...
Comment: I find this a bit strange to get tagged "linear algebra" instead of "precalculus", maybe that's just me.  I mean, in linear algebra, don't you try and find all solutions to equations like ((4*a)+b)=3?  Digressing, ((4*a)+b)=3 has an infinity of solutions which you can describe by a line, or equivalently an infinite set of pairs on the reals.  All members of the set of pairs have form
(a, (3-(4*a))).  So, (1, -1), (2, -5), (3, -9), etc. all work as particular solutions for ((4*a)+b)=3, though of course this isn't what this question is asking.  The tag "linear-algebra" just trips me up here.
Comment: @Doug: Feel free to change it!
Answer: You are quite correct: something has to be missing. No matter what value you give $a$, there is a value of $b$ that makes the first equation true, so $20a+5$ is indeterminate.
From the answers given, it's clear that the second expression was supposed to be $20a+5b$; since this is $5(4a+b)$, the intended correct answer must be $15$.
Comment: Ok thanks, That sounds about right.
Answer: You got 1 equation with 2 unknowns and you're trying to find the first one (finding 20a+5 requires a). Therefore it's unsolvable.
Comment: Thanks!, thats what I thought.

If $4a + b = 3$ then $20a + 5b =$?


Minimizer of a multivariable function and iteration through Newton's method

Question: I got stuck on the following question.

Find the minimizer for $$f (x_1,x_2) = \frac 12 (x_1^2 - x_2)^2 + \frac 12 (1-x_1)^2$$ and compute one iteration for minimizing $f$ from point $(2,2)$. Also, is this a good step?
Comment: You can find the extrema by solving $\nabla f(x) = 0$. As for the iteration, just apply Newton's method (you'll have to compute the gradient and the hessian.
Comment: i did f'(x1)=0 and f'(x2) =0 and found (1,1) as the critical point but didn't know what to do after.
Comment: I was confused because (1,1) is a root so does it have to be min/max? I wasn't sure how to chech min/max using f''() because this was a multivariable function.
Comment: Checking whether it's a minimum or a maximum can be done by computing the Hessian and checking whether it's positive/negative semi-definite at the given point. You would need the Hessian for the iteration step anyways.
Answer: $f(x_1,x_2)$ is a well known test function. It is the Rosenbrock function. The Newton method
$$
X_{k+1}=X_k - H^{-1}_k \nabla f_k
$$
gives
$$
\left[
\begin{array}{ccc}
x_1 & x_2 & f(x_1,x_2)\\
 2 & 2 & 2.5 \\
 1.8 & 2. & 0.3208 \\
 1.05926 & 3.2 & 0.15229 \\
 1.03101 & 0.573333 & 0.000480989 \\
 1.00005 & 1.06217 & \text{4.603702725}*{10}^{-7} \\
 1. & 0.999141 & \text{4.47192928}*{10}^{-15} \\
\end{array}
\right]
$$


Using $\chi^2$ or Fisher's exact test with low expected counts

Question: Let's say I have 7 urns filled with random numbers of colourful marbles. An example dataset is as below:
data <- matrix (c(5,3,4,4,4,2,1,1,1,1,1,2,2,1,2,2,1,1,2,1,4,1,1,2,4,1,3,1,7,1,1,2,1,3,3), ncol = 5);
rownames(data) <- as.character(seq(1,7));
colnames(data) <- c("red", "blue", "yellow", "green", "pink");

I assume the count-based colour distribution to be multinomial. I want to test whether the contents of Urn 1 follow the colour distribution of Urns 2-7.
I get the MLEs for Urns 2-7 as:
p_sample <- colSums(data[2:7,])/ sum(colSums(data[2:7,]))

1) When I run a $\chi^2$ test as below, R displays a warning message since the expected counts (EC)<5.
obs <- data["1",]
chisq.test(obs,p=p_sample)
# Chi-squared test for given probabilities

# data:  obs
# X-squared = 8.0578, df = 4, p-value = 0.08948
#
# Warning message:
# In chisq.test(obs, p = p_sample) :
#  Chi-squared approximation may be incorrect

One of the answers to this question states that $\chi^2$ test would nevertheless return accurate results as long as ECs exceed 1.0 if a very simple $\frac{N-1}{N}$ correction is applied to the test statistic.  Is the correction implemented simply like this: $\chi^2 = (\sum_i \frac{(O_i - E_i)^2}{E_i} )* \frac{N-1}{N}  $  ?
This link suggests so but I am not sure about its reliability:

If one has the regular Pearson chi-square (e.g., in the output from
  statistical software), it can be converted to the 'N - 1' chi-square
  as follows:
           'N -1' chi-square = Pearson chi-square x (N -1) / N

2) Alternative to $\chi^2$, since the number of counts is low, I ran a Fisher's exact test and got the results below. Is how I call the fisher.test below correct? The result I get makes me think: "No". I am confused since the R help document only refers to use cases for contingency tables.
fisher.test(obs, sum(obs)*p_sample)

# Fisher's Exact Test for Count Data
#
# data:  obs and sum(obs) * p_sample
# p-value = 1
# alternative hypothesis: two.sided

Comment: You should't use the same observation twice. You probably want to compare the color distribution in urn 1 with the color distribution of the other urns combined. The null hypothesis of the corresponding (generalized) Fisher's exact test is 'the two distributions are equal' which is similar to 'the distribution in urn 1 is equal to the one of all urns together'.
Comment: @MichaelMayer, thanks I will update the code and question accordingly to get answers to 2) and 3)
Answer: As already pointed out in my comment referring to the original question, your preferred null hypothesis "color distribution in Urn 1 is equal to color distribution in all Urns combined" is equivalent to the null hypothesis "color distribution in Urn 1 is equal to color distribution in Urn 2-7". The former recycles observations in Urn 1, destroying independence. Thus, for technical reasons, the latter formulation is better suited.
Null hypotheses like this are usually tested by the approximate $\chi^2$-test of independence and, for not too large samples, by Fisher's exact test for general two-dimensional tables. Your sample is not large, so in principle we can go with Fisher's version and do not need to care about improving approximation by $\chi^2$. Nevertheless, we can compare the different ways in your example and see what comes up:
Fisher's exact test for general tables:
data2 <- cbind(data[1,], colSums(data[2:7,]))
data2

# Data looks like this
       [,1] [,2]
red       5   18
blue      1    8
yellow    2   11
green     1   12
pink      7   11

fisher.test(data2)  # Yields: p value 0.2977

Quite the same with the classic $\chi^2$-test:
chisq.test(data2)

# Output
X-squared = 5.6345, df = 4, p-value = 0.2282

Warning message:
In chisq.test(data2) : Chi-squared approximation may be incorrect

Now, the slightly less optimistic version with $(N-1)/N$ correction:
N <- sum(data2)
1 - pchisq(5.6345*(N-1)/N, 4) # Yields a p value of 0.234

Or, calculating degrees of freedom and Pearson's statistic from scratch
N <- sum(data2)                     # Number of obsevations
E <- outer(rowSums(data2), colSums(data2), "*")/N   # Expected cell counts
T <- sum((data2-E)^2/E)                 # Pearson's statistic
df <- prod(dim(data2)-1)                # degrees of freedom
1 - pchisq(T*(N-1)/N, df)                           # Corrected p value

Thus, in your example (no matter what procedure we use) and at the 5% level, we cannot claim that color distribution in Urn 1 is different to the rest. We also cannot claim that the color distribution in Urn 1 is equivalent to the rest.
The discussion and references in the link in your post show advantages and disadvantages of the three procedures (among others). Given that the procedure needs to be simple, it seems to be reasonable to use the $N-1$-corrected version of the original $\chi^2$-test of independence. Please make sure to use the formula for two-dimensional tables, i.e. the test statistic of the $\chi^2$-test of independence (http://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test).
What went wrong in your two attempts?
$\chi^2$: You performed a $\chi^2$ goodness of fit test. This is wrong as you do not know the true color distribution of Urns 2-7. You only know the empiric distribution.
Fisher's exact test: You multiply the relative counts in Urns 2-7 with the sample size of Urn 1, which doesn't make much sense.
Comment: Modified the answer regarding this point. I wasn't aware that computational simplicity is required.
Comment: Thank you Michael, this is very helpful. So in this case can we not apply a $\frac{N-1}{N}$ correction to ensure the $\chi^2$ test produces accurate results? I am investigating this as part of a model that is supposed to run on a web script - which may not be able to access the FortRan and C code that runs behind the `fisher.test` function in R. If it were an option the $\chi^2$ test would be more convenient implementation-wise.


How do I run pipenv, if I always get the same error?

Question: I'm trying to run 'pipenv' on Windows 10, and it doesn't want to run... The error is receive is below
<code>PS C:\Users\charl\Documents\Python\Face Recognition> pipenv shell
pipenv : The term 'pipenv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ pipenv shell
+ ~~~~~~
    + FullyQualifiedErrorId : CommandNotFoundException```
</code>
Comment: try to check if you have install pipenv already in your system. it look like you dont. https://docs.pipenv.org/en/latest/
Comment: I have installed it, today
Comment: check it using the command pip list to look if you have installed right
Comment: There are lots of possible solutions here, including altering your path or calling python -m pipenv: https://stackoverflow.com/questions/46041719/windows-reports-error-when-trying-to-install-package-using-pipenv
Comment: It still doesn't work...

```
pipenv : The term 'pipenv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ pipenv --python 3.7
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (pipenv:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
```
Answer: Use python -m pipenv instead of just pipenv, it should work. Best of luck to you.


IE9: do not track: enable and disable

Question: I tried googling for how to enable and disable "do not track" in IE9 but could not find anything.  I could only find "press releases".
Please let me know the step-by-step on how to enable and disable "do not track" in IE9.
I will then verify the results using the "fiddler http protocol analyzer".
NOTE: In FF4, it is so easy to enable and disable "do not track":
http://support.mozilla.com/en-US/kb/how-do-i-stop-websites-tracking-me?s=do+not+track&as=s
Thanks.
Comment: Flagged for possible cross posting to super-user
Comment: sorry, uSlackr. I am a nube. I found superuser after posting to serverfault and was thinking that superuser might be the better place to post.
Comment: No sweat.  we all live & learn.  Hope the answer help.  Don't forget to accept an answer when you get one
Answer: got this from here

Click on the settings button in the upper-right corner of the browser window (it looks like a gear).
Click on or mouse over "Safety." This will bring up a submenu.
Click on "Tracking Protection." This will bring up the "Manage Add-ons" dialogue box.
Make sure "Tracking Protection" is selected under the list of "Add-on Types." Then click on "Your Personalized List."
Click the "Enable" button in the lower-right hand corner of the dialogue box.
Note: This does something similar to turning on the "Do Not Track" feature in Firefox, although Internet Explorer will also start automatically creating a list of blocked operators by monitoring the third-party content served up by the sites you visit. Rather than waiting to generate a list, users can simply subscribe to a pre-existing one. To subscribe to one such list:
Open up a new browser window.
Go to https://easylist.adblockplus.org/en/
Scroll down to the box that says "EasyPrivacy Tracking Protection List"
Click on the link to "Add EasyPrivacy Tracking Protection list to Internet Explorer 9." This will bring up the "Tracking Protection" dialogue box.
Click on "Add List."
Comment: Thanks uSlackr. I like the detailed response.  I just then found this http://windows.microsoft.com/en-US/windows7/How-to-use-Tracking-Protection-and-ActiveX-Filtering . I like your details better.
Answer: You have to pick a TPL (Tracking Protection List) like the one found here.; once that is enabled DNT is automatically enabled in IE9+.
Comment: Thanks Chris. I did not realize i needed to also go to a "3rd party site" like you point out. I then found this: http://windows.microsoft.com/en-US/windows7/How-to-use-Tracking-Protection-and-ActiveX-Filtering . As an aside, ff4 just seems simpler :  Just enable/disable "Tell websites I do not want to be tracked", done. Not sure what the IE9 implementation buys me.
Comment: IMHO the IE9 way is trying to combine something like Ad Block Plus (who's TPL you can actually use in IE9) with the DNT header. IE9 has no built in ad blocking system (like Chrome does) and no popular add-in to do it (like ABP for FF). I would imagine IE9 sends information on what TPL you're using back to MS too, so they can sell that information to advertising networks (just a guess based on previous experience).

IE9: do not track: enable and disable


Dynamic NROW Variable in read.csv, how to select all rows?

Question: Suppose I have a .csv file which I want to read.csv into R.
Sometimes I want to use the "nrow" option to restrict the rows read into R.
However other times I want all rows to be read in.
How do I add in a variable to dynamically code for the nrow option in read.csv?
For example:

want<-100

testdat<-read.csv("~/username/downloads/mycsv.csv",nrow=want)

The code chunk gives me dataset "testdat" with 100 rows.
Now suppose the mycsv.csv is 1000 rows long, now I want to set the "nrow" value to "all rows" without having to constantly delete and re-input the "nrow" option, I just would like to change the "want" variable.
Answer: We can use Inf as a value
want <- Inf
read.csv("~/username/downloads/mycsv.csv",nrows = want)

Answer: You can try with if else condition -
if(some_condition)
   testdat <- read.csv("~/username/downloads/mycsv.csv")
else
  testdat <- read.csv("~/username/downloads/mycsv.csv", nrows = want)


Asian handicap bets combination

Question: What happens if I bet on 5 games over 1.0/1.5 goals and 3 of them ended over 1.5, but the rest two only with one goal?
How is the outcome calculated?
Answer: This is the wikipedia page that explain asian handicap betting.
In a case of multiple bet you have to calculate in this way:

bet Y
event 1 over 1.5 => win (coefficient X1)
event 2 over 1.5 => win (coefficient X2)
event 3 over 1.5 => win (coefficient X3)
event 4 over 1 => draw (win divided by 2)
event 5 over 1 => draw (win divided by 2)

So in this case your outcome is:
 - Y*((X1*X2*X3)/4)
For each event you can think in this way:

half betting on over 1.0 (you win 5 out of 5 bets)
and half on over 1.5 (you win 3 out of 5 times)
Comment: @KirilMladenov I update the reply because I was thinking about a normal betting on asian matches
Comment: the option is 1.0/1.5 You have a push if you play single, but i am not sure what happens in combination
Comment: as i said that is an asian betting

Asian handicap bets combination


Como clicar e descer uma parte oculta?

Question: Sei que a pergunta é confusa, mas é o seguinte, eu fiz um codigo jQuery pelo codeacademy (jQuery Functions and Selectors 11.Click and Pull), e lá ensina a fazer um algoritmo que achei muito legal, mas ele só funciona lá, no meu browser não funciona, seja no firefox ou no Chrome. Ele é assim:

E depois quando se clica no Slide Up/Down deveria ficar assim:

Porem, no browser ele quando clico nada acontece. Fica só como esta na primeira imagem.

$(document).ready(function(){
    $('.pull-me').click(function(){
        $('.panel').slideToggle('slow');
        });
});
body {
    margin:0 auto;
    padding:0;
 width:200px;
    text-align:center;
}
.pull-me{
    -webkit-box-shadow: 0 0 8px #FFD700;
    -moz-box-shadow: 0 0 8px #FFD700;
    box-shadow: 0 0 8px #FFD700;
    cursor:pointer;
}
.panel {
 background: #ffffbd;
    background-size:90% 90%;
    height:300px;
 display:none;
    font-family:garamond,times-new-roman,serif;
}
.panel p{
    text-align:center;
}
.slide {
 margin:0;
 padding:0;
 border-top:solid 2px #cc0000;
}
.pull-me {
 display:block;
    position:relative;
    right:-25px;
    width:150px;
    height:20px;
 font-family:arial,sans-serif;
    font-size:14px;
 color:#ffffff;
    background:#cc0000;
 text-decoration:none;
    -moz-border-bottom-left-radius:5px;
    -moz-border-bottom-right-radius:5px;
    border-bottom-left-radius:5px;
    border-bottom-right-radius:5px;
}
.pull-me p {
    text-align:center;
}
<!DOCTYPE html>

        <title>Slide Panel</title>

        <link rel="stylesheet" type="text/css" href="Slide.css"></link>

Now you see me!

Slide Up/Down

Os três arquivos estão na mesma pasta e estão nomeados como Slide.html, Slide.css e Slide.js;
Sendo que tanto o html quanto o css já estavam feitos, eu só fiz o javascript. Não sei se é alguma configuração ou algo que tem que estar instalado, mas não acredito que seja nada disso.
Se puderem me ajudar e explicar o que há de errado eu agradeceria muito.
Obrigado.
Answer: Alternativa com CSS3
A quem interessar possa, segue uma versão que faz o proposto, mas sem usar Javascript:

#slideout label {
  position: absolute;
  top: 2px;
  left: 75px;
  width: 150px;
  color: #fff;
  background-color:#c00;
  text-align: center;
  border-radius: 0 0 6px 6px;

  transition-duration: 0.5s;
  -webkit-transition-duration: 0.5s;
  -moz-transition-duration: 0.5s;
}
#slide {
  position: absolute;
  background-color:#fe9;
  width: 200px;
  height: 150px;
  top: -150px;
  left: 50px;
  border-bottom: 2px solid #c00;

  transition-duration: 0.5s;
  -webkit-transition-duration: 0.5s;
  -moz-transition-duration: 0.5s;
}
#toggle:checked ~ label { top: 150px }
#toggle:checked ~ div { top: 0 }

.hide {display:none}

  <input type="checkbox" class="hide" id="toggle">

    Aqui vai seu texto.

  abrir/fechar

Dê um clique no executar, logo acima, para testar em funcionamento.
Breve explicação do que foi feito:
Para termos um estado aberto / fechado, usamos um checkbox escondido. Para acionar este checkbox, o label for nos serve bem, pois um click no label aciona o controle principal.
Uma vez que temos esse toggle funcionando, usamos o pseudo-seletor :checked para diferenciar o CSS do aberto e do fechado, mudando a propriedade top. Para isto, usamos o "seletor de irmão", o ~, que age sobre os controles no mesmo nível do nosso checkbox.
Para animar a descida e a subida, utilizamos o transition-duration, também do próprio CSS. Como a única coisa que muda quando acionamos o checkbox é a propriedade top, o efeito é a rolagem vertical.
De resto, aproveitamos o CSS para estilizar um pouco o slider, basicamente como exemplo.
Comment: Muito interessante, sempre bom aprender novas formas e idéias.
Comment: @AlanRezende eu acho legal complementar com alternativas, assim ficam possibilidades diversas para os futuros visitantes. Assim já temos a sua, que resolve o código do autor, uma alternativa com CSS e quem sabe alguém mais poste alternativas. Esses sliders servem para um monte de coisa.
Answer: Você precisa adicionar o jQuery ao seu código.
utilize o código abaixo antes de seu código javascript.

Ex:
<!DOCTYPE html>

        <title>Slide Panel</title>

        <link rel="stylesheet" type="text/css" href="Slide.css"></link>

Now you see me!

Slide Up/Down

Veja funcionando em http://plnkr.co/edit/pUcRy2L0WpOp3WuGCzcx?p=preview

Como clicar e descer uma parte oculta?


How to do k-fold cross validation with classifiers?

Question: I want to cross-validate a model that plays the card game below (see image).
I trained the model on a dataset of 1000 games, with the goal to maximise the profit of each game. It works great on the training dataset, but I want to k-fold validate it, to ensure that it generalises well.
How can I apply k-fold validation in this case?. This model is not a regressor. It is a classifier, with a variable reward/penalty moreover. So I cannot calculate the MSE of validation folds, there is no MSE here. How can I apply k-fold validation in this case?
Comment: In what sense is this a classifier? In fact, in what sense is it even a supervised algorithm? It seems to be that what you're interested in is the profit of the "model" (the model here being a game-playing algorithm). Right..?
Comment: CLASSIFIER: at every iteration, it has to guess ♥/♦/♣/♠ based on the available data, i.e. the cards that have been already seen in previous iterations. SUPERVISED: when the current card is revealed, you get your learning feedback (i.e. the profit/loss of the table above). Don't get stuck on game specifics. This is just an instrumental example to discuss KFold validation, for a classification problem where classifying right or wrong yields different rewards/costs for each class.
Comment: But in your own words the model is not applied to iterations, it's applied to games. The iterations within games are not independent (you are sampling without replacement). The strategy for the first guess should obviously be very different for the strategy for the 52nd guess. It therefore makes little sense to me to treat that as a standard classification task.
Comment: ok  i will repost with a different example so that the game doesnt get in the way of discussing kfold for a classifier  with different rewards/costs for right/wrong guesses. thanks!
Answer:
This model is not a regressor. It is a classifier, with a variable reward/penalty moreover. So I cannot calculate the MSE of validation folds, there is no MSE here. How can I apply k-fold validation in this case?

Nobody said that Cross Validation only applied to Mean Square Error (i.e. $L_2$ loss) ! In fact you can use CV with a wide range of loss functions..
Without going into the context and details which you can find here: Cross Validation and Confidence Interval of the True Error.. there are a number of loss functions which you can use with CV.

For regression $L_2$ or quadratic loss is commonly used: $L(f,x,y)) = (f(x) - y)^2$
For classification the ${0,1}$ loss is well known $L(f,x,y)) = 1_{f(x) \neq y}$
For comparing algorithms $A_1$ and $A_2$ you can use cross validation with matched pairs
The Jacknife cross validation
Many more....

Have a look at this wikipedia page: https://en.wikipedia.org/wiki/Loss_functions_for_classification and this SE post: What are the impacts of choosing different loss functions in classification to approximate 0-1 loss
Classification metrics in particular
The Sklearn documentation provides a nice summary
http://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics