How to get a data.frame with cases from a contingency table in r?

Question: I would like to reproduce some calculations from a book (logit regression). The book gives a contingency table and the results.
Here is the Table:

    .

    example <- matrix(c(21,22,6,51), nrow = 2, byrow = TRUE)
    #Labels:
    rownames(example) <- c("Present","Absent")
    colnames(example) <- c(">= 55", "<55")

It gives me this:
            >= 55 <55
    Present    21  22
    Absent      6  51

But to use the glm()-function the data has to be in the following way:
(two colums, one with "Age", and one with "Present", filled with 0/1)
    age <- c(rep(c(0),27), rep(c(1),73))
    present <- c(rep(c(0),21), rep(c(1),6), rep(c(0),22), rep(c(1),51))

    data <- data.frame(present, age)

    > data
        present age
    1         0   0
    2         0   0
    3         0   0
    .         .   .
    .         .   .
    .         .   .
    100       1   1

Is there a simple way to get this structure from the table/matrix?
Comment: Coding it by hand seems like the easiest way for a one-off. If you think that you will do this many times, you could build a function that takes the elements of the matrix/table as arguments for the `rep()` functions that you have above. This would be especially easy if the dimensions of the matrix remain as 2X2. If they change, then it would involve more coding.
Comment: Your  age variable defined in `age <- c(rep(c(0),27), rep(c(1),73))` doesn't match the table definition. The 27 subjects aged above 55 should be encoded with a 1 and the 73 subjects aged below 55 should be encoded with a 0.
Comment: Thank you! I didn't notice that.
Answer: reshape2::melt(example)

This will give you,
     Var1  Var2 value
1 Present >= 55    21
2  Absent >= 55     6
3 Present   <55    22
4  Absent   <55    51

which you can easily use for glm
Comment: You can also get this with `as.data.frame(as.table(example))` (but column `value` is `Freq`)
Comment: Also, I may be wrong but not sure it's fit to easily use in glm yet. I wrote an answer that shows how http://stackoverflow.com/a/36508547/4598520
Answer: You could perhaps use the countsToCases function as defined here.
countsToCases(as.data.frame(as.table(example)))
#        Var1  Var2
#1    Present >= 55
#1.1  Present >= 55
#1.2  Present >= 55
#1.3  Present >= 55
#1.4  Present >= 55
#1.5  Present >= 55
# ...

You can always recode the variables to numeric afterwards, if you prefer.
Answer: The code below might look long but only the group_by() and do() instruction deal with expanding the data. All the rest is about changing the data in long format and encoding character variables as 0 and 1. I tried to start from the exact matrix you gave in your question.
Load data manipulation packages
library(tidyr)
library(dplyr)

Create a data frame
Create a matrix as in your example, but avoid ">" signs in column names
example <- matrix(c(21,22,6,51), nrow = 2, byrow = TRUE)
rownames(example) <- c("Present","Absent")
colnames(example) <- c("above55", "below55")

Convert the matrix to a data frame
example <- data.frame(example) %>%
    add_rownames("chd")

Or simply create a data frame directly
data.frame(chd = c("Present", "Absent"),
           above55 = c(21,6),
           below55 = c(22,51))

Reshape data
data2 <- example %>%
    gather(age, nrow, -chd) %>%
    # Encode chd and age as 0 or 1
    mutate(chd = ifelse(chd=="Present",1,0),
           age = ifelse(age=="above55",1,0)) %>%
    group_by(chd, age) %>%
    # Expand each variable by nrow
    do(data.frame(chd = rep(.$chd,.$nrow),
                  age = rep(.$age,.$nrow)))

head(data2)
# Source: local data frame [6 x 2]
# Groups: chd, age [1]
#
#     chd   age
#   (dbl) (dbl)
# 1     0     0
# 2     0     0
# 3     0     0
# 4     0     0
# 5     0     0
# 6     0     0
tail(data2)
# Source: local data frame [6 x 2]
# Groups: chd, age [1]
#
#     chd   age
#   (dbl) (dbl)
# 1     1     1
# 2     1     1
# 3     1     1
# 4     1     1
# 5     1     1
# 6     1     1

table(data2)
#        age
# chd  0  1
#   0 51  6
#   1 22 21

Same as your example except for the age encoding
issue mentioned in my comment above.
Answer: I would go for:
library(data.table)
tab <- data.table(AGED = c(1, 1, 0, 0),
                  CHD = c(1, 0, 1, 0),
                  Count = c(21, 6, 22, 51))

tabExp <- tab[rep(1:.N, Count), .(AGED, CHD)]

Edit: Quick explanation, as it took me some time to figure it out:
In data.table objects .N stores the number of rows of a group (if grouped with by) or just the number of rows of the whole data.table, so in this example:
tab[rep(1:.N, Count)]

and
tab[rep(1:4, Count)]

and finally
tab[rep(1:4, c(21, 6, 22, 51)]

are equivalent.
Same with base R:
tab2 <- data.frame(AGED = c(1, 1, 0, 0),
                   CHD = c(1, 0, 1, 0),
                   Count = c(21, 6, 22, 51))

tabExp2 <- tab2[rep(1:nrow(tab2), tab2$Count), c("AGED", "CHD")]

Answer: So, glm is not quite that inflexible. In part ?glm reads
 For 'binomial' and 'quasibinomial' families the response can also
 be specified as a 'factor' (when the first level denotes failure
 and all others success) or as a two-column matrix with the columns
 giving the numbers of successes and failures.

I'll assume you want to test the effect of age on Present/Absent.
The key is for to specify the response like (in psueudo-code) c(success, failure).
So you need data like data.frame(Age= ..., Present = ..., Absent). The easiest way to do this from your example is to transpose, then coerce to data.frame, and add a column:
example_t <- as.data.frame(t(example))
example_df <- data.frame(example_t, Age=factor(row.names(example_t)))

which gives you
      Present Absent   Age
>= 55      21      6 >= 55
<55        22     51   <55

Then, you can run the glm:
glm(cbind(Present, Absent) ~ Age, example_df, family = 'binomial')

to get
Call:  glm(formula = cbind(Present, Absent) ~ Age, family = "binomial",
    data = example_for_glm)

Coefficients:
(Intercept)       Age<55
      1.253       -2.094

Degrees of Freedom: 1 Total (i.e. Null);  0 Residual
Null Deviance:      18.7
Residual Deviance: -1.332e-15   AIC: 11.99

Addendum
You could also get here via the answer by @therimalaya. But it's just the first step
as.data.frame(as.table(example))

(only gets you part way there)
    Var1  Var2 Freq
1 Present >= 55   21
2  Absent >= 55    6
3 Present   <55   22
4  Absent   <55   51

but to actually have a column of successes and failures, you need to do something more. You could use tidyr to get there
as.data.frame(as.table(example)) %>% tidyr::spread(Var1, Freq)

is similar to my example_df above
  Var2 Present Absent
1 >= 55      21      6
2   <55      22     51


what's the role of Ebmaj7 on this Ebmaj7 D7#9 Gm9 progression?

Question: I'm trying to understand this chord progression:
Ebmaj7 D7#9 Gm9
Ebmaj7 F#dim7 Gm9 Bb13
I know from D7#9 to Gm9, that's a V-i.
But where's the Ebmaj7 coming from? Does it come from the relative major of Gm (Ebmaj7 is the 4th degree of Bb)?
Which role does F#dim7 play here as well?
Comment: Eb is also a tritone away from A, which would be the II in a II-V-i...
Comment: It's not enough context. It can be anything that makes sense. If that progression is looped then it is clearly a Imaj7. Do you realize that F#dim7 has the same note as D7b9(without the D)?
Answer: EbM7 is the VI chord in G minor, a standard predominant chord. VI - V - i is a common progression. F#dim7 is the leading-tone chord (vii) in G minor — it's standard to raise the seventh degree in minor, particularly when leading back to the tonic — and serves the same purpose as a V chord; thus, VI - vii - i. The Bb chord is the V relative to Eb, so allowing the progression is a cycle, it serves to lead back to the beginning.
Answer:
where's the Ebmaj7 coming from?

This might seem like a controversial or even wrong statement, but you should consider relative major and minor as being one bi-polar key. It's like a person standing, sometimes her weight is more on the major foot and sometimes on the minor foot. There's a lot of music where it's completely ambiguous which is more "home", the minor or the major. It would make things easier and more honest to a lot of pop/rock/jazz music if everyone standardized on "I" meaning the major key's tonic and "vi" as the relative minor key's tonic within the same key signature.
In my experience, insisting that relative minor and major keys are completely separate things causes more problems than it solves.
In your progression the Ebmaj7 is the IV relative to Bb, or if you insist on the tune being in G minor, then it's a VI relative to Gm. But if later in the song, the balance shifts more towards Bb major being the center, then you'll have to change all the numbers. Is it worth it? The problem goes away if you look at keys as having two centers simultaneously, the major center and the minor center.
Try replacing every Ebmaj7 with Cm9  - not a very big change IMO:
<code> Cm9  D7#9  Gm9
 Cm9  F#dim7 Gm9 Bb13
</code>
But how could we make Bb feel more like being the home note. How about this, how does the minor/major balance shift?
<code> Ebmaj7 D7#9/A Bbmaj7
 Ebmaj7 F#dim7 Gm9 Bb13
</code>
What if you swap the lines, is this more like "in Bb" if it ends on Bb?
<code> Ebmaj7 F#dim7 Gm9 Bb13
 Ebmaj7 D7#9/A Bbmaj7
</code>
Even if you think G or Bb would be the more likely tonic, is a full re-numbering of everything needed, and does it change the way you treat the chords?
Comment: I tend to agree with your "one bi-polar key" comment. There is no need to say "the song modulates from G to Em here". However I do think it is sometimes a good idea to label a song as being specifically in a minor or a major key when the home base is clearly established as one or the other.
Comment: @JohnBelzaguy I mean this more about how said things should be understood, not how things should be said. It is useful to say that a song is in such and such minor or major key, but it should be understood as meaning the minor or major side of a minor/major pair. All of the harmonic possibilities of both sides are simultaneously available all the time, regardless of which side happens to have more emphasis.
Comment: Agreed, I'll buy that.


How to send a signal to a Docker container without affecting restart policy?

Question: TIL that docker kill means kill in the sense of "make it dead", as opposed to the POSIX sense of "send a signal".
We have several containers that we need to send a SIGHUP to in order to reload configuration, but this causes them to ignore their restart policy of "always", which isn't what we want.
What's the best way to send signals to these containers without affecting their ability to auto-restart?

To more clearly demonstrate the issue we're seeing, take the following example.
We have some container that has a restart policy set to always
$ docker inspect cloudwatch-exporter | jq .[].HostConfig.RestartPolicy
{
  "Name": "always",
  "MaximumRetryCount": 0
}

We reload the config at some point using docker kill:
$ docker kill --signal=SIGHUP cloudwatch-exporter
cloudwatch-exporter

Some time later, something happens that kills the process. To simulate this, I'll send a signal within the container:
$ docker exec cloudwatch-exporter bash -c "kill 1"

At this point, the container is dead and won't restart:
$ docker ps -a | grep cloudwatch-exporter
c7827204bba5        prom/cloudwatch-exporter:cloudwatch_exporter-0.8.0                                "java -jar /cloudwat…"   20 hours ago        Exited (143) 3 minutes ago                            cloudwatch-exporter

What alternatives do we have for using docker kill? For many cases docker exec works, but it won't work for any container that only contains a single static-linked binary.
Answer: This is a known Docker bug.
See https://github.com/moby/moby/issues/11065 "Non-fatal signals break restart policies"
See https://github.com/moby/moby/issues/41302 "Signal breaks unless-stopped restart policy"
Bypass the problem by doing docker exec "kill -HUP 1".
Comment: ....and 7 years later, they still didn't fix this behaviour :/
Answer: With the --signal option to docker kill?
docker kill --signal=SIGHUP <container>

Or with docker exec?
docker exec <container> killall -HUP nginx

Comment: I'm not as concerned with why the process stopped as I am with why it didn't restart. FWIW, in the specific case I ran into today it was a startup timing issue with our deploy automation. We sent a SIGHUP to a container right after it started, and the container promptly died. My guess is that we sent the signal before it could register its signal handler. But that's irrelevant. Whether its a signal handler, segfault, or OOM, I'm expecting Docker to restart my container. The docker kill command stops that from working, so I'm looking for alternatives.
Comment: We're seeing docker kill interfere with the restart policy, which is what I'm trying to avoid. The kill and killall are usually shell builtins, so you'd have to wrap it in bash -c "killall -HUP nginx" or something. But that won't work with images that are single statically linked binaries.
Comment: Did you actually specify the signal?
Comment: Yes, we specified the signal. I edited the question to be more specific about what we're seeing.
Comment: You sent the signal correctly. Now you should investigate why the process is stopping. This focus on sending the signal seems like a red herring.


How do I manage a 3rd party jar dependency with Eclipse, Maven and Jenkins?

Question: We're trying to manage a project made of multiple Eclipse plug-ins.
One plug-in has a dependency to a 3rd party plug-in. It imports a class from a library named bpmn2. This library is a jar file and NOT included in the Maven remote repository.
Locally, in Eclipse, we have the library checked out into the workspace and referenced in the classpath of the Eclipse project. The plug-in manifest doesn't explicitly state the dependency to bpmn2. Which works locally...
We try to use Jenkins for continuous integration. The Multi-Plug-in-Project is managed using Maven and multiple POM files, using the Maven tycho plug-in.
The problem is that Maven doesn't care for the locally present library bpmn2 (of course). So we thought that using Maven install:install to install bpmn2 to the local Maven repository
./mvn install:install-file -Dfile=/home/someUser/bpmn2/org.eclipse.bpmn2_0.7.0.201111021300.jar -DgroupId=org.eclipse.bpmn2 -DartifactId=bpmn2 -Dversion=0.7.0 -Dpackaging=jar -DlocalRepositoryPath=/var/lib/jenkins/localRep/

and adapting the corresponding POM with a dependency entry for the library
<dependencies>
    <dependency>
      <groupId>org.eclipse.bpmn2</groupId>
      <artifactId>bpmn2</artifactId>
      <version>0.7.0</version>
      <type>jar</type>
    </dependency>
  </dependencies>

would work. But it didn't.
The output of Maven is:
[ERROR] Cannot resolve project dependencies:
[ERROR]   Software being installed: modeltype.bpmn2 1.0.0.qualifier
[ERROR]   Missing requirement: modeltype.bpmn2 1.0.0.qualifier requires 'bundle org.eclipse.bpmn2 0.7.0' but it could not be found
[ERROR]

The question is:
How could we better integrate the 3rd party library?
The local classpath reference is not the optimal solution for the greater picture, I think. Should everyone who develops for the project install the bpmn2 library and only use the dependency in the manifest?
And what are we doing wrong with Maven? The local repository is
/var/lib/jenkins/localRep

and after installing the library to the repository, it seemed that the created dir structure was okay.
/org/eclipse/bpmn2/bpmn2/0.7.0/bpmn2-0.7.0.jar

Can somebody help?
Answer: The simplest solution would be to set up a repository server for your company and proxy all your calls through that server.
I use Nexus from Sonatype.
It acts as a proxy when you need to download artifacts from remote locations, like maven central, but it also has an ability to setup repositories to store non-publcicly distributed artifacts.
The whole setup process is very well documented here -> http://www.sonatype.com/books/nexus-book/reference/.
Note, that open-source edition of the product is very good and is enough for your purposes.
Just to be fair, there is a competing product called Artifactory.  You may read about it here -> http://www.jfrog.com/products.php
Comment: Will this repository server be able to use the local repository and work with it?

Also, if I want to just make it work on Jenkins, is there are simpler solution?
Comment: @danowar. 1) Your local repository is really oblivious whether artifacts are coming from maven central or from the intranet server. 2) You could setup a build step to install a 3rd party jar to a local repository during Jenkins build, but to me it sounds more complex than setting this server once and forgetting about it.
Answer: I had a similar scenario where I need to include a third party library (not loaded in maven repository) in Maven Web project.
Following approach helped us make the code portable.
Solution:
Created the "lib" directory under WEB_INF as below:
<<Project_Base_Dir>>/src/main/webapp/WEB-INF/lib

Copied the third party JARs to LIB directory.
Updated POM.xml to use the SYSTEMPATH as below:
<dependency>
    <groupId>GROUP_ID</groupId>
    <artifactId>ARTIFACT_ID</artifactId>
    <version>VERSION</version>
    <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/<<JAR_FILENAME>></systemPath>
    <scope>system</scope>
</dependency>

Answer: If you can't use Nexus, you can use scope system :
Dependency Scopes

system: This dependency is required in some phase of your
  project's lifecycle, but is
  system-specific. Use of this scope
  is discouraged: This is considered an
  "advanced" kind of feature and should
  only be used when you truly understand
  all the ramifications of its use,
  which can be extremely hard if not
  actually impossible to quantify.
  This scope by definition renders your
  build non-portable. It may be
  necessary in certain edge cases. The
  system scope includes the
  <systemPath> element which points to
  the physical location of this
  dependency on the local machine. It is
  thus used to refer to some artifact
  expected to be present on the given
  local machine an not in a repository;
  and whose path may vary
  machine-to-machine. The systemPath
  element can refer to environment
  variables in its path: ${JAVA_HOME}
  for instance.

You can use it with
Be careful, using this is probably making your build not portable.
Comment: Never use system scope cause the produces many problems...and make builds depending on a specific machine....


Create JIRA Ticket with JIRA Version 3.12

Question: I want to Automate JIRA ticket create process,for which i have got below links stating to use JIRA Rest API.
1.JIRA REST API link
2.Code project Link
But our current JIRA version is :(Professional Edition, Version: 3.12.3-#302), which we can't upgrade it to newer versions 5.0 or later.When executing this API code by using required credentials and parameters, i am getting below error as return message.

It seems,JIRA REST API is made applicable from version 5.0 or later. If anyone has any other way achieving this,let me know.
Answer: Before the REST APIs for JIRA were added (this started in JIRA 4), there were SOAP and XML-RPC interfaces (they were removed in JIRA 7).
You can still find links to documentation for older JIRA versions on this page.
It seems that the online JIRA 3 documentation does not work very well anymore though, but you can try the docs for JIRA 4 instead. They seem to have working links. Ie:

Developer tutorials
JIRA RPC Services


Prove that for every positive integer, this polynomial is divisible by 8

Question: prove that: $$8\mid (n-1)n(n+1)(n+2)$$
I tried to simplify this expression but had no luck.
Comment: Among four consecutive integers, one is divisible by $4$, and another is divisble by $2$, hence the product is divisible by $8$.
Comment: Of course you had no luck in *simplifying*. That's not possible here.
Comment: hi thanks for all the answers i finally resolved the problem in this question can you please close my question
Answer: With four consecutive numbers, you can write them like $4m, 4m+1, 4m+2, 4m+3$ (or possibly a shifted version of this like $4m-1, 4m, 4m+1, 4m+2$). In any case, taking four consecutive natural numbers, one of them is divisible by $4$ and a separate one is divisible by $2$. Thus the product is divisible by $8$.
Answer: Hint:
prove that of $4$ consecutive numbers at least one is a multiple of $4$, and that one of the others is a multiple of $2$.
Comment: i know that (n-1)n is divisible by 2. but why (n+1)(n+2) is divisible by 4?
Comment: you have to consider the cases $$n\equiv 0,1,2,3,4,5,6,7\mod 8$$
Comment: @AlonBerman What's the difference between consecutive multiples of $4$?
Comment: @Dr.SonnhardGraubner Actually, no. Mod 4 is enough.
Comment: Can you prove that at least one of $n-1,n,n+1,n+2$ is a multiple of $4$? If e.g. $4|n$ then $2|n+2$ and you are ready. Also in the other cases there is a way out.
Comment: but $\mod 8$ is also possible
Comment: @Dr.SonnhardGraubner Yes, possible. But let's keep it as simple as possible.
Answer: For an integer $n>1$ the number $${n+2\choose 4}$$ is an integer, we recognize that
$$3{n+2\choose 4}=3\cdot\frac{(n+2)(n+1)n(n-1)}{4!}=\frac{(n+2)(n+1)n(n-1)}8$$
Hence $(n+2)(n+1)n(n-1)$ is a multiple of $8$.


Drupal site menu overlay stuck in Chrome Browser when scrolling down

Question: I noticed a strange behavior in my drupal Portal when using Chrome. There is an overlay at the top of the page. In Chrome, that overlay gets "stuck" on the page and you can't easily get rid of it, and it starts constantly flickering. If I refresh its fine and when I scroll down the problem appears again.
Any help or suggestions are appreciated. Thank you.
Please find the attached screenshot here:

enter code here

.fixed#header {
    position: fixed;
    top: 0;
    left: 0;
    padding: 18px 0;
    width: 100%;
    background-color: #fff;
    background-color: rgba(255,255,255,.95);
    -webkit-box-shadow: 0 2px 3px rgba(0,0,0,.2);
    -moz-box-shadow: 0 2px 3px rgba(0,0,0,.2);
    box-shadow: 0 2px 3px rgba(0,0,0,.2);
    transform: translateZ(0);
}
style.css?onmqdt:859
header {
z-index: 99;
padding-top: 40px;
background-color: #fff;

}
Comment: Hello! This doesn't look like the work of the overlay module. Is it a custom overlay?
Comment: Hi, it's custom overlay, but its working fine in IE, Firefox browsers. only stucks in Crome browser.
Comment: Ok. Then its probably a CSS issue. Can you post the CSS for the overlay?
Comment: .fixed#header {
    position: fixed;
    top: 0;
    left: 0;
    padding: 18px 0;
    width: 100%;
    background-color: #fff;
    background-color: rgba(255,255,255,.95);
    -webkit-box-shadow: 0 2px 3px rgba(0,0,0,.2);
    -moz-box-shadow: 0 2px 3px rgba(0,0,0,.2);
    box-shadow: 0 2px 3px rgba(0,0,0,.2);
    transform: translateZ(0);
}
style.css?onmqdt:859
#header {
    z-index: 99;
    padding-top: 40px;
    background-color: #fff;
}
Answer: Hard to say without being able to inspect it, the z-index on #header won't work unless position is set to something other than static (the default value).
Try setting #header to be position: relative; or position: absolute; whichever is more appropriate and see if that helps, or at least helps narrow down the problem.
Comment: i changed to position: relative & absolute, but it didn't worked. any other suggestions ? Thanks Wes Ruvalcaba for the reply.
Answer: i added these four lines of code to style.css file
position: fixed;
        /* apply these 4 lines only for chrome browser */
        -bracket-:hack[;
        position: sticky;
        position: -ms-sticky;
    ];
and reduce the speed in sticky.js file from 300 to 1.
i have done these. now problem is resloved.


Is there any real documentation or code examples on how to use the expect4j library?

Question: The documentation on the google code project page is pretty thin. I'm wondering if there is any in depth documentation on using expect4j; code examples would be helpful as well.
Comment: Added link; please revert if incorrect.
Answer: I found some examples here:
http://code.google.com/p/expect4j/source/browse/#svn/trunk/src/test/java/expect4j%3Fstate%3Dclosed
Usually with very small libraries like this don't expect walls of documentation


Abrir arquivo texto e tratar por linha

Question: Estou começando os primeiros passo em go e estou precisando primeiramente pegar um arquivo texto abrir ele e filtrar por linha onde cada linha está separado por | e vou fazer um if pra mostrar determinada linha
Exemplo:
Conteúdo do arquivo texto:
|C100|1|0||55|05|1|000002397||||||||||||||||||||||
|C100|1|0||55|05|1|000002399||||||||||||||||||||||
|C100|1|0||55|05|1|000002425||||||||||||||||||||||
|C100|1|0||55|05|1|000002371||||||||||||||||||||||
|C400|2D|ECF|asdasda2330|002|
|C405|02012012|3|1036|116526|6001060,09|5693,36|
|C420|DT|151,78|||
|C420|01T1700|429,50|01||
|C420|F1|5112,08|||
|C460|2D|00|116397|02012012|25,00|0,16|0,75|||
|C470|0000008556|2,000|0|RL|25,00|060|5403|0|0,16|0,75|
|C460|2D|00|116398|02012012|27,67|0,19|0,83|||
|C470|0000015177|1,000|0|RL|5,60|060|5403|0|0,04|0,17|
|C470|0000022346|1,000|0|RL|5,60|060|5403|0|0,04|0,17|
|C470|0000025882|1,000|0|PC|6,09|060|5403|0|0,04|0,18|
|C470|0000025885|2,000|0|PC|10,38|060|5403|0|0,07|0,31|
|C460|2D|00|116399|02012012|19,90|0,13|0,60|||
|C470|0000000247|3,000|0|RLO|10,20|060|5403|0|0,07|0,31|
|C470|0000020634|1,000|0|PC|9,70|060|5403|0|0,06|0,29|
|C460|2D|00|116400|02012012|10,52|0,07|0,32|||
|C470|0000004762|1,000|0|CAT|1,12|060|5403|0|0,01|0,03|
|C470|0000008873|1,000|0|PC|1,94|060|5403|0|0,01|0,06|
|C470|0000016902|1,000|0|PC|3,68|060|5403|0|0,02|0,11|
|C470|0000023287|1,000|0|PC|1,26|060|5403|0|0,01|0,04|
|C470|0000023303|1,000|0|PC|1,26|060|5403|0|0,01|0,04|
|C470|0000023308|1,000|0|PC|1,26|060|5403|0|0,01|0,04|
|C460|2D|00|116401|02012012|5,00|0,04|0,16|||
|C470|0000009772|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|
|C470|0000009789|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|
|C470|0000009826|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|
|C470|0000009842|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|

Preciso primeiro abrir o arquivo e armazenar todo conteúdo dele e depois fazer tipo um explode de cada linha colocando em um array
feito isso vou fazer um if linha[1] == "C460" e mostra o conteúdo todo dessa linha
package main

import (
    "fmt"
    "io/ioutil"
    "strings"
)

func main() {
    conteudo,_ := ioutil.ReadFile("sped.txt")
    linhas := strings.Split(string(conteudo),"|")
    for _,v := range (linhas){
        if v == "C100"{
            fmt.Println(v) // preciso mostra a linha toda do registro C100
        }
    }
    fmt.Println(linhas[1])

}

Comment: Se você quer pegar a linha, o seu `strings.Split` deve considerar o separador `\n` e não `|`
Answer:
Não utilize split, ele remove o carácter passado no segundo parametro e retorna uma string do resultado do conteúdo.
Quando você fez um for você comparou se todo o arquivo é igual a C100, mas você precisa comparar coluna por coluna de cada linha.

Fiz com expressão regular, veja se é isso, neste caso é só trocar o nome pelo conteúdo exemplo: C100,C470
package main

import (
    "fmt"
    "io/ioutil"
    "regexp"
    "strings"
)

func main() {
    // abrindo arquivo sped.txt
    conteudo, _ := ioutil.ReadFile("sped.txt")

    nome := "C100" // nome que irá procurar...

    // expressão regular que verifica se o nome existe, se existe ele pega todo conteúdo
    r, err := regexp.Compile("(?P<name>" + nome + ").+")

    // verifica se houve algum erro ao pegar o conteudo
    if err != nil {
        fmt.Printf("Ocorreu um erro: %v", err)
    }

    // cria um slice de string para cada linha de string encontrada
    x := strings.Fields(string(conteudo))

    // percorre todo o slice de string
    for _, a := range x {
        fmt.Println(r.FindString(string(a))) // retorna apenas as linhas pelo nome escolhido
    }
}

Link para o código completo, https://play.golang.org/p/U_dniiFOeJ porém não vai funcionar porque dentro do servidor não tem o arquivo sped.txt, então você copia e executa localmente.

Abrir arquivo texto e tratar por linha


Querying backslash

Question: I am trying to query a table that has strings with backslash (/). For example strings are:
test
test/test
test4
test5
When I use wild char I do not get "test/test" in the result set. I searched web for including backslash in queries, but couldn't find a solution. Does any one have any idea about this issue?
EDIT:
it is a simple query like:
<code>SELECT * FROM aTable;
</code>
Comment: Can you show some code? What have you tried?
Comment: How are you viewing the results? backslash has no particular significance in SQL Server. Have you tried your query in SSMS (or Enterprise Manager/Query Analyzer)?
Answer: I believe that you should escape backslashes. I've needed to do it when I used PHP's echo statement.
You can escape backslashes by typing a backlash before the backslash you want to escape
Like: \\
You might want to also add backticks to the table name
`aTable`, or there;s a chance that it won't work.

Querying backslash


Sencha Touch : Refresh Store Content

Question: I want to Refresh my store content, I have a to display list of tickets and for each ticket there are list of attachments. Attachments are optional so For a ticket there maynot be an attachment.
I am using Odata proxy based on Rest Proxy.
I have 2 separate store for ticket and attachment. If I load the attachment store based on a ticket which has attachment, the store gets populated, now when I select a ticket which has no attachment the store doesnt get refreshed but is showing me the store content from the previous ticket. If I try to use "RemoveAll" method on the store, the store tries delete the entries in the Backend system which  I dont want. I just want the store to refresh its content on each request.
what is the way to reload the store and refresh its content
thanks!
Answer: Did you try with:
<code>Ext.getStore('myStore').load();
</code>
Comment: in case the user isn't aware the string passed to the getStore function is the value set in your storeId property when defining your store.

Sencha Touch : Refresh Store Content


Date Filter in drupal

Question: Can anyone help me how to expose the date filter in a view in drupal 7.
I have added post date filter with in FILTER CRITERIA but it is not working
Can anyone help me
Answer: Here is how I used exposed filter to filter content with date (assumed you have the Date Views module enabled).
I just create a view with all fields and the in the "Advanced > CONTEXTUAL FILTERS" section, I add a new contextual filter and I select Date : Date (node). In the next box, I can select the date field to filter (publication date or any date field). I can also give a default value if no date parameters is passed through the url : for example, the current date.
And that's it, I just pass the date as URL parameter : I have something like
<code>http://mydomain.com/agenda/all/2012-03
</code>
where /all is my first parameter, I us it to match a country for my posts, and the second parameter is the month of the post.
I hope that helps.
Comment: I think the module Date Views is not enabled
Comment: I am not able to find Date:Date(node) value in contextual filter
Answer: You can add "your date filed" as filter and can expose the filter. so that the filter will appear in the front end, using that we can display the nodes that are created between two given dates.

Date Filter in drupal


Is there a program (webbased or not) that allows me to draw freeform on a graph?

Question: I need to draw some graphs for a distance-based course I'm taking where I need to draw graphs that are based on my own estimates, not on a function. All of the websites I'm finding online are based around submitting a function and the app plots the graph. How can I plot a graph by hand on the computer?
Answer: You can use any drawing program. The common free one is InkScape. Non-free ones are Adobe Illustrator, Powerpoint, etc, etc.
See this answer for some other suggestions.


.js extension for javascript file is missing

Question: I am using below code in my custom module "my_module".
    function my_module_theme(){
        return array(
                'my_module_javascript'=>array('arguments'=>array(),),
        );
 }

 function my_module_init(){
        theme('my_module_javascript');
 }

 function theme_my_module_javascript(){
        drupal_add_js(drupal_get_path('module','my_module').'/my_module.js');
 }

After I enable the module page source is showing the javascript file without .js extension.
like src="modulepath/my_module?lrmjns"
Am I doing anything wrong? why is it skipping .js?
Answer: What version you are using? And are you sure the my_module.js is exist?
I think the best way is using hook_library to add js and css for your module:
function my_module_library() {
  $libraries['my-module'] = array(
    'title' => 'My custom module library',
    'version' => '1.0',
    'js' => array(
      drupal_get_path('module', 'my_module') . '/my_module.js' => array(),
    ),
  );
  return $libraries;
}

function my_module_init() {
  drupal_add_library('my_module', 'my-module');
}

Comment: Sorry my mistake, there is one more file being added with correct extension because of the typo in my .info file. By the way, is there any difference between these methods? drupal_add_js, drupal_add_library and script[] ='mymodule.js' in info file?
Comment: [drupal_add_js](http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/7) is the low level to add a Javascript file.

script[]='mymodule.js' will always add Javascript file to Drupal, can't lazy load.

[hook_library](http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_library/7) allow your module and other module can add Javascript, CSS file which version support in easy way and when you need to upgrade your Javascript source (and keep current source), only one modify need.


Monkey-patching jest object for all test files

Question: How can I monkey-patch some methods in the global jest object for all test files at once? I don't want to add any extra code to my test files, it has to be done somewhere in setup and it can be an ugly hack.
I tried doing that from a custom environment, setupFiles and setupFilesAfterEnv, but it looks like they all get a different instance of jest object and my changes aren't visible in test files.
Disclaimer: I know that it's a bad thing to do but I need it for some one-time benchmarking only and it's the easiest solution that gets the job done.
Comment: I haven't gotten this working yet, but I found an article that describes something similar. It says to monkey patch the `jest-runtime` module. http://www.petecorey.com/blog/2019/03/25/bending-jest-to-our-will-restoring-nodes-require-behavior/
Answer: I got this working! You're right, Jest does re-construct the global jest object for every test case, but you can override the function it uses to do that. In jest.config.js, set globalSetup to something like <rootDir>/jest-global-setup.js. Then, in jest-global-setup.js, add this:
const jestRuntime = require('jest-runtime');

const { _createJestObjectFor } = jestRuntime.prototype;

jestRuntime.prototype._createJestObjectFor = function(...args) {
    // Call the original function to create a normal jest object.
    const jestObject = _createJestObjectFor.apply(this, args);

    // Apply your changes.
    jestObject.isMonkeyPatched = true;

    // Return the patched object.
    return jestObject;
}

// Jest expects to find a function of some sort as well,
// but we don't need it for this example.
module.exports = function() { /* do nothing */ }


Switch causes a method to become non static? So do if statements?

Question: What causes a method to become "Non-Static"?
I've been trying to get this coin toss to work. Sadly a few decades (at least it feels like it) passed  and all I figured out is that if (feel free to correct me) a if statement/switch is inside a method, it makes the method non-static. So how does one get around this error. Important stuff to know: p1Choose starts out true. I want this method to return true if the person won the coin toss (if p1Choose is true then its player 1 and if it is false then it is player 2).
public boolean cointoss() {
  int toss = Random.nextInt(1); // Or is this random causeing it????
  String HorT;
  String callHorT;
  boolean returnMe = true;
  if ( toss == 0) {
    HorT = "Heads";
  } else {
    HorT = "Tails";
  }
  if ( p1Choose == true ) {
    System.out.println( p1Name + " choose Heads or Tails!");
    callHorT = scan.next();
    if( HorT != callHorT) {
      System.out.println("You lose!");
      returnMe = false;
    }
    if( HorT == callHorT) {
      System.out.println("You lose!");
      returnMe = true;
    }
  } else if ( p1Choose ==false ) {
    System.out.println( p2Name + " choose Heads or Tails!");
    callHorT = scan.next();
    if( HorT != callHorT) {
      System.out.println("You lose!");
      returnMe = false;
    }
    if( HorT == callHorT) {
      System.out.println("You lose!");
      returnMe = true;
    }
  }
  if (p1Choose == true ) {
    p1Choose = false;
  }
  if (p1Choose == false) {
    p1Choose = true;
  }
  return returnMe;
}

I get this error saying non-static method cannot be refrenced by a static context
Answer: You're barking up the wrong tree.  The presence or absence of the static keyword is the only thing that makes a method static or not.
Comment: It's an ill-wind that blows nobody good: your upvote of my answer put my reputation over 10,000 and now I have Moderator privilege.  Years from now, historians will look back and lament, if only he used his privilege for good, instead of for evil...
Comment: So what do I have to do... put static in the method top?
Comment: @OladamOladam -- yes, in the function declaration: `public static boolean cointoss()`
Comment: Happy "Moderator privilege" acquisition day!
Answer: I don't understand why you are thinking like that. I don't see anything in your code, that may force you to think like that.
A method can be static if it have a static modifier in its signature. Other then it can't be static. A static or non-static does not depend on the internal structure of the method.
But, there is one thing, you can't use any non-static method or variable from your static method. This part may trouble you to think like that your are now thinking. When you use if-else you may use some non-static variable but when use switch you may does not need to use those.
Request you to check this part.
Answer: I think you're misinterpreting your error...

int toss = Random.nextInt(1); // Or is this random causing it????

nextInt() is not a static method on Random.  Try this instead:
Random r = new Random();
int toss = r.nextInt(1);

Comment: In the top of my class i constructed a Random named "Random". Wouldn't that be the same thing as    Random r = new Random();


shortcut method for calculating a determinant

Question: \begin{vmatrix} b+c & a & a \\ b & c+a & b \\ c & c& a+b \end{vmatrix} = 4abc
Is there any shortcut method to compute this determinant without breaking by definition. Need some help please.
Comment: What's wrong with computing it from the definition? It's only $3\times3.$
Comment: No, there is no shortcut.
Comment: to simplify the calculation, try to do row operations
Answer: $$\begin{vmatrix}
b+c & a & a \\ b & c+a & b \\ c & c& a+b
\end{vmatrix} \xrightarrow{-R_1+R_2} \begin{vmatrix}
b+c & a & a \\ -c & c & -a+b \\ c & c& a+b
\end{vmatrix} \xrightarrow{-R_2+R_3} \begin{vmatrix}
b+c & a & a \\ -c & c & -a+b \\ 2c & 0 & 2a
\end{vmatrix} \xrightarrow{-0.5R_3+R_1} \begin{vmatrix}
b & a & 0 \\ -c & c & -a+b \\ 2c & 0 & 2a
\end{vmatrix} \xrightarrow{-0.5R_3+R_2}  \begin{vmatrix}
b & a & 0 \\ 0 & c & b \\ 2c & 0 & 2a
\end{vmatrix} = b(2ac) - a (-2bc) = 4abc$$
Answer: The Rule of Sarrus immediatley gives the result of the determinant. Of course, it also involves a calculation. But it has a geometric interpretation (in German "Zaunregel"), which is helpful for the computation.


Old 16-bit Application Causing GPF in WIN87EM.DLL, intermittently

Question: I've got an old 16-bit application, that was developed for Windows 3.1.  It preforms some calculations and is part of a more complex system.  The system sets up the inputs for the program, and collects the output results.
Unfortunately, the 16-bit program is here to stay for the mean time, so we have to work around the frustrations it causes on modern operating systems.
The system runs on Windows XP, and on physical Windows XP machines it runs alright.  The machine I'm having a problem with, is a Windows XP instance running on VirtualBox (version 4.1.12) on a Debian box.  The physical computer is an HP Proliant server, with Quad Core Xeon 3.4 Ghz. I'm using remote desktop to access the computer from my Windows 7 box.
The error I'm getting is, "PROGRAM caused a General Protection Fault in WIN87EM.DLL at address : 0001:02C9".  The annoying thing is, at times it works and other times it doesn't, making troubleshooting all that more frustrating.
From trawling the internet, I've come across a few sites that mention the same problem.  None of them seem to offer real solutions, except to say that WIN87EM.DLL supplies floating point routines, and has some issues with certain printers.
I've uninstalled all printers on the virtual machine, I've also tried installing a PDF writer and setting it as the default printer - so that there is a printer on the machine.  I've disabled resource sharing with my Remote Desktop connection.  I've updated the Virtual Machine Guest drivers on the machine.  I've also tried setting the compatibility to Windows 95 in the properties of the executable.
Any pointers for troubleshooting this problem, or methods I could try to get it working?
Comment: I'd try to debug it and see what it's doing wrong and how it gets there.
Comment: What do you recommend to use for debugging?
Answer: This was the same error I had with Microsoft XP Mode.
Obviously WIN87EM.DLL has Problems with virtualized processors.
My Solution: I "unloaded" the XP-Version of WIN87EM.DLL in the registry (search and delete every item with this name), and copied a much older version into application folder. The old version can be found her: http://support.microsoft.com/kb/86869/de
Good luck!
Comment: So far, out of all the answers on the internet, this one works much better (not 100%, still GPF in some cases, but the speed is 1000% better. All of these Hide87 Solutions ignore the fact that you loose the co processor which will bring drawing/cad/floating point apps to a crawl
Comment: Here is an updated link to find the patch for the old version of the DLL http://download.microsoft.com/download/win31/Patch/1.0/WIN/EN-US/ww0548.exe
Answer: This question is old but I had this exact win87em.dll crash with some 16-bit factory automation software running natively on windows 7. By following the method of HIDE87.com and editing autoexec.nt I was able to make the software stop crashing so that I could make edits.
This machine was running Intel 8 Series/C220 Series chips. I attribute this configuration to the crash because I have used this same 16-bit software on tons of other windows 7 machines for years now.
edit: here's the steps I used to fix the problem

Download winfloat.exe from http://www.conradshome.com/win31/archive/
Open winfloat.exe with 7zip. Find HIDE87.com and extract it to desktop.
Copy HIDE87.com to C:\Windows\System32\
Open c:\windows\system32\autoexec.nt with notepad
At top of file, after first group of comments add the following

lh %SystemRoot%\system32\HIDE87.com

Add a comment above your last line

REM Fix for Gen. Protection Fault in win87em.dll

Save changes to autoexec.nt and reboot pc.
Answer: Video Driver win87em.dll
This is the step by step resolution to the problem we had with the "win87em.dll" issue.

Left-Click the START button in the bottom left corner of the screen.
Right-Click My Computer and left click Properties.
Left-Click the tab at the top that says Hardware
Left-Click the button that says Device Manager.
Left-Click the + sign next to Display Adapters near the top of the list.
Right-Click the items shown in the expanded list under Display Adapters and left-click Disable.
Left-Click the Yes button that shows when windows asks if you are sure you want to disable it.
Left-Click the No button when windows asks if you want to reboot.
Repeat the disable process for each item listed under Display Adapters (usually only one or two)
Reboot the PC and the win87em.dll General Protection Fault errors should go away.

This is only applicable for users on Windows XP.  Most likely the display adapters listed will be shown as an Intel G41 internal display adapter, but it may be another Intel device.  If this does not fix the issue then it is likely a bad printer driver causing the problem.
Disabling the video adapter will not hurt windows.  It will make their computer unable to watch videos or play 3D games, but windows will still run and look fine.  (They will probably need to change their screen resolution after rebooting.)
Answer: VirtualBox 4.3.16 should also have a fix. See https://www.virtualbox.org/ticket/12646  If you want the fix immediately, you'll have to build VirtualBox from OSE sources.
Update: VirtualBox 4.3.16 containing this fix is now officially released.
Answer: I know this is an old thread but I came across it while searching as I was having the same issue under Windows XP running VirtualBox. Eventually I found the following:
https://communities.vmware.com/people/jmattson/blog/2012/03
This is for VMWare and seems to have fixed the issue, couldn't find anything similar for VirtualBox but as VMWare Player is free it is a good workaround for anyone having this problem.
Answer: in the case of virtual machines - vxBOX (tested) of VM ware (maybe)
you just have to switch off all para virtualization options in the processors section of VX BOX options.
works like magic!


Do data transformations before factor analysis need to be consistent across different variables?

Question: (This question continues the previous one)
I am creating a questionnaire, and I have identified 3 questions which are skewed (2 positively skewed & 1 negatively skewed). I successfully transformed two of the questions using Lg10 and inverse of Lg10 on SPSS, but the second positively skewed question is still positively skewed even after the Lg10 transformation. My questions are the following:

Is it "okay" for the question to still be positively skewed after the transformation? Is any further action needed (any further transformation(s))?
Can I use a different transformation on this specific question (the remaining positively skewed one) or do I have to use the same transformation for all of the skewed questions?
What is the next "strongest" transformation after Lg10 on SPSS? How would it be entered on SPSS? (e.x. negatively skewed question = Lg10((Max. Score + 1) - Question))
Answer: Regarding 1) Factor analysis is based on correlations/covariances. When a highly skewed variable is  part of a correlation, the correlation can be affected by the extreme points. This will affect the factor analysis, although I do not know of literature on the extent of the effect (it's probably been studied, though).
Regarding 2) You do not need to use the same transformation on each variable. But transforming variables in different ways and then doing factor analysis can lead to factors that are somewhat hard to interpret.
Regarding 3) I don't know SPSS, sorry.
More generally, what is the nature of these questions? Are they Likert-type scales? Physical measurements? Or what? Ideally, you could tell us what they actually mean.
Comment: Thanks for your response! The questions are Likert-type scales. Example: "I do not have a reason for living" with answer choices 1 - 6 (1 = strongly disagree/ 6 = strongly agree).
Comment: I would NOT transform Likert type items in any way. Items that are very highly skewed, with only 7 levels, are items where nearly all gave a response at the end. These items may not be useful at all; they add little information.

I would first look at Cronbach alpha for items that don't correlate well with the others. I'd consider deleting the highly skewed items.

However, all this depends on SUBSTANCE and what the actual questions are. It may well be that factor analysis is not the best tool.
Answer: I agree with Peter that scores on 1-6-range items should not be transformed - not because it's theoretically wrong but because it's unlikely to help.  One option is, as I think Peter implies,  to find sound ways to combine such items into scales, which may have more interval-level properties than do the items themselves.  These scales could then be inputs for further procedures that address your research questions.
A second option is actually to use the original items in a factor analysis.  This goes against common wisdom, and it won't help you if you are using maximum likelihood factor extraction, which depends on normally distributed items.  But I am often surprised at how useful principal axis factor analytic results turn out even when based on skewed distributions from what were originally considered ordinal-level items.  Commonalities, variances explained, and factor loadings often come out satisfyingly high despite this apparent violation of best practices. If you try this, you may find it's helpful, but don't take the loadings and other results too literally:  they'll be approximations.  E.g., I wouldn't report loadings to more than 1 decimal place.
Comment: I'm not to say I disagree with you, but I (a kind of) feel lack of clearness in your response. For example, you don't explain why transformation of a 6-point rating scale is `unlikely to help` and what you mean under "help" here. Also `skewed distributions from ordinal-level items` looks like farrago. Linear FA don't have ordinal input, it treats everything as interval.
Comment: @rolando2 - Thank you for your response, but as ttnphns said, I would appreciate it if you could clarify your answer. The reason why I am doing an exploratory factor analysis on the individual questions is so that I can identify factors and then compare these factors to other mental health constructs such as depression, anxiety, and stress. Within psychological research, the steps for questionnaire development are principal components analysis (which I already did), EFA (to develop factors), then confirmatory factor analysis (to confirm factors).
Comment: I already tried doing the EFA but backtracked upon realizing that I didn't check for normality in my data. When I checked for skew and kurtosis, I realized that there were issues with 4 out of 20 questions. I don't know what you mean by "don't take the loadings and other results too literally" - how would it be possible to report results when they are not exact? In journals which report psychological research, I have only seen loadings reported to 2 decimal places.
Comment: Think about what it means to estimate a quantity or parameter, such as a factor loading.  Then think about what it means for a parameter to have uncertainty, as quantified by a standard error.  This is always the case even under random sampling from a population.  Now add to that idealized level of uncertainty an additional element...
Comment: ...you are not only dealing with sampling error but also inexactness that comes out of trying to approximate, with non-interval-level data, what you would ideally learn through interval-level data.  Now consider the fact that the data are skewed, taking you further away from your idealized situation. All of this adds up to a considerable level of uncertainty.  It's highly debatable whether confirmatory factor analysis would be appropriate under these conditions.
Answer: In my experience, transforming data using any combination of techniques (Square-root, Log, Inverse, etc.) is entirely valid from a statistical point-of-view. The only issue is that interpretation becomes increasingly difficult. For example, if two items load on the same factor and one has a log transformation, then you must interpret the two as related in some nonlinear fashion.
Moreover, Norris & Aroian (2004) indicate transformations may not even be necessary: http://www.ncbi.nlm.nih.gov/pubmed/14726780


Express/Loopback/NodeJS is there a possibility to add an "index" to db field

Question: we are using Express/Loopback Framework for our backend and what i am trying right now to assure that the indexes on some fields in models are created int he MongoDB. Is there a possibility to use some kind of meta-tags in models, on hand of which DataJuggler creates indexes in MongoDB?
Have a nice day!
Comment: Have you looked at the examples in the Mongodb connector here:  https://github.com/strongloop/loopback-connector-mongodb/blob/master/example/model.js
Comment: Take a look at the documentation for the model definition JSON file here: https://docs.strongloop.com/display/public/LB/Model+definition+JSON+file#ModeldefinitionJSONfile-Indexes
Answer: As M1kstur have  mentioned in the comments, you can define indexes in the model definition JSON file:
<code>"indexes": {
  "name_age_index": {
    "keys": {"name": 1, "age": -1}
  },
  "age_index": {"age": -1}
}
</code>
Also note that

Indexes will not be automatically created for you, even with NoSQL
  datasource connectors like MongoDB. You must run automigrate
  or autoupdate
  create your indexes!


Making alarm in disk space using cloudwatch

Question: I'm a system engineer, and I'm looking for a way to make an alarm on the disk space on our Amazon EC2 cloudwatch, please advice ?
Comment: This is an excellent question and something a lot of folks would like to accomplish. Even though Amazon doesn't have this built in, you can create your own metrics and alerts with CloudWatch, so a custom script would be fantastic all around. I have  no idea why people are downvoting the question.
Comment: There is a project, [AWS-Missing-Tools](https://github.com/colinbjohnson/aws-missing-tools/blob/master/ec2-write-storage-used/ec2-write-storage-used.sh) that has some of the functionality you are looking for (e.g. creating a disk-space metric on CloudWatch). You should be able to modify it to suit your needs, run it via cron, and then create an alarm once the metric is populated.
Comment: It's getting down voted for a couple of reasons IMHO. First, it's a "gimme teh codez" type question, which are frowned upon here. Second, the OP shows no evidence that he's done any research into this himself, explaining what he has tried, etc.
Comment: I have no idea why people are downvoting the question /// Eric was correct. i do think - why people are down-voting..
Comment: This is a duplicate of ["How to get the amount of free disk space with CloudWatch?"](http://serverfault.com/a/578668/211052).
Answer: Amazon has provided exactly what you are looking for:

mon-put-instance-data.pl - Collects system metrics on an Amazon EC2 instance (memory, swap, disk space utilization) and sends them to Amazon CloudWatch.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html

Note: Somebody had posted a pointer to this as an answer here earlier, but it looks like the answer was taken down.  I wish they could get credit for this answer.
I'll also pull the aws-missing-tools link from cyberx86's comment above into this answer as it could be helpful:

https://github.com/colinbjohnson/aws-missing-tools/blob/master/ec2-write-storage-used/ec2-write-storage-used.sh
Comment: Note: You'll also need to be able to write to cloudwatch from your instance. You can do this either by creating an IAM role with cloudwatch permissions and attaching it to the instance on launch OR by creating an IAM user for this purpose and saving credentials on each host. The cloudwatch push scripts are then called from cron and they push the desired metrics to cloudwatch regularly.
Comment: The URL has changed to http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/mon-scripts.html
Answer: Amazon doesn't have any visibility into the state of your filesystems.
As such, you'll need to write your own script that pushes disk space metrics into a CW custom metric.


How to test if some data points is drawn from a distribution with linear PDF?

Question: I have some data in the range [0, 1], and from the histogram below, it seems that they might be drawn from a distribution with linear probability density function (what's the name of that kinds of distribution?). How do I estimate the parameters of that distribution and how do I test how likely the data were drawn from it in R?

Update:
Now I know (thanks to Glen_b) if I can define the PDF (e.g., $ P(x) = ax + b$), I can test it with Kolmogorov-Smirnov test, but how do I estimate $a$ and $b$ from the sample?
Comment: see my edited answer.
Answer: At least as a rough approximation, one might regard that as having a pdf that increases linearly from 0:

(However, you should check whether there's actually a spike at exactly 1 - and perhaps a small one at exactly 0. Are the values near 1 all less than 1, or are there a number at exactly 1? Similarly with 0.)
Such a linear-increasing pdf might be regarded as a special case of the triangular distribution or as a particular beta distribution (a beta(2,1)).
You can do a hypothesis test for a fully-specified distribution using a Kolmogorov-Smirnov test (for example; there are other choices). In R, that's ks.test.
 f <- function(x) pbeta(x,2,1)
 ks.test(x,f)

    One-sample Kolmogorov-Smirnov test

data:  x
D = 0.1309, p-value = 0.3291
alternative hypothesis: two-sided

However, two caveats:
1) If you chose the shape to test for based on the same data you run the test on, the p-values are pretty much meaningless.
2) In any case, even if that wasn't at issue, a formal hypothesis test is usually not what you want if your question is "is it reasonable to use this as a model?" -- it mostly answers the wrong question.
Alternatively, perhaps you might have regarded the pdf as starting not from 0 but from something higher than 0:

That's not a triangle, but a trapezoid (often called a trapezium if you're not in the US).
Both the triangle I initially drew and this are linear of course.

if I can define the PDF (e.g., P(x)=ax+b), I can test it with Kolmogorov-Smirnov test, but how do I estimate a and b from the sample?

Some comments:
1) if you intended a triangular pdf (with peak at the right) you have a=2 and b=0
2) If you instead assume that it's a trapezoid(/trapezium) as drawn above there aren't two free parameters, but 1 (think instead of the height of the density at 0.5 and the slope - the height at 0.5 is restricted to be exactly 1, leaving you only with the slope)
3) The K-S test is for fully specified distributions (the triangle I had thought you mean was fully specified, now if we're dealing with the trapezoid above, you're estimating a parameter). If you use the same test statistic as the Kolmogorov-Smirnov, you are now doing what's called a Lilliefors test and you'll need to simulate to obtain the distribution of the test statistic.
4) you said you had spikes at exactly 0 and 1; this trapezoid-shaped model we're now discussing doesn't. If you have spikes in your data, a model without them won't fit. But perhaps you mean to have spikes as well as some linear density in between. The density in between doesn't look to me to be particularly linear though (which is why I suggested a beta model), but it might do well enough.

Let's imagine that we condition only on the data between 0 and 1 and we're fitting that trapzoidal-shaped (linear pdf) model, $f(x) = 1+\beta(x-\frac{_1}{^2})\,,\quad -2\leq\beta\leq 2$.
(The term "trapezoidal distribution" usually means a shape with the parallel sides parallel to the x-axis, not the y-axis as here. So perhaps I should stick with your 'linear' characterization after all.)
This parameter may be estimated in a number of ways (the most common approaches would be method of moments and maximum likelihood estimation).
Let's look at MLE.
$$\cal{L}(\beta) = \prod_{i=1}^n 1+\beta(x_i-\frac{_1}{^2})$$
$$\log(\cal{L}(\beta))=\cal{l}(\beta) = \sum_{i=1}^n \log(1+\beta(x_i-\frac{_1}{^2}))$$
Now this is a nice smooth function and it's quite possible to take the derivative, but we can't solve it for $\hat{\beta}$ in closed form (or at least I don't think so) by setting that derivative to zero. However, we can take our sample, evaluate the log-likelihood at any value of $\beta$, and so we can use optimization routines to locate the maximum.

The maximum for this sample occurs at about 1.2875. (The actual population value I generated the data from was $\frac{4}{3}\approx 1.3333$.)
Method of moments:
The mean of a random variable with the linear pdf is $\int_0^1 x [ 1+\beta(x-\frac{1}{2})] dx = [\frac{x^2}{2} + \beta (\frac{x^3}{3} -\frac{x^2}{4})]_0^1 = \frac{1}{2} + \frac{\beta}{12}$
Equating sample and population mean, $\hat{\beta}=12(\bar{x}-\frac{1}{2})$
This has the very nice advantage of great simplicity, but of course if $\bar{x}$ is not between $\frac{1}{3}$ and $\frac{2}{3}$, this yields an impossible estimate for $\beta$ (in that the resulting density isn't actually a density).
In the data I used in the MLE example, the method of moments estimate was 1.324 .
Comment: For you Caveat 1), how do I eliminate the possibility that the data were drawn from any linear PDF? My though was to get a linear PDF that best fit the data, then perform a K-S test. If the best linear PDF have p-value less than 0.05, then no linear PDF is likely to be the underlying distribution.
Comment: It's true that this will yield a conservative test, so if you reject you could be relatively confident that it's not the distribution specified in the null.  In this case, however, if you choose a better test statistic, you can rule any continuous pdf (including the linear one) out completely. Consider this test: *Test statistic*: $T$ = number of values in the sample that are equal to another value in the sample. Rejection Rule: reject if $T>0$. Type I error rate: 0. Power against mixed alternatives is excellent.
Comment: The thing is, just because one can rule out a distribution doesn't mean it's not good enough to use as a model. I can frequently rule out normality completely (e.g. are the lengths of the bolts from this manufacturing process normally distributed? No, because they have no chance of being negative, while normal distributions have non-zero chance of that. But a normal might still be a great model! Being rejected by some test often tells us little about the utility of the model)
Comment: Yes, there is a spike at exactly 1 and another at exactly 0. Neither triangular distribution or beta distribution is able to handle the spike at 0, right?
Comment: Both are continuous, they don't have spikes in either position. You could try to model it as a 0-inflated, 1-inflated beta (total of 4 parameters), which might fit pretty well as long as the distribution is continuous between the extremes.


why isn't the chi squared test just n-1?

Question: The chi squared test for the sample variance is:
$\chi^{2} = \frac{(n-1)s^{2}}{\sigma^{2}}$
Say that we do not know $\sigma^{2}$. In these cases it is common to use an estimator $s^{2}$, since $s^{2} \rightarrow \sigma^{2}$ in probability. Let us then swap $\sigma^{2}$ with $s^{2}$
$\chi^{2} = \frac{(n-1)s^{2}}{s^{2}}$
And with simple algebra, this leaves us with:
$
 \chi^{2} = n-1
$
So, when we don't now the population variance, then why go through all the effort of calculating this chi square value when we can just use n-1?
Comment: If you are using this to test the variance, the null hypothesis is typically that $\sigma^2$ is a specified value, and you are testing how likely the observed sample variance (or something more extreme) would be seen if the null hypothesis were true.  It makes no sense to estimate $\sigma^2$ from $s^2$ in such a test.
Comment: $s^2/s^2$ is obviously not identical to $s^2/\sigma^2$ - no mystery remains.
Comment: Your second line gives $n-1$, and you have no need for the rest
Comment: Your initial formula is wrong; it should have denominator $\sigma^2$ (population variance), not $S^2$ (sample variance.
Comment: It is my understanding that $s^{2}$ is an uncontroversial way to estimate $\sigma^{2}$.

Thanks for @Henry for clarifying that when using this test the population variance should be known. As seen in when calculating a t-statistic, it is commonly accepted to use $\bar{x}$ to estimate $\mu$, so I thought that the same would apply here.

Is there a test that is used when the population variance is unknown? When I search for hypothesis tests for a difference of variances I can only find $\chi^{2}$
Comment: It might be worth noting (a) that $s^2=\frac1{n-1}\sum(x_i-\bar x)^2$ is an unbiased estimator of $\sigma^2$ and (b) that the mean of a $\chi^2$ distribution with $n-1$ degrees of freedom is $n-1$
Answer: Perhaps an example will help. Let's test if the population variance, which we do not know, is equal to $1$.
$$
H_0: \sigma^2 = 1\\H_a: \sigma^2\ne 1
$$
Under this null hypothesis, we posit that $\sigma^2=1$, and we calculate the $\chi^2$ statistic that we can use to calculate p-value or compare to reference values.
$$\chi^2 = \dfrac{(n-1)s^2}{1}
$$
The key is that we must posit that the population variance has some value. In my example, I posited that the population variance was $1$. Perhaps you have a different null hypothesis you want to test. As an exercise, you might want to calculate the $\chi^2$ value for a null hypothesis of $H_0: \sigma^2 = 4$ for the following data: $1, 2, 3$.
Comment: It's the same as how you hypothesize some null value in other tests, like the t-test or a proportion test (to name two with which you might be familiar).
Comment: Oh gosh I see I just made a very fundamental mistake in the role of $\sigma^{2}$ in the Chi square test, I hadn't realized it was hypothesized in this context. Thank you for clearing that up


Select no banco access

Question: Estou com dificuldade para pegar dados do banco.
Consigo inserir no meu banco com esse codigo
<code>    Dim comandosql As OleDbCommand
    cadastro = "Insert INTO usuarios (usuario, senha) Values ('" & TextBox1.Text & "','" & TextBox2.Text & "')"
    comandosql = New OleDbCommand(cadastro, conexao)
    comandosql.ExecuteNonQuery()
    MsgBox("Dados inseridos com sucesso")
</code>
Agora eu queria selecionar os dados já cadastrados, por exemplo procurar na tabela usuarios o "codigo" = 1 e selecionar o "usuario" e "senha" deste codigo e fazer ele aparecer nos textbox3 e textbox4
Alguem poderia me ajudar?
Answer: Levando em consideração que você estará buscando estes dados de um unico usuario você pode usar o seguinte código:
<code>Dim comandosql as OleDbCommand
Dim reader As OleDbDataReader = Nothing
busca = "Select senha from usuarios where usuario = " & Textbox3.Text
comandosql = New OleDbCommand(busca, conexao)
reader = comandosql.Executereader()
TextBox4.Text = reader.Read()
read.Close()
</code>
Assim você busca a senha do usuario.
Espero ter ajudado.
Comment: Entendi o seu código, ajudou muito. Obrigado pela ajuda!

Select no banco access


How to compare two datasets using metrics drawn from unknown distributions and with small sample sizes?

Question: I have two datasets consisting of metrics from several experiments. Dataset 1 is the collection of results of experiments E performed by user A on product A, repeated N times. Dataset 2 is the collection of results of the same experiments E performed by the same user A on product B, repeated the same N times.
N is not large, and cannot be large due to practical limitations (typically around 15-20). The data CANNOT be assumed to be Gaussian. In some cases, it is known to be definitely not normal, and in some cases we do not know for sure. It is just an unknown distribution. We know that the metrics cannot be negative. That is pretty much the only definite information we know.
Using this data, how do we compare product A and product B and give a result with some statistical significance? How do we devise a hypothesis test to check if A > B with some x% confidence?
Edit
Answer: You could do a permutation/randomization test.
Possibly Wilcoxon rank sum will answer your question as well, although permutation test is probably closer to what you want.
In R there is perm.test in the exactRankTests package that seems made for your problem.
Comment: Randomization/permutation tests are almost designed for small samples. As far as I know, it doesn't depend on any theory at all. If samples are very small, you can list all possible permutations and have exact p value. For small samples there is `permtest` in the `BHH2` package.
Comment: When the assumptions of a parametric method are met, non-parametric methods often have lower power (although the difference can be small). When the assumptions are not met, the parametric tests can't be used properly. In another sense, though, permutation tests have the perfect power - they are a pure distillation of what "statistical significance" means. However, my view is that effect size is more important than stat sig.
Comment: Very good suggestion, but both Wilcoxon rank sum and the permutation test are non parametric tests whose significance tests are based on asymptotic theory and hence have lesser power for small sample sizes. That is a major concern.
Comment: Non-parametric tests in general are the way to go when sample sizes are small, but is it not true that they lack statistical power? How can I reject a null hypothesis with some probability using a non-parametric test?
Comment: Makes sense! I accepted your answer. One more related question: Is there a reason to prefer the permutation test over other standard non-parametric tests like the Mann Whitney U test or the Kruskal-Wallis' one way ANOVA?
Comment: I am not sure on that last one.


Check the presence of Disqus plugin

Question: I installed the Disqus plugin, and my WP theme now presents a problem at height of sidebar.
Disqus loads the iframe after the content of my theme. For this reason the JS script that sets the height of the sidebar doesn't see that new section, because is added before the calculation.
I need to fire again the THEME.siteHeight(); function in theme/js/init.js after the load of Disqus.
In that file, there's this section:
$( window ).load( function() {
        CANOS.retina( 'desktop-logo', 'retina-logo' );
        CANOS.retina( 'footer-desktop-logo', 'footer-retina-logo' );
        CANOS.siteHeight();
    });

})( jQuery );

I need to put a custom function here:
$( window ).load( function() {
        CANOS.retina( 'desktop-logo', 'retina-logo' );
        CANOS.retina( 'footer-desktop-logo', 'footer-retina-logo' );
        CANOS.siteHeight();
    });

    if disqus is loaded {
        CANOS.siteHeight();
    }

})( jQuery );

Obviously I need to find a way to check the presence of Disqus ("if disqus is loaded" doesn't work).
I'll appreciate any help.
Comment: Maybe try adding a resize handler on the iframe?http://stackoverflow.com/a/9484560/2103033
Answer: You can check if a disqus script is loaded with a function like
function hasDisqus() {
    var url = "disqus";
    var scripts = document.getElementsByTagName('script');
    for (var i = scripts.length; i--;) {
        if (scripts[i].src.match(/url/g)) return true;
    }
    return false;
}

Then you can do
if(hasDisqus())

Bit of a dirty solution, but might work for you

Check the presence of Disqus plugin


Are experts harmful?

Question: I'm reading "The Role of Chess in Artificial Intelligence Research" (pdf) and  interestingly, it says:

Experience [...] suggests that inputs from chess experts, while generally useful, cannot be trusted completely.
A good example of this is Deep Thought's evaluation function. Several changes by capable human chess experts failed to produce significant improvements and occasionally even affected the machine's performance negatively.
Here, human experts, along with their expertise, introduced their own
  prejudices into the program. One way of solving this problem is to
  limit the type and the amount of expert inputs allowed into the
  program; in other having an almost "knowledge-free" machine.

How true is that in modern research and practice?
Is that a big problem, or just something specific to the game of chess?
Comment: there is a famous quote to this effect in NLP. Something along the lines of "every time I fire a linguist, my system's performance improves"
Comment: Atilla's answer is largely correct (with the qualification I supply as a comment), but I thought I'd briefly note that in the specific case of chess, it has been demonstrated that Deep Thought plays chess very differently than chess experts. Deep Thought explicitly computes many moves in advance. Experts only compute a few moves, but they also employ the computational shortcut of *remembering* previously experienced games and outcomes. This remembering manifests via implicit pattern recognition that overlays an affective experience to the game (certain moves "just feel right").
Answer: I think this is more about Engineering Problem Solving. Most successful engineering projects do not duplicate expert reasoning or or the expert's nature exactly. They solved the problem in a different way.
For example washing machines use a different technique than humans, airplanes use different dynamics than birds.
If you are duplicating Expert Reasoning, their input is everything. But if you are solving same problem using different techniques (fast search, huge memory ...), their input is only helpful.
Comment: I'd add that one needs to consider that expertise is often so automated as to become opaque to conscious examination. Thus experts may often fail to have verbalizable awareness of the information processing steps they're taking that achieve expert performance.
Comment: While I do agree with you, this reasoning seems just too obvious, sorry :) In the paper they say that experts "introduced their own prejudices" which _seems_ a bit more than misunderstandings in implementation.
Comment: Of course they give input both good and bad since they see world in their own way which is very different from computer's view.
Answer: I think the key is consistency. The expert has not only particular knowledge, but a system within which that knowledge operates. They have a persona, an overall strategy, within which their tactics reside and evolve.
In some sense, a computer program playing chess is a Frankenstein's Monster created from a mashup of various bodies (programmers, experts, etc). So it's not surprising that any one expert's advice would not fit well with the system that exists.
I agree with other comments that experts may not know how they do what they do. In which case, being human, their conscious mind makes up a plausible story as to why they reached a particular decision. But I still think that expert advice to the programming team is always out of context (i.e. inconsistent with the context of the program's design and history).
EDIT: There may also be a Reinforcement Bias here. I can't find any good links to explain Reinforcement Bias, but the way I understand the term it's the effect you get when you update (refit) a supervised model using the previous results of the model -- usually indirectly -- as targets. It's similar to Confirmation bias, but it involves a level of indirection. Human experts would have their Reinforcement Biases, which could affect things.
Comment: I'm not sure I understand your last sentence, which looks to be an interesting point. Could you elaborate?
Comment: @Michelle: The program which we're hoping to improve with a subject-matter expert's advice already has its own context (original design, programmers, prior experts, etc). The advice which we're trying to incorporate comes from a different context, and may not work well -- may not even be consistent with -- the context that the program already has. My last statement was an attempt to say that it's actually unlikely that new input would work in the context (of the program) that's already established.
Answer: Such issues are common in various areas requiring judgement.
The chapter on "Judgemental forecasting and adjustments" in Makridakis, Wheelwright and Hyndman Forecasting: Methods and applications has similar stories of expert judgement under-performing sometimes even very simple systems.
There's a paper (Dawes et al (1989) "Clinical vs Actuarial Judgement" Science, Vol 243, No 4899, p1668-74) about the failures of expert judgement in the medical area against what it calls 'actuarial' methods - basically fairly simple statistical models.
On the other hand there's a paper in the actuarial literature about the 'noisiness' and inconsistency of expert judgement in a particular problem in that area where expert judgement is often regarded by its practitioners as of paramount importance.
Makridakis et al discuss failures of expert judgement in many areas, as they relate to forecasting, and contains quite a bit of valuable advice.
And so on it goes. Cognitive biases abound and human experts suffer from them along with everyone else.
Answer: The question of whether or not experts are harmful is certainly not a problem restricted to the game of chess.
An interesting question in the literature regarding the optimal design of Monetary Policy Committees (MPC) is whether or not committees should include outside experts who are not full-time employees of the central bank.
To put this into perspective, first consider the Bank of England's MPC. It is composed of five internal executive members of the bank and four outside experts. On the other hand, the Federal Reserve Bank employs a committee composed solely of bank employees.
Outside expert members are included in the Bank of England's MPC as they are believed to bring in expertise and extra information to that gained inside the Bank of England.
So, which MPC design is better? Experts in, or experts out?
Well, this area of research is still active and it has been investigated recently by Hansen & McMahon (2010). I suggest consulting the references mentioned in this paper for further reading on this issue of "committees of experts".
Is this a big (important) problem? Taking into consideration the effects that a MPC's decision may have for the economy, I'd say this is a pretty important problem!
Lastly, I ought to mention that monetary policy decisions can, in theory, be delegated to a computer. For example, the computer could be programmed to implement, say, a simple monetary policy rule; for instance, one of committment. This would remove expert input after the monetary policy rule has been programmed into the computer. The use of the computer in monetary policy is mentioned in Svensson (1999).
Reference:
Stephen Eliot Hansen & Michael McMahon, 2010. "What do outside experts bring to a committee? Evidence from the Bank of England," Economics Working Papers 1238, Department of Economics and Business, Universitat Pompeu Fabra.
Lars E.O. Svensson, 1999. "How should monetary policy be conducted in an era of price stability?," Proceedings, Federal Reserve Bank of Kansas City, pages 195-259.

Are experts harmful?


Proof Verification of Baby Rudin 3.3

Question: I am currently studying Rudin's Foundations of Real Analysis. I wrote a solution to Rudin's problem 3.3, and wanted to get some input on my proof. I appreciate any suggestions on the technicalities, style, and clarity.
Problem:

If $s_1=\sqrt2$, and
  $s_{n+1} = \sqrt{2+\sqrt{s_n}}$ where $n = 1, 2, 3, ...$ prove that {${s_n}$} converges, and that $s_n<2$ for $n = 1, 2, 3, ...$

My proof:
We first show $s_n<2$ for $n = 1, 2, 3, ...$ by induction.
When $n = 1$, it is clear that $\sqrt2 < 2$.
If $s_{n-1}$ < 2, then
$0<\sqrt{2+\sqrt{s_{n-1}}} < 2$
$\leftrightarrow 2+\sqrt{s_{n-1}}<4$
$\leftrightarrow \sqrt{s_{n-1}} < 2$
$\leftrightarrow s_{n-1} < 4$
But $s_{n-1} < 2$ by induction hypothesis. Thus $s_n<2$ for $n = 1, 2, 3, ...$ as desired.
We next show that {$s_{n}$} is monotonically increasing by induction.
It is clear that $s_1\leq s_2$.
We need to show $s_n \leq s_{n+1}$ for all $n \geq 2$. Assume $s_{n-1} \leq s_{n}$. Then,
$s_n \leq s_{n+1}$
$\leftrightarrow 0 < \sqrt{2+\sqrt{s_{n-1}}} \leq \sqrt{2+\sqrt{s_{n}}}$
$\leftrightarrow 2+\sqrt{s_{n-1}} \leq 2+\sqrt{s_{n}}$
$\leftrightarrow \sqrt{s_{n-1}} \leq \sqrt{s_{n}}$
$\leftrightarrow s_{n-1} \leq s_{n}$
But $s_{n-1} \leq s_{n}$ by the induction hypothesis. Thus the sequence is monotonically increasing.
Since {$s_n$} is bounded above and monotonically increasing, it converges.
Answer:
For the first proof, Also, I would show a series of forwards implications rather than the string if "if and only if" statements you are using, since your proof doesn't require both directions.

In this spirit, your first inductive proof, upper-bounding $s_{n}$ by $2$, could be re-phrased as follows:
We claim $s_{n} < 2$ for all $n$ by induction.
For $n = 1$ this is clear. Assume $s_{n-1} < 2$. Then since the square root function is monotonically increasing on the non-negative reals,
\begin{eqnarray}
\sqrt{s_{n-1}} < 2 \\
2 + \sqrt{s_{n-1}} < 4 \\
\sqrt{2 + \sqrt{s_{n-1}}} < 2
\end{eqnarray}
Since $s_n = \sqrt{2 + \sqrt{s_{n-1}}}$ this completes the inductive step.
I made a point of saying the square-root function is monotonic. This is fairly pedantic, and I don't think is really needed, but given that your argument implicitly uses this fact, it doesn't hurt.

Your second proof has what I believe to be a typo.

We need show $s_n \leq s_{n−1}$ for all $n \geq 2$. Assume $s_n \leq s_{n-1}$.

At first glance, I wasn't sure if this was a typo or if you meant to do a proof by contradiction. I think what you meant to show was $s_n \leq s_{n+1}$. In that case, replacing $s_{n-1}$ with $s_{n+1}$ where appropriate should be fine, and stylistically I would say the same things as regarding your first proof.
Comment: Thanks for the review! I corrected the typos you mentioned.

Proof Verification of Baby Rudin 3.3


How to create a custom template for entities?

Question: I'm having a dickens of a time overriding entity.tpl.php to create custom themes for my entities.
I am using ECK and Inline Entity Form. I've created entity section with bundle masthead. I've linked this to a node.
In a subtheme of the bootstrap theme, I then try to create a custom theme for this entity in a file called entity--section--masthead.tpl.php (note: I've also tried section.tpl.php and section--masthead.tpl.php and all sorts of permutations). Clear Cache. Nothing.
The only documentation is a few years old, and one of the most recent comments says this SHOULD work without the need for various hooks.
Answer: You don't need to add entity string to the template file name. These are theme hook suggestions for entity built with ECK (assuming your entity and bundle names):

section.tpl.php
section--masthead.tpl.php
section--masthead--full.tpl.php
section--[ID].tpl.php

As you can see there is a naming conflict because section is a drupal builtin template. It would be safer to use different name for your entity.
Comment: I can't figure how I should put a file so-named into my module (not theme) folder... because it is not loaded (it is in the template suggestions, but the entity.tpl is always loaded)
Comment: This is incorrect. The template names should be delimited with -- and not __. I have suggested an edit.


An unhandled exception occurred in webdev.webserver.exe

Question: It happens when I'm trying to open a certain page using it. (Page contains some CSS and JS, nothing really special).
Any ideas how to fix this?
Answer: This happens when disk path passed to the server (the path parameter) contains a double slash. (i.e. d:\somedir1\\somedir2)


How to prove Cauchy Criterion for limits

Question: Let $A$ be a nonempty subset of $\mathbb R$ and $f: A\rightarrow \mathbb R$. Suppose $c$ is a cluster point of $A$.

Suppose the limit of $f(x)$ at $c$ does not exist. Show that there exists $\varepsilon>0$ and two sequences $(x_n)$ and $(y_n)$ in $A\setminus \{c\}$, both converging to $c$, such that $|f(x_n)-f(y_n)|\geq\varepsilon$ for all $n\in\mathbb N$.
limit of $f(x)$ exists if and only if for all $\varepsilon>0$, there exists $\delta>0$ such that if $x,y\in A$ with $0<|x-c|$ ,$|y-c|<\delta$, then $|f(x)-f(y)|<\varepsilon$.

I have made some efforts on this but failed.
My plan for question 1: the limit of $f(x)$ at $c$ doesn't exist means for every $L$ in $\mathbb R$ there is some $\varepsilon>0$ such that for any $\delta>0$ there is a $x_\delta≠c
$ in the $\delta$-neighborhood of $c$ such that $|f(x_\delta)-f(c)|≥\varepsilon$.
Then, since we can find an arbitrary sequence $(y_n)$ in $A\setminus \{c\}$ converging to $c$, by letting $L_n=f(y_n)$ and $\delta_n=1/n$, we can find $\varepsilon_n>0$ so that there is a $x_n≠c$ in the $1/n$-neighborhood of c such that $|f(x_n)-f(y_n)|≥\varepsilon_n$. And it also follows that $(x_n)$ converges to c.
So we just need to let $\varepsilon=inf${$\varepsilon_1, \varepsilon_2,...,\varepsilon_n,...$}. Here the question comes that I cannot assure $\varepsilon>0$.
Can anybody give a hand?
Thanks very much!!
Comment: Thank you for your question! It is good practice on this site to add a bit of information on the context your question came up in, and to share your own work on it. It's also fine if you state that you're completely lost -- the information is helpful for answerers to gauge their answer on. For more information on asking a good question on this site, see [here](http://meta.math.stackexchange.com/q/9959/43351).
Answer: ad 1: As you saw yourself, the assumption that the $\lim$ doesn't exist is difficult to handle, since there are an unfinite number of values $\eta$ to which $f(x)$ doesn't converge. In fact we need some heavier tools to prove the claim.
If $f$ is unbounded in a neighborhood of $c$ then construct a sequence $(x_n)_{n\geq0}$ recursively as follows: Choose $x_0$ arbitrarily, and for each $n\geq1$ choose an $x_n$ with $|x_n-c|<{1\over n}$ and $|f(x_n)|\geq |f(x_{n-1})|+1$. Then the $x_n$ converge to $c$, as do the $y_n:=x_{n-1}$ $\>(n\geq1)$. Furthermore we have $|f(x_n)-f(y_n)|\geq1$ for all $n\geq1$.
When $f$ is bounded choose an arbitrary sequence $(x_n)_{n\geq0}$ converging to $c$. The sequence $a_n:=f(x_n)$ is a bounded sequence of real numbers and so has an accumulation point $\alpha\in{\mathbb R}$. Passing to a subsequence we may assume $\lim_{n\to\infty} f(x_n)=\alpha$. As, by assumption, the $\lim_{x\to c} f(x)$ does not exist there is an $\epsilon>0$ and a sequence $(y_n)_{n\geq0}$ converging to $c$ with $|f(y_n)-\alpha|\geq\epsilon$ for all $n$. It follows that we have $|f(x_n)-f(y_n)|\geq{\epsilon\over2}$ for all large enough $n$.
A second way to prove 1. would be to prove 2. first.
ad 2: This is the "Cauchy criterion for functions". As with the Cauchy criterion for sequences one direction is trivial. So assume that for all $\epsilon>0$ there is a $\delta>0$ such that $\ldots$
Choose an arbitrary sequence $(x_n)_{n\geq0}$ converging to $c$. Then it is easy to see that $\bigl(f(x_n)\bigr)_{n\geq0}$ is a Cauchy sequence in ${\mathbb R}$ and therefore converges to a number $\alpha\in{\mathbb R}$. Given an $\epsilon>0$ there is a $\delta>0$ with
$$|f(y)-f(x_n)|<\epsilon\tag{1}$$
for all $y\in \dot U_\delta(c)$ and all large enough $n$. For fixed $y$ letting $n\to\infty$ in $(1)$ we conclude that
$$|f(y)-\alpha|\leq\epsilon\quad\forall \ y\in \dot U_\delta(c)\ .$$


How to properly apply AM-GM inequality?

Question:
Find the minimum value of $y = \frac{x^2}{x-9}$ using AM-GM inequality when $x \gt 9$.

How I tried to solve it:
Setting $p=x-9$, I did the following:
$$y=\frac{(p+9)^2}{p}$$
$$y=\frac{p^2+18p+81}{p}$$
$$y=p+18+\frac{81}{p}$$
Since $x \gt 9$, $p\gt0$.
Using the AM-GM inequality, we can say,
$$\frac{p+18+\frac{81}{p}}{3} \ge \sqrt[3]{p \cdot 18 \cdot \frac{81}{p}}$$
$$y \ge 27\sqrt[3]{2}$$
The actual minimum of $y$ is 36. Now, $y \gt 27\sqrt[3]{2}$ is certainly true, but it is surely not a satisfactory answer. This link here hints at the proper way to solve the problem.
How should the inequality be used in general so that it does not cause problems like this? Why is my solution inappropriate?
Comment: You also need to find a value of $y$ that achieves your proposed minimum. Otherwise you just have an inequality.
Comment: AM/GM is only optimal when all the addends have the same value. It's impossible that $p$, $18$ and $81/p$ are all equal.
Answer: One has
$$p+\frac{81}p\ge18$$
by AM/GM, with equality when $p=9$, so
$$p+18+\frac{81}p\ge36,$$
with equality when $p=9$.
Answer: By AM-GM $$\frac{x^2}{x-9}=\frac{x^2-81+81}{x-9}=x-9+\frac{81}{x-9}+18\geq2\sqrt{(x-9)\cdot\frac{81}{x-9}}+18=36.$$
The equality occurs for $x-9=\frac{81}{x-9},$ which says that $36$ is a minimal value.

How to properly apply AM-GM inequality?


Why doesn't the timezonedb extension have the latest data?

Question: I've created a Dockerfile, and I use the wordpress:php7.4-apache.
I've tried to install the timezonedb extension using 3 different methods separately.
Method 1.
RUN docker-php-source extract \
    && pecl bundle -d /usr/src/php/ext timezonedb \
    && docker-php-ext-configure timezonedb \
    && docker-php-ext-install -j$(nproc) timezonedb \
    && docker-php-source delete

Method 2.
RUN mkdir -p /usr/local/src/pecl \
    && pecl bundle -d /usr/local/src/pecl timezonedb \
    && docker-php-ext-configure /usr/local/src/pecl/timezonedb \
    && docker-php-ext-install -j$(nproc) /usr/local/src/pecl/timezonedb \
    && rm -rf /usr/local/src/pecl

Method 3.
RUN apt-get -y install gcc make autoconf libc-dev pkg-config \
    && pecl install timezonedb \
    && bash -c "echo extension=timezonedb.so > /usr/local/etc/php/conf.d/docker-php-ext-timezonedb.ini"

The 3 methods work properly. After building the docker container, I confirm that the extension is installed and loaded properly by checking the PHP information:
CLI:

Web:

The timezonedb uses the latest version which according to the pecl site is version 2022.7.
I've checked the timezonedb github file, and there should be 597 timezones.
But, upon checking the timezonedb array, I can see it only contains 420 timezones.

So, the question is, why is it so different? What did I miss here?
Any kind of help would be much appreciated.
Comment: What does a timezonedb verion have to do with docker or php (tagging). I would expect hat the OS image or whatever has the timezone db is outdated - there is no connection here to PHP or Docker that looks relevant.
Comment: I just wanted to make sure I did it the right way, that's all.
Comment: Nope,you did not. See, PHP is irreleant as is docker to a degree, but mostly I would point timezonedb as tag
Comment: Given that this is a docker / Programming thing, I think this is better off at stackoverflow than serverfault.
Comment: PHP 7.4 is end-of-lifed, so it's not that surprising instances with it are too. https://www.php.net/eol.php
Comment: @TomTom, the issue I posted is related to how the timezonedb extension is installed and I needed to know whether I installed it the right way, that's why I posted it here, if you can't help, don't bother to complain, my post isn't worth your time :)
Answer: I think fundamentally this boils down to the tag php7.4-apache in combination with probably just what was included in the underlying OS image at the time. (As the question in the image says it uses an "external" database, I assume this is the OS-provided one.)
If you have a quick look at wordpress Dockerhub page, they have no php7.4-apache in their list of supported tags anymore.
And searching for this one finds this commit in the corresponding repository, noting how php 7.4 is EOL and they therefore removed the support for it.
Ie, it appears that everything is outdated in the image tagged this way as it is no longer maintained, not limited to the Olsen database.
I think the security side of this situation should probably be seen as more concerning than the outdated Olsen database.
I think it would probably be a good idea to pick a less specific tag (even latest, or apache, or 6 or 6-apache or whatever might fit your requirements) unless you actually have very specific requirements.
And if you do have requirements that forces you into using some more specific tag, you will simply have to keep in mind that such a specific tag will probably have a shorter lifetime than a more general one, and stay in touch with what its status is.
Comment: I tried this before, the WP site was broken when I used the latest version, so I have to "lock" the image to that tag :(
Answer: everything all right!
just try to use ALL_WITH_BC timezone group.
https://www.php.net/manual/en/class.datetimezone.php
echo 'ALL : ';
echo count(timezone_identifiers_list(DateTimeZone::ALL)); // -> result : 425
echo '
';

echo 'ALL_WITH_BC : ';
echo count(timezone_identifiers_list(DateTimeZone::ALL_WITH_BC)); // -> result : 593
echo '
';

Comment: Thank you for your answer, the dev didn't give me detailed information on what he did. But, I think this was the workaround. I'll mark this as an answer.
Answer: in case anyone stumbles with the same case, I hope this helps.
So, the dev finally reached out to me, saying he finally managed to get the correct timezones.
He said he had to use the PHP wrapper to get the timezones.
This confirms that the timezonedb installation is correct.
I'd like to thank you @ceejayoz and @håkan-lindqvist for your help, I really appreciate this, took quite some time for me to solve this case.


Upload e estrutura para armazenamento de imagens no Laravel

Question: Comecei a construir um portal, utilizando Laravel, que tem como requisitos: users, locais pertencentes a users e classificados pertencentes a users.
Como cada um destes requisitos necessita de imagens, gostaria de saber qual o melhor método para armazenar imagens para cada um destes requisitos e associados a eles.
A minha dúvida está mais assente na estrutura de pastas para armazenar as fotos, e sobre o sistema de upload das imagens em Laravel.
Answer: Há várias coisas a analisar:

Seu deploy é feito por alguma ferramenta automatizada? P.e. Capistrano? Se sim, a estrutura deverá ser montada fora da pasta de sua aplicação (que normalmente é feita no diretório public). Pode também apontar o diretório public à uma pasta externa (através de um link simbólico)
Irá gravar em algum CDN/Cloud files? P.e. Amazon S3? Nesse caso a estrutura é feita por buckets, então terá que pensar meio diferente.

A estrutura poderia ser algo como:
<code>shared/usuarios/
shared/usuarios/places
shared/usuarios/classificados
</code>
No caso da opção 1 seriam pastas, na opção 2, buckets.
Sobre o sistema de uploads não tem segredo, a API para upload é simples de utilizar. Recomendo que salve o caminho desses diretórios em arquivos de configuração, para que seja fácil você mover futuramente, caso precisar.
Segue link das docs: http://laravel.com/docs/requests#files
Comment: Nesse caso, a estrutura não teria de ser:

shared/usuarios/usuario_id/classificados ?
Comment: Sim, pode ser, ou então shared/usuarios/classificados/{usuario_id} assim a pasta usuarios fica mais "limpa". Ai é questão de gosto mesmo
Answer: Uso o pacote codesleeve/stapler
Com ele você vai associar imagens a um registro da tabela que deseja colocar uma imagem.
Em geral eu crio uma tabela só para as imagens e vinculo o registro dessa tabela a um registro de outras tabelas, assim posso ter mais de uma imagem vinculada ao registro sem dificuldades.
Um dos diferenciais desse pacote é a possibilidade de enviar sua imagem para um bucket S3 da amazom, oque se torna uma coisa muito boa se você tem que lidar com muitas imagens.
Comment: Obrigado, não conhecia essa forma de armazenas para um bucket S3 Amazon!


Finding a column in a DataTable row

Question:  Client_Payroll_Items.AsEnumerable().Select((row, index) => new {row, index})
    .Where(
            (x => x.row.Client_KEY == 3)   &&
            (x => x.row.Description == "401(k) % of Gross")
    )
    //.Where(x => x.row.Description == "401(k) % of Gross")
   .Select(x=> x.row.Payroll_item_calculation_type_KEY)
   .DefaultIfEmpty(-1)
   .First();

When I am running it I am getting "Operator '&&' cannot be applied to operands of type 'lambda expression' and 'lambda expression'" error.
 Client_Payroll_Items
    .AsEnumerable()
    .Select((row, index) => new {row, index})
    .Where(x => x.row.Client_KEY == 3)
    .Where(x => x.row.Description == "401(k) % of Gross")
    .Select(x=> x.row.Payroll_item_calculation_type_KEY)
    .DefaultIfEmpty(-1)
    .First()

The latest one runs and I get what I need.
Question - why the error in the first code?
Answer: In your first code sample you have specified the lambda x => multiple times, which is incorrect:
.Where((x => x.row.Client_KEY == 3) && (x => x.row.Description == "401(k) % of Gross"))

It should be specified only once and can be used multiple times, as needed, within the predicate:
.Where(x => x.row.Client_KEY == 3 && x.row.Description == "401(k) % of Gross")

Finding a column in a DataTable row