I have a table showing the first three columns for each member:
The date column includes 365 dates for multiple years, they are consecutive. The service columns shows numbers of services they received. I would like to add the GroupDate column to it. Every time there's a service, it would trigger the GroupDate to start counting 1, and it will last 5 consecutive days. And I want to name it using the beginning of the group date. When it ends on day 5, it would look for the next date that the member has a service, and it would start counting 5 consecutive days again.
Member
Date
Service
GroupDate
Mary
1/1
1
1/1
Mary
1/2
2
1/1
Mary
1/3
0
1/1
Mary
1/4
1
1/1
Mary
1/5
0
1/1
Mary
1/6
0
NA
Mary
1/7
0
NA
Mary
1/8
1
1/8
Mary
1/9
1
1/8
Mary
1/10
1
1/8
Mary
1/11
0
1/8
Mary
1/12
2
1/8
Mary
1/13
1
1/13
Mary
1/14
0
1/13
Mary
1/15
0
1/13
Mary
1/16
0
1/13
Mary
1/17
0
1/13
Related
I would like to use the Lead function to get the closest value for a group
Below is some sample data from flx_alps_boundaries
Subject code
Grade
Score
20-BD-AC-AL
1
1.12
20-BD-AC-AL
2
1.03
20-BD-AC-AL
3
0.97
20-BD-AC-AL
4
0.92
20-BD-AC-AL
5
0.86
20-BD-AC-AL
6
0.84
20-BD-AH-AL
1
1.15
20-BD-AH-AL
2
1.10
20-BD-AH-AL
3
1.05
20-BD-AH-AL
4
1.00
20-BD-AH-AL
5
0.98
20-BD-AH-AL
6
0.96
I am calculating the score for a subject using a formula and getting the grade for the nearest matching score from the above table . eg if score is 0.95 for subject 20-BD-AC-AL the grade should be 4
This is my current sql
select top 1
ab.alps_grade as alps_grade,
round( sum (actual_alps_points - expected_alps_points)
/ (count(reference) * 100) + 1,2 ) as alps_score
from alps_cte
inner join [flx_alps_boundaries] ab
on alps_cte.course = ab.course_code
where ab.course_code in ('20-BD-AC-AL','20-BD-AH-AL')
group by course,ab.alps_grade,ab.alps_score
order by abs(round(sum(actual_alps_points
- expected_alps_points)
/ (count(reference)*100) + 1, 2)
- ab.alps_score)
This query only returns one row. How do I use LEAD to get the appropriate grade for each
subject's score?
country_name
rank
show_title
Argentina
2
The Queen of Flow
India
1
Cobra Kai
Argentina
1
The Queen of Flow
England
3
Stay Close
Argentina
1
The Queen of Flow
I am trying to get a table that will display the number of times each show title is ranked 1st, 2nd or Third. The result something like this:
Rank
Cobra Kai
The Queen of Flow
Stay Close
1
1
2
0
2
0
1
1
3
0
0
0
You can use pivot_table like this.
df.pivot_table(index=['rank'], columns=['show_title'], aggfunc='count', fill_value=0)
Result
country_name
show_title Cobra Kai Stay Close The Queen of Flow
rank
1 1 0 2
2 0 0 1
3 0 1 0
Anyone could suggest an alternative for PSM package in R for parametric survival model since this package has been removed?
psm() is a function within the rms package; can you clarify which psm package do you mean?
the PSM package is here: https://rdrr.io/cran/PSM/
You can reproduce the results of the paper with the following codes:
Zhang Z. Parametric regression modelfor survival data: Weibull
regression model as an example. Ann Transl Med 2016;4(24):484. doi:
10.21037/atm.2016.08.45
> install.packages("rms")
> library(rms)
> library(survival)
> data(lung)
> psm.lung<-psm(Surv(time, status)~ph.ecog+sex*age+
+ ph.karno+pat.karno+meal.cal+
+ wt.loss,lung, dist='weibull')
> anova(psm.lung)
Wald Statistics Response: Surv(time, status)
Factor Chi-Square d.f. P
ph.ecog 13.86 1 0.0002
sex (Factor+Higher Order Factors) 10.24 2 0.0060
All Interactions 3.22 1 0.0728
age (Factor+Higher Order Factors) 3.75 2 0.1532
All Interactions 3.22 1 0.0728
ph.karno 5.86 1 0.0155
pat.karno 3.54 1 0.0601
meal.cal 0.00 1 0.9439
wt.loss 3.85 1 0.0498
sex * age (Factor+Higher Order Factors) 3.22 1 0.0728
TOTAL 33.18 8 0.0001
Maze
I have a maze as shown above(use the link) and state 3 contains prize while state 7 contains shock. a mouse can be placed in any state from 1 to 9 randomly and it move through the maze uniformly at random
Pi denote the probability that mouse reaches state 3 before state 7, given that AIM started in compartment i.
how to compute Pi for ∈ {1,2,3,4,5,6,7,8,9}.
Let Px be the probability that the game ends in position 3 if it starts in position x.
We know that P3=1 and P7=0
If you start in any other cell, then after you move you are essentially beginning the game again in the new cell. The probabilities for the other 7 cells can therefore be calculated from the probabilities for their neighbors that they can move to:
P5 = P2/4 + P4/4 + P6/4 + P8/4
P2 = P1/3 + P5/3 + P3/3
P1 = P2/2 + P4/2
... etc.
For each cell you have a linear equation -- 9 equations for 9 cells. Use Gaussian elimination or similar technique to solve the system of equations for the 9 probabilities.
Intuitively, it seems that the probabilities of landing on either square would be even, as long as the starting point is random.
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
If the mouse starts in position 3 or 7, the game is over.
If the mouse starts in position 1, there is a 1/3 chance of it ending up in position 2, 5 or 4, and so on.
IF we start with each cell containing a 1/9 probability, we can compute the frequency distribution for the next generation by multiplying the current value by the probability of a mouse moving in from another location. For example, in the second generation. Cell 1 will have 1/5 of the mice from cell 2 + 1/5 of the mice from cell 4 and 1/8 the mice from cell 5. So, the next generation of cell 1 is (1/9)(1/5)+(1/9)(1/5)+(1/9)(1/8), or 21/360, or 0.0583. We can then compute the probabilities for all the remaining cells.
Here are the first five generations in a terribly formatted table
1 2 3 4 5
1 0.111 0.058 0.059 0.046 0.039
2 0.111 0.095 0.078 0.065 0.054
3 0.111 0.169 0.228 0.274 0.312
4 0.111 0.095 0.078 0.065 0.054
5 0.111 0.163 0.115 0.101 0.082
6 0.111 0.095 0.078 0.065 0.054
7 0.111 0.169 0.228 0.274 0.312
8 0.111 0.095 0.078 0.065 0.054
9 0.111 0.058 0.059 0.046 0.039
Rows are Temperature and columns are Pressure.
Temp 750 755 760 765(pressure)
0 1.1 2 1 4
1 3 4 2 1 (factors)
2 4 5 5 9
I need a help in making this table in code with that i would like to access factor values for respective temp and pressure .
For example if temp 0 and pressure 750 the factor value is 1.1 ,if temp 1 and pressure 750 factor value is 3.
My Sample Output image