Negative & Positive sum - if-statement

I'm pulling activity data from Oura ring into a Google Sheet. I'm pulling Target kilometers (in 0.0) en Actual KM (in 0.0) then format them in an output sheet to meters (0.000).
The problem is that if I beat my Target kilometers (say 10.0), with for example 3 kilometers, then it will say -3 instead of 13 kilometers. However, if I don' beat it, it will just say i.e. 7.
Is there a way, like a formula, that can do like =IFnegative, count target KM + actual KM so it says 13 in the Output sheet?

try:
=IF(A1>0, A1+B1, (A1*-1)+B1)
also you can use ABS function

Related

Google Sheets: I want to create a formula or function that will automatically recalculate given certain conditions

I have tried "If, and, or" combining with simple script, and I am really new at the Apps script in Google Sheets.
What I need is: if any condition is true, I want the formula/function to subtract the corresponding value only for that condition from a total.
Background: a client is expected to serve 2-3 hours a day M-F (it can vary). If M=3, T=2, W=1, TH = 3, Fri = 3, then weekly total hours (WorkHours) = 12.
But the client may not always be scheduled on any day and these nonscheduled days can vary by client location. When this occurs, I need to reduce WorkHours by the number of hours for a nonscheduled day.
For Example:
When Cell C4 = Monday and Cell C6 = x are true, that equals a nonscheduled day on Monday then the formula would only subtract Monday hours from WorkHours total = 9. But there can be multiple days, M, W and F. This can vary.
I tried the first half of this formula:
=IF(OR(AND(C4="Monday",C6="x"),AND(D4="Tuesday",D6="x"),AND(E4="Wednesday",E6="x"),AND(F4="Thursday",F6="x"),AND(G4="Friday",G6="x"))
But I can’t get the “then” half of this if-statement to work in a formula.
I tried combining it with Apps Script and I can share that, but it’s nothing to be proud of.
Thank you so much
Marby
Taking a wild guess from your data, try a SUMIFS or SUMIFformula
=SUMIFS(A4:E4,A6:E6,"")
OR
=SUMIFS(A4:E4,A6:E6,"<>x")
OR
=SUMIF(A6:E6,"",A4:E4)
(If still in need do share a test sheet or more info)
perhaps the solution to your problem is simpler than it looks, and there is no need to use scripts to make this calculation.
try to do the following way
A
B
C
1
DayWeek
isValid
TimeWorked
2
M
X
3
3
T
X
2
4
W
X
1
5
TH
3
6
F
X
3
FORMULA TO CALCULATE
=SUMIF(B2:B6;"X";C2:C6) // result 9 as example
if your spreadsheet is fed sequentially, or has multiple collaborators, I suggest you use the rows as columns.
A
B
C
D
E
F
G
H
I
J
K
1
M
3
T
2
W
1
TH
x
F
3
FORMULA
FORMULA
=sum(sumif(B1;">0";B1);sumif(D1;">0";D1); sumif(F1;">0";F1); sumif(H1;">0";H1);sumif(J1;">0";J1))
if you are sure that you will not have other numbers in the columns, the formula can be reduced to
=sum(SUMIF(A1:J1;">0";A1:J1))
Initially, none of your answers would have helped with the sheets as I had structured them. But your answers sparked a restructuring of my sheets. It made them simpler and more useful to my users.
I really appreciate all your help. It helped me improve my product. Now that my sheets have been adapted, the formula I used =SUMIFS(A4:E4,A6:E6,"<>x") - working beautifully.
I have to really thank you - you helped me improve my whole approach.
This is great, really really helpful
Marby

Google Sheet Not Multiplying in IF Formula

I am trying to calculate a price based on rates. If the number is $20,000 or below, there is a flat rate of $700. If the number is between 20,001.01 and $50,000, the rate is 3.5% of the number. The rates continue to lower as the numbers go up. I can get Google Sheets to populate the box with $700 if it is below $20,000 but I can't seem to make it do the multiplication for me. The cell just shows C4*.035
I want it to multiply the number shown in the C4 square by the percentage listed.
Here is the code as it currently sits:
=if(AND(C4<=20000),"700",IF(AND(C4>=20000.01,C4<=50000),"C4*.035", IF(AND(C4>=50000.01,C4<=100000),"C4*.0325", IF(AND(C4>=100000.01),"C4*.03"))))
Note, I know nothing about coding so I apologize if it is sloppy or doesn't make sense. I tried to copy and format based on an example that was semi similar to mine.
try:
=IF(C4<=20000, 700,
IF(AND(C4>=20000.01, C4<=50000), C4*0.035,
IF(AND(C4>=50000.01, C4<=100000), C4*0.0325,
IF(AND(C4>=100000.01), C4*0.03))))
As BigBen noticed in his comment - there's a mistake in your formula. You should not use " " around the formula if you don't want it to be read as a string.
Actually more clean solution is using IFS formula for this task.
=ifs(C4<=20000,700,
C4<=50000,C4*0.035,
C4<=100000,C4*0.0325,
C4>100000,C4*0.03)

'Where' clause with graphite queries

I hold a lot of time-series metric data in graphite. Let's assume I have 2 different metrics X and Y that represent the prices of two different products.
I would now like to query the data from an app and do something like this (of course, that's a pseudo-sql):
Select all points of metric X where value is smaller than value of metric Y during a time frame
I couldn't find any reasonable way, without writing my own script or some map-reduce job, to do it.
I could, for example, plot the graphs and try to understand it visually pretty easily. But it won't be usable for an app to use.
I also thought about using some functions like currentBelow and currentAbove but it doesn't look like I can provide two different series to compare but only one specific integer per the entire period of time.
Disclaimer: I hope there is some better solution ;).
The solution is:
Metrics:
product.X.price
product.Y.price
create a filter mask - values 0 is not greater, 1 is greater
diffSeries - to set bound 0 - above points we want to include, below to exclude.
diffSeries(product.X.price, product.Y.price)
removeBelowValue - to remove all excluded - below 0,
removeBelowValue(diffSeries(product.X.price, product.Y.price), 0)
divideSeries - divide above series by itself to create a mask of 0 and 1 - fortunately in graphite's functions 0/0 = 0 (sic!),
divideSeries(removeBelowValue(diffSeries(product.X.price, product.Y.price), 0), removeBelowValue(diffSeries(product.X.price, product.Y.price), 0))
filter out using prepared mask with multiplySeries, since
0 * datapoint = 0
1 * datapoint = datapoint
multiplySeries(product.X.price, divideSeries(removeBelowValue(diffSeries(product.X.price, product.Y.price), 0), removeBelowValue(diffSeries(product.X.price, product.Y.price), 0)))

Stata: Difference in mean with Survey data

I am fairly new to Stata so some of my questions may be pretty basic, but I appreciate any help I can get. An example of my data is below
wage surveyweights Constant Sex
32 14 .56 1
56 45 .96 1
77 88 .25 0
I have survey data and I am trying to bootstrap the difference in mean wages for women ONLY. I want to find out the difference in mean outcome with [survey weights] and with [survey weights * constant] that is Manual calculation
mean x [pw=wt] if sex==1
mat x1=e(b)
mean x [pw=wt*constant] if sex==1
mat x2= e(b)
mat dd=x1-x2
**my outcome of interest is the point estimate and bootstrapped SE of dd
I have written the following program, but I am not getting the results I want. In particular, my SE column turns up blank and my point estimates for the means and the difference in means do not match with what I get with manual calculation.
program define meandiff, eclass properties (svyb)
args vars
mean `vars'
mat x1=e(b)
mean `vars' [pw=constant]
mat x2=e(b)
mat dd = x1-x2
ereturn scalar dd=e1(dd,1,1)
end
local vars wage
svy bootstrap e(dd), subpop(sex): means `vars'
I have already svyset my data using bootstrap weights. My questions are as follows:
When I type svy: mean 'vars' the program seems to start running bootstrap replications, and when I type svy bootstrap: means `vars' also the program seems to start replications. What is the difference between the two commands?
When I do mean x with regular survey weights do I need to do [pw=wt] or will svy command automatically apply the survey weights?
If I do have to write [pw=wt] in the first mean then do I need to create a variable called, say, gen wtxcons = wt * constant to do [pw=wtxcons] when I calculate the second mean?
How do I calculate the bootstrap SE and point estimates for my outcome of interest, which is the difference in means. Why are my point estimates not matching my manual calculation?

Calculating p-value by hand from Stata table

I want to ask a question on how to compute the p-value without a t-stat table, just by looking at the table, like on the first page of the pdf in the following link http://faculty.arts.ubc.ca/dwhistler/326UBC/stataHILL.pdf . Like if I don't know the value 0.062, how can I know it is 0.062 by looking at other information from the table?
You need to use the ttail() function, which returns the reverse cumulative Student's t distribution, aka the probability T > t:
display ttail(38,abs(_b[_cons]/_se[_cons]))*2
The first argument, 38, is the degrees of freedom (sample size less number of parameters), while the second, 1.92, is the absolute value of the coefficient of interest divided by its standard error, or the t-stat. The factor of two comes from the fact that Stata is doing a two-tailed test. You can also use the stored DoF with
display ttail(e(df_r),abs(_b[_cons]/_se[_cons]))*2
You can also do the integration of the t density by "hand" using Adrian Mander's integrate:
ssc install integrate
integrate, f(tden(38,x)) l(-1.92) u(1.92)
This gives you 0.93761229, but you want Pr(T>|t|), which is 1-0.93761229=0.06238771.
If you look at many statistics textbooks, you will find a table called the Z-table which will give you the probability that Z is beyond your test statistic. The table is actually a cumulative distribution function of the normal curve.
When people went to school with 4-function calculators, one or more of the questions on the statistics test would include a copy of this Z-table, and the dear students would have to interpolate columns of numbers to find the p-value. In your example, you would see the test statistic between .06 and .07 and those fingers would tap out that it was closer to .06 and do a linear interpolation to come up with .062.
Today, the p-value is something that Stata or SAS will calculate for you.
Here is another SO question that may be of interest: How do I calculate a p-value if I have the t-statistic and d.f. (in Perl)?
Here is a basic page on how to determine p-value "by hand": http://www.dummies.com/how-to/content/how-to-determine-a-pvalue-when-testing-a-null-hypo.html
Here is how you can determine p-value using Excel: http://ms-office.wonderhowto.com/how-to/find-p-value-with-excel-346366/
===EDIT===
My Stata text ("Microeconometrics using Stata", Revised Ed, Cameron & Trivedi) says the following on p. 402.
* p-values for t(30), F(1,30), Z, and chi(1) at y=2
. scalar y=2
. scalar p_t30 = 2 * ttail(30,y)
. scalar p_f1and30 = Ftail(1,30,y^2)
. scalar p_z = 2 * (1 - normal(y))
. scalar p_chi1 = chi2tail(1,y^2)
. display "p-values" " t(30)=" %7.4f p_t30
p-values t(30) = 0.0546