Could we use uniroot() instead of optimize() for this minimization in R?
In the last line of my R code below, I use optimize()
to find the df2
that minimizes the ncp_diff
function.
However, I was wondering if I could uniroot()
instead of optimize()
for this minimization?
alpha = c(.025, .975); df1 = 3; peta = .3 # The input
f < function(alpha, q, df1, df2, ncp){ # Notice `ncp` is the unknown
alpha  suppressWarnings(pf(q = (peta / df1) / ((1  peta)/df2), df1, df2, ncp, lower = FALSE))
}
ncp < function(df2){ # Root finding: finds 2 `ncp` for a given `df2`
b < sapply(c(alpha[1], alpha[2]),
function(x) uniroot(f, c(0, 1e7), alpha = x, q = peta, df1 = df1, df2 = df2)[[1]])
b / (b + (df2 + 4))
}
ncp_diff < function(df2, target = 0.15){
the_ncp < ncp(df2)
return(abs(abs(the_ncp[2]  the_ncp[1])  target))
}
optimize(ncp_diff, c(0, 1000)) ## HERE can I use `uniroot()` instead of `optimize()`
1 answer

alpha = c(.025, .975); df1 = 3; peta = .3 # The input f < function(alpha, q, df1, df2, ncp){ # Notice `ncp` is the unknown alpha  suppressWarnings(pf(q = (peta / df1) / ((1  peta)/df2), df1, df2, ncp, lower = FALSE)) } ncp < function(df2){ # Root finding: finds 2 `ncp` for a given `df2` b < sapply(c(alpha[1], alpha[2]), function(x) uniroot(f, c(0, 1e7), alpha = x, q = peta, df1 = df1, df2 = df2)[[1]]) b / (b + (df2 + 4)) } ncp_diff < function(df2, target = 0.15){ the_ncp < ncp(df2) return((the_ncp[2]  the_ncp[1])  target) } uniroot(ncp_diff, c(100, 1000)) # $root [1] 336.3956 $f.root [1] 3.74663e09 $iter [1] 7 $init.it [1] NA $estim.prec [1] 6.103516e05
Edit:
In order to use the same interval of (0,1000) we can look for a way to go around a scenario where both the lower and upper values produce results on the same side of the number line. Since this is an error in r, we can go through it by
tryCatch
ncp < function(df2){ # Root finding: finds 2 `ncp` for a given `df2` b < sapply(c(alpha[1], alpha[2]), function(x) tryCatch(uniroot(f, c(0, 1e7), alpha = x, q = peta, df1 = df1, df2 = df2)[[1]], error =function(e)NA )) if(any(is.na(b)))b= c(1,10) b / (b + (df2 + 4)) } uniroot(ncp_diff, c(0, 1000)) # $root [1] 336.3956 $f.root [1] 2.132438e09 $iter [1] 8 $init.it [1] NA $estim.prec [1] 6.103516e05
See also questions close to this topic

data.table conditional row count integrated with other nonconditional aggregates in R
Is it possible to include a conditional count with other nonconditional aggregates ? I understand that you can do this with most aggregates functions using which(), but haven't been able to find a solution for counting rows.
For example, the following code sums col1 by col4 into sum1 and sums col2 for which col3 > 3 by col4 into sum2.
> DT[, .(sum1 = sum(col1) , sum2 = sum(col2[which(col3 > 3)])) , by = (col4)]
anything similar for .N? e.g .N[which(col3 > 3)]
I understand you can do this separately using:
> DT[col3 > 3, .N, by = .(col4)] # this works but not what I'm after
That's not what I'm after. I'd like to integrate the conditional count with other nonconditional aggregates together if I can using only data.table operations. I wouldn't want using any other packages.
here is sample data:
> DT < data.table(col1 = c(3,5,2,2,4), col2 = c(0,1,0,1,1), col3 = c(3,4,6,7,1), col4 = c('a', 'b', 'a', 'a' ,'b')) > DT col1 col2 col3 col4 1: 3 0 3 a 2: 5 1 4 b 3: 2 0 6 a 4: 2 1 7 a 5: 4 1 1 b
My desired output:
DT[, .( sum1 = sum(col1) , sum2 = sum(col3[which(col3 > 3)]) , count3 = ???????) #.N[which(col3 > 3), but this doesn't work , by = .(col4)] col4 sum1 sum2 count3 1: a 7 13 2 2: b 9 4 1

how can we highlight specific letters and save it to xls
I have a data like this
df<structure(list(X = structure(3:1, .Label = c("CQLSKGQSYSVNVTFTSNIQSKSSKAVVHGILMGVP", "KLALQLHPDRNPDDPQAQEKFQDLGAAYEVLSDSEKRKQYD", "MVEAIVEFDYQAQHDDELTISVGEIITNIRKEDGGWW" ), class = "factor")), class = "data.frame", row.names = c(NA, 3L))
I am trying to highlight letter D in each of the row and then save it in xlsx. I tried to use this Excel Cell Coloring using xlsx
library(xlsx) # To export the data sheetname < "mysheet" write.xlsx(d, "mydata.xlsx", sheetName=sheetname) file < "mydata.xlsx" # I want to color special letters in one cell wb < loadWorkbook(file) # load workbook fo1 < Fill(foregroundColor="blue") # create fill object # 1 cs1 < CellStyle(wb, fill=fo1) # create cell style # 1 sheets < getSheets(wb) # get all sheets sheet < sheets[[sheetname]] # get specific sheet rows < getRows(sheet, rowIndex=2:(nrow(df)+1)) # get rows # 1st row is headers cells < getCells(rows, colIndex = 2:cols) # get cells values < lapply(cells, getCellValue) # extract the cell values
Next we find the letter that need to be highlighted
# find cells meeting conditional criteria D highlightblue < NULL for (i in names(values)) { x < as.character(values[i]) if (x == D && !is.na(x)) { highlightblue < c(highlightblue, i) } }
apply the formatting and save the workbook.
lapply(names(cells[highlightblue]), function(ii) setCellStyle(cells[[ii]], cs1)) saveWorkbook(wb, file)
However, I cannot figure out how to color a letter inside a cell.

Horizontal standard error bars on bar graphs with negative values
I have a bar graph that looks this:
and I am trying to get standard error bars on it  so two standard error bars for each column (one for the positive Y, and one for the negative N). I am aware of geom_errorh, but I cannot get it to work for this type of bar graph. Here is a reproducible example with the code that I used to get a bar chart like the one above:
Dataframe
Behavior<as.character(c("Hammock","Hammock","Climbing Trees","Climbing Trees","Structures","Structures","Grade","Grade")) Presence<c("Y","N","Y","N","Y","N","Y","N") Mean<as.numeric(c("18.5", "6.4","3.5","6.8","13.2","10.1","4.7","2.3")) SD<as.numeric(c("17.6","11.9","1.2","4.4","3.6","6.25","1.23","0.4")) DF<data.frame(Behavior,Presence,Mean,SD)
Coord Flip Geom Bar
brks < seq(20, 20, 2) lbls = paste0(as.character(c(seq(20, 0, 2), seq(2, 20, 2))), "") ggplot(DF, aes(x = Behavior, y = Mean, fill = Presence )) + geom_bar(data = subset(DF, Presence == "N"), stat = "identity") + geom_bar(data = subset(DF, Presence == "Y"), stat = "identity") + scale_y_continuous(breaks = brks,labels = lbls) + scale_fill_manual(values=c("#0b6bb6", "#6eaf46"),name="", breaks=c("N", "Y"),labels=c("N", "Y"))+ coord_flip()+ theme_bw()+ xlab("Pen Characteristic  Behavior")+ ylab("Average Behavior per Session")+
Is it possible to get the SE bars on this type of graph?
Thanks!

function hoisting shows different results with and without block scope
This is the example:
function b() { console.log(f); { function f() {} } } b()
I thought it would become:
function b() { // hoist to function scope function f() {} console.log(f); // should output function f }
or
function b() { console.log(f); // should output reference error { // just hoist to block scope like this function f() {} } }
but it outputs undefined, like
var
hoisting. why? 
Objects and functions in C++
I'm working on something that will take in an input file of strings and organize them based on a user input file. The top of the input file is the way it's organized and the rest is formatted information, each on it's own line to take in to feed into an object. Each individual piece of information that's needed for my object is divided into 5 consecutive lines.
My problem is the print function, which would be called on the array of object pointers and pass in the user sort preference when called. What I've found so far is that I am unable to to even call the function on the object. I've tried a bunch of different solutions like creating an entirely new object (Object myObject();) and calling the same print function on it but that won't work either. Is there a way to call the function on the array of pointers to objects that I created and pass in that preference variable? I've tried adding a default constructor and calling the function on that, but when I did it the two tests in object.cpp that cout the variables when print is called show that the variables are passed in as NULL possibly.
main.cpp
#include <iostream> #include <string> #include "object.h" int main() { Object **ptr = new Object*[5];//Create array of object pointers string firstLine, secondLine, thirdLine, userPreference; float fourthLine; int fifthLine; string currentLine; int lineCounter = 0; int arrayCounter = 0; getline(cin, userPreference);//Preference on sorting method while(getline(cin, currentLine)) {//Main loop switch(lineCounter) { case(1): firstLine = currentLine; lineCounter++; break; case(2): secondLine = currentLine; lineCounter++; break; case(3): thirdLine = currentLine; lineCounter++; break; case(4): fourthLine = stof(currentLine);//Get float value of currentLine and set fourthLine equal to it lineCounter++; break; case(5): fifthLine = stoi(currentLine);//Get integer value of currentLine and set fithLine equal to it ptr[arrayCounter] = new Object(firstLine, secondLine, thirdLine, fourthLine, fifthLine); arrayCounter++;//Step to next element now that the current element is filled lineCounter = 0;//Reset lineCounter cout << "case 5 test" << endl; break; }//End switch } ptr > print(userPreference); for(int i = 0; i < arrayCounter; i++) {//Delete everything cout << "Test"; } }
object.cpp
#include "object.h" #include <iostream> using namespace std; Object::Object(string title, string URL, string comment, float length, int rating) { vidTitle = title; vidURL = URL; vidComment = comment; vidLength = length; vidRating = rating; } void Object::print(string preference) { vidPreference = preference; cout << vidPreference; cout << preference; if(vidPreference == "rating") { //Rating sort //Is the array passed through correctly? If so, can I climb through like I do to assign it in main? } if(vidPreference == "length") { //Length sort } if(vidPreference == "title") { //Title sort } else { cout << preference << " is not a legal sorting method, giving up." << endl; } }
object.h
#ifndef OBJECT_H #define OBJECT_H #include <string> using namespace std; class Object { public: Object(string title, string URL, string comment, float length, int rating); void print(string preference); private: string vidTitle, vidURL, vidComment, vidPreference; float vidLength; int vidRating; }; #endif

How to stop the time from running after pressing "stop" on stopwatch
I have created this stopwatch and it runs pretty well. The only problem that I am having is that whenever I click my "stop" button, the time stops on the screen but it is still running in the background.
Is there any way to stop this from happening? I want the timer to stop on its current time, then when I click "start", it resumes from the time it was stopped on.
Im thinking maybe create a "new Date()" variable before the update function and another "new Date()" variable inside of the update function and somehow subtract those to get the current date. But I cannot figure that out either.
start = document.getElementById('Start'); stop = document.getElementById('Stop'); let watchRunning = false; Start.addEventListener('click', startHandler); Stop.addEventListener('click', stopHandler); function startHandler() { if (!watchRunning) { watchRunning = setInterval(update, 70); } } function stopHandler() { clearInterval(watchRunning); watchRunning = null; } update(); var seconds; var milliseconds; var d; function update() { d = new Date(); seconds = d.getSeconds(); milliseconds = Math.floor((d.getMilliseconds() / 10)); if (milliseconds < 10 && seconds < 10) { document.getElementById("Time").innerHTML = "0" + seconds + ".0" + milliseconds; } else if (milliseconds < 10 && seconds >= 10) { document.getElementById("Time").innerHTML = seconds + ".0" + milliseconds; } else if (milliseconds >= 0 && seconds < 10) { document.getElementById("Time").innerHTML = "0" + seconds + "." + milliseconds; } else if (milliseconds >= 0 && seconds >= 10) { document.getElementById("Time").innerHTML = seconds + "." + milliseconds; } }
#Time { backgroundcolor: yellow; maxwidth: 2.3%; }
<h1>Stop Watch</h1> <button id="Start">Start</button> <button id="Stop">Stop</button> <h3>Elapsed Time:</h3> <p id="Time"></p>
Try running the snippet and you will see what I mean. The time doesn't stop "running" after I click stop, and when I click start it resumes as if it was never stopped.

Applying a product of sequences to a column for each row
I'm working with a test dataset that I want to predict the probability of false negatives for using the following equation:
y = ∏ij(1  d)
Where y = the probability of a false negative, i = 1, j = number of samples (obtained from the sample column in the dataset below; 1, 2, or 3) and d = proportion of samples the amplified/pcr detection.
site sample sample.volume..L. pcr1 pcr2 pcr3 pcr4 pcr5 pcr6 d.prop f.negatives pond 1 1 2 1 1 1 0 1 1 0.83 0.167 pond 1 2 2 1 1 0 0 1 1 0.67 0.333 pond 1 3 2 0 0 1 1 1 1 0.67 0.333
I calculated
d.prop
using the following code:testdf$detection.proportion < length(subset(testdf, select = c(pcr1, pcr2, pcr3, pcr4, pcr5, pcr6))) p.detection < rowSums(data[,c(1, 2, 3, 10)] == "1") testdf$detection.proportion < p.detection/testdf$detection.proportion
Basically for each row I did: # of occurrences of 1/sum(1+0). This is because
d.prop
is the d in the equation.For f.negatives (which is y, which is what I'm looking for) I have only managed to do the (1d) part of the equation using this:
testdf$false.negatives < (1  testdf$detection.proportion)
I need to know how to do ∏ij part of the equation. This basically means for each row there will be a different ∏ij value:
If sample = 1 then ∏ij = 1*1 = 1 If sample = 2 then ∏ij = 1*2 = 2 If sample = 3 then ∏ij = 1*2*3 = 6
I'm aware of:
ij < cumprod(1:3)
but I don't know how I would apply this to each row, with a different j value (based on the sample column). 
C#  How To Mathematical Operations In DataTable
I have a DataTable and I want to do some mathematical operations before adding them to DataGridView. First one, I want to find maximum number of third column of my DataTable, second one I want to divide all values in third column with this maximum number. After all I want to replace my new values instead old values. My DataTable looks like that;
Column 1 Column 2 Column 3  a b 2000000 q r 250000 s t 185000 m w 400000 o p 750000
After the operations, my DataTable should look like that;
Column 1 Column 2 Column 3  a b 1 q r 0.125 s t 0.0925 m w 0.0002 o p 0.375
It's my code;
connection.Open(); //Some insignificant operations here. for (int q = 0; q < w.Length; q++) { for (int a = q + 1; a < w.Length; a++) { string query = "....."; SqlDataAdapter myAdapter = new SqlDataAdapter(query, connection); DataTable myTable = new DataTable(); myAdapter.Fill(myTable); //I started here for finding maximum number in DataTable. int[] myColumn = dt.AsEnumerable().Select(x => x.Field<int>("Column3")).ToArray(); int myMaximum = myColumn.Max(); //I don't know what should I do after that. foreach (DataRow myRows in myTable.Rows) { //Some significant operations again... dgv1.Rows.Add(...); } } } connection.Close();

How to flip the trigonometric circle around the 45° angle?
I am looking for a mathematical function capable of flipping the trigonometric circle around the "axis" located on the 45° vector (or pi/4 radians).
Such as :
  x  f(x)    0  90   45  45   90  0   135  315   180  270   225  225   270  180   315  135  
Just to give a few examples.
So basically I need to turn a compass rose :
into a trigonometric circle :
I only found things like "180  angle" but that is not the kind of rotation I'm looking for. Is it possible ?

BigQuery SQL Query Optimization
I managed to get a query that works, but I'm curious if there is a more succinct way to construct it (still learning!).
The BigQuery dataset that I'm working with comes from Hubspot. It's being kept in sync by Stitch. (For those unfamiliar with BigQuery, most integrations are appendonly so I have to filter out old copies via the
ROW_NUMBER() OVER
line you'll see below, so that's why it exists. Seems like the standard way to deal with this quirk.)The wrinkle with the
companies
table is every single field, except for two ID ones, is of typeRECORD
. (See the screenshot at the bottom for an example). It serves to keep a history of field value changes. Unfortunately they don't seem to be in any order so wrapping up the fields properties.first_conversion_event_name
for example  in aMIN()
orMAX()
and grouping by companyid formula doesn't work.This is what I ended up with (the final query is much longer; I didn't include all of the fields in the sample below):
WITH companies AS ( SELECT o.companyid as companyid, ARRAY_AGG(STRUCT(o.properties.name.value, o.properties.name.timestamp) ORDER BY o.properties.name.timestamp DESC)[SAFE_OFFSET(0)] as name, ARRAY_AGG(STRUCT(o.properties.industry.value, o.properties.industry.timestamp) ORDER BY o.properties.industry.timestamp DESC)[SAFE_OFFSET(0)] as industry, ARRAY_AGG(STRUCT(o.properties.lifecyclestage.value, o.properties.lifecyclestage.timestamp) ORDER BY o.properties.lifecyclestage.timestamp DESC)[SAFE_OFFSET(0)] as lifecyclestage FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY o.companyid ORDER BY o._sdc_batched_at DESC) as seqnum FROM `project.hubspot.companies` o) o WHERE seqnum = 1 GROUP BY companyid) SELECT companyid, name.value as name, industry.value as industry, lifecyclestage.value as lifecyclestage FROM companies
The
WITH
clause at the top is to get rid of the extra fields that theARRAY_AGG(STRUCT())
includes. For each field I would have two columns [field].value
and[field].timestamp
 and I only want the[field].value
one.Thanks in advance!

MATLAB: quadprog index issue?
I am trying to run several files of code for an assignment. I am trying to solve an optimization problem using the "quadprog" function from the "optim" package.
quadprog is supposed to solve the optimization problem in a certain format and takes inputs H,f, A,b, Aeq, Beq, lb, ub.
The issue I am having involves my f which is a column vector of constants. To clarify, f looks like c*[1,1,1,1,1,1] where c is a constant. Quadprog seems to run my code just fine for certain values of c, but gives me the error:
error: index (_,49): but object has size 2x2
error: called from quadprog at line 351 column 32
for other values of c. So, for example, 1/3 works, but 1/2 doesn't. Does anyone have any experience with this?
Sorry for not providing a working example. My code runs over several files and I seem to only be having problems with a specific value set that is very big. Thanks!

What prevents Particle Swarm Optimization from wandering off?
I am using SAS to implement PSO, following the code given in this link
I also saw the pseudo code both in Wikipedia and this website. In all three, I see that there are constrains on the location say 10
For example it can randomly happen that the initialized x=10 and then velocity=20, then what prevents x1 from being 30? What grantees that the solution will be within the bounded range?
I am especially concerned when I am actually seeking the solution which may be at the boundary.

Getting a desired vector from lsqr in python when solving a linear system
I am trying to solve a linear system which has multiple solutions. Here is part of my code:
from scipy.sparse.linalg import lsqr solution = lsqr(M, b)[0]
Now, if the matrix M is this:
[1 1 1 1] [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]
and b is this:
[1 0 0 0 0 0]
The solution given by this code is this:
[1/4 1/4 1/4 1/4]
However, for my purposes, I would like to get as the solution a vector with as many zeros as possible, so in this case, it would be this:
[1 0 0 0]
Is there any way to do this? I am fine using packages other than
scipy.sparse.linalg
, too. Thank you! 
ScalarVector multiplication with meshgrid (Matlab)
I have an expression such as s=aU+bV, where
a
andb
are scalars andU
andV
3component vectors. The outputs
is clearly a 3component vector. Let's assume I want to plot the first component ofs
find out how this varies when I changea
andb
.In order to plot I have to use surf, which takes matrices for the variables
a
b
. So I attempt to create matrices withmeshgrid
:A=0:10; B=1:10; [a,b]=meshgrid(A,B); U=[1,1,0]; V=[1,0,1]; s = a*U + b*V;
This clearly doesn't work, because nor the matrix product nor the elementwise product are well defined in this case. How do I actually make the matrices which represent the grid
a
b
multiply elementbyelement the vectorsU
andV
? 
Block matrix inner products in Matlab
I have been using the following custom function to perform the multiplication of a vector by a matrix, in which each element of the vector multiplies a 3x3 block within a (3xN)x(3) matrix:
function [B] = BlockScalar(v,A) N=size(v,2); B=zeros(3*N,3); for i=1:N B(3*i2:3*i,:) = v(i).*A(3*i2:3*i,:); end end
Similarly, when I want to multiply a collection of 3x3 matrices by a collection of 3x3 vectors, I use the following
function [B] = BlockMatrix(A,u) N=size(u,2); B=zeros(N,3); for i=1:N B(i,:) = A(3*i2:3*i,:)*u(:,i); end end
Since I call them very often, these unfortunately, slow down the running of my code significantly. I was wondering if there was a more efficient (perhaps vectorised) version of the above operations.