Stata - panel data from column data - panel

Panel data newbie here! I have data in Stata in the following format:
Name Company1 Company2 Company3 Company4 Company5 Company6
1985 6.0781 2.4766 1.4258 2.6508 13.2083
1986 6.4844 3.0938 2.1953 3.1351 15.7917
1987 10.1563 .2769 5.7109 3.6406 4.4058 15.5833
1988 10.4688 .4219 5.125 3.75 3.6767 8.1667
1989 11.0625 .4289 5.4453 3.9844 3.7288 10.25
1990 11.6875 .7206 6.875 5.6406 5.1974 8.6667
1991 13.6563 1.4863 10.1406 8.9687 5.6869 5.7083
1992 13.75 2.5522 12.2187 13 6.4681 10.875
1993 16.0938 2.6172 10.3437 16.4375 7.0826 13.6667
1994 16.3125 2.5313 9.9375 14 8.7387 12.125
1995 15.8125 3.9766 14.4375 12.5 8.8324 13.2083
I know I normally have to use the reshape command somehow, but I am not sure how. I try
reshape wide Name, i(Name) j(Name)
which obviously fails
(note: j = 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2
> 007 2008 2009 2010 2011 2012 2013 2014 2015)
no variables defined
r(111);
CompanyNumber (Number1, 2, 3, 4, 5, 6) will be the ID to be used in the panel data, and the year will be the time for each company respectively.
Any ideas how to reshape or w/e this data to be in proper panel data format?
How my data actually look like in Stata
The error I get when I try to run nick cox code:

Your sandbox data has implicit missing values, so the first two lines get omitted the way I read this in. I take that as being incidental. As #Roberto Ferrer clearly explained, this is an (utterly standard) reshape long.
clear
input Name Company1 Company2 Company3 Company4 Company5 Company6
1985 6.0781 2.4766 1.4258 2.6508 13.2083
1986 6.4844 3.0938 2.1953 3.1351 15.7917
1987 10.1563 .2769 5.7109 3.6406 4.4058 15.5833
1988 10.4688 .4219 5.125 3.75 3.6767 8.1667
1989 11.0625 .4289 5.4453 3.9844 3.7288 10.25
1990 11.6875 .7206 6.875 5.6406 5.1974 8.6667
1991 13.6563 1.4863 10.1406 8.9687 5.6869 5.7083
1992 13.75 2.5522 12.2187 13 6.4681 10.875
1993 16.0938 2.6172 10.3437 16.4375 7.0826 13.6667
1994 16.3125 2.5313 9.9375 14 8.7387 12.125
1995 15.8125 3.9766 14.4375 12.5 8.8324 13.2083
end
reshape long Company, i(Name)
rename (Company Name _j) (whatever year company)
sort company year
list , sepby(company)
+---------------------------+
| year company whatever |
|---------------------------|
1. | 1987 1 10.1563 |
2. | 1988 1 10.4688 |
3. | 1989 1 11.0625 |
4. | 1990 1 11.6875 |
5. | 1991 1 13.6563 |
6. | 1992 1 13.75 |
7. | 1993 1 16.0938 |
8. | 1994 1 16.3125 |
9. | 1995 1 15.8125 |
|---------------------------|
10. | 1987 2 .2769 |
11. | 1988 2 .4219 |
12. | 1989 2 .4289 |
13. | 1990 2 .7206 |
14. | 1991 2 1.4863 |
15. | 1992 2 2.5522 |
16. | 1993 2 2.6172 |
17. | 1994 2 2.5313 |
18. | 1995 2 3.9766 |
|---------------------------|
19. | 1987 3 5.7109 |
20. | 1988 3 5.125 |
21. | 1989 3 5.4453 |
22. | 1990 3 6.875 |
23. | 1991 3 10.1406 |
24. | 1992 3 12.2187 |
25. | 1993 3 10.3437 |
26. | 1994 3 9.9375 |
27. | 1995 3 14.4375 |
|---------------------------|
28. | 1987 4 3.6406 |
29. | 1988 4 3.75 |
30. | 1989 4 3.9844 |
31. | 1990 4 5.6406 |
32. | 1991 4 8.9687 |
33. | 1992 4 13 |
34. | 1993 4 16.4375 |
35. | 1994 4 14 |
36. | 1995 4 12.5 |
|---------------------------|
37. | 1987 5 4.4058 |
38. | 1988 5 3.6767 |
39. | 1989 5 3.7288 |
40. | 1990 5 5.1974 |
41. | 1991 5 5.6869 |
42. | 1992 5 6.4681 |
43. | 1993 5 7.0826 |
44. | 1994 5 8.7387 |
45. | 1995 5 8.8324 |
|---------------------------|
46. | 1987 6 15.5833 |
47. | 1988 6 8.1667 |
48. | 1989 6 10.25 |
49. | 1990 6 8.6667 |
50. | 1991 6 5.7083 |
51. | 1992 6 10.875 |
52. | 1993 6 13.6667 |
53. | 1994 6 12.125 |
54. | 1995 6 13.2083 |
+---------------------------+

Related

Combinate of values in a table to get the sum of each combination

I have a table with numeric data that i need make diferent combinations itself.
For example:
| A |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
I need to combine this single column to get the next result:
| A | B | C | D |
| - | - | - | - |
| 1 | | | |
| 1 | 2 | | |
| 1 | 2 | 3 | |
| 1 | 2 | 3 | 4 |
| 1 | 2 | | 4 |
| 1 | | 3 | |
| 1 | | 3 | 4 |
| 1 | | | 4 |
| | 2 | | |
| | 2 | 3 | |
| | 2 | 3 | 4 |
| | 2 | | 4 |
| | | 3 | |
| | | 3 | 4 |
| | | | 4 |
At the end of the table, i have to create a column with the Count of every column that has data and another column that contains the sums of number of each columns.
Maybe it sound very difficult or impossible, but I haven't a way to make it work.
I have try to "Cross Join" from SQL but didn't got the expected result.
Help!
In this case, you can solve this by counting in binary ending with the digits being the number of numbers in the set. etc. the starting set 2568 would end with 1111. this binary number would decide if you show that number in each row. Heres a table of how it would work.
| A |
|---|
| 2 |
| 5 |
| 6 |
| 8 |
A
B
C
D
Binary
Row number
8
0001
1
6
0010
2
6
8
0011
3
5
0100
4
5
8
0101
5
5
6
0110
6
5
6
8
0111
7
2
1000
8
2
8
1001
9
2
6
1010
10
2
6
8
1011
11
2
5
1100
12
2
5
8
1101
13
2
5
6
1110
14
2
5
6
8
1111
15

How to select data based on week in mysql

I want to select data from say 2nd week of a particular month and I have a column containing dates, then, how should I go with it.
I have data as such -
+-----+---------------------+-------------+
| id | date | daily_cases |
+-----+---------------------+-------------+
| 1 | 2020-01-30 00:00:00 | 1 |
| 2 | 2020-01-31 00:00:00 | 0 |
| 3 | 2020-02-01 00:00:00 | 0 |
| 4 | 2020-02-02 00:00:00 | 1 |
| 5 | 2020-02-03 00:00:00 | 1 |
| 6 | 2020-02-04 00:00:00 | 0 |
| 7 | 2020-02-05 00:00:00 | 0 |
| 8 | 2020-02-06 00:00:00 | 0 |
| 9 | 2020-02-07 00:00:00 | 0 |
| 10 | 2020-02-08 00:00:00 | 0 |

Laravel Eloquent query order hierarchically

Need some help in getting a collection, using Laravel Eloquent, from a table but ordered by the parent_id to achieve, as an example, the following hierarchy:
Africa
Eastern Africa
Middle Africa
Northern Africa
Asia
Central Asia
Eastern Asia
(...) etc.
The below table is what I have and it's ordered by parent_id only.
What I'm trying to achieve is the parent > children ordering. Thanks in advance for any help.
+----+---------------------------------+-----------+
| id | region_common_name | parent_id |
+----+---------------------------------+-----------+
| 1 | Global | 0 |
| 2 | Africa | 1 |
| 14 | Americas | 1 |
| 33 | Antartica | 1 |
| 3 | Asia | 1 |
| 6 | Australasia | 1 |
| 4 | Europe | 1 |
| 10 | Eastern Africa | 2 |
| 11 | Middle Africa | 2 |
| 8 | Northern Africa | 2 |
| 12 | Southern Africa | 2 |
| 9 | Sub-Saharan Africa | 2 |
| 13 | Western Africa | 2 |
| 20 | Central Asia | 3 |
| 21 | Eastern Asia | 3 |
| 22 | South-eastern Asia | 3 |
| 23 | Southern Asia | 3 |
| 24 | Western Asia | 3 |
| 25 | Eastern Europe | 4 |
| 26 | Northern Europe | 4 |
| 27 | Southern Europe | 4 |
| 28 | Western Europe | 4 |
| 16 | Caribbean | 5 |
| 17 | Central America | 5 |
| 15 | Latin America and the Caribbean | 5 |
| 19 | Northern America | 5 |
| 29 | Australia and New Zealand | 6 |
| 30 | Melanesia | 6 |
| 31 | Micronesia | 6 |
| 32 | Polynesia | 6 |
| 5 | North America | 14 |
| 18 | South America | 14 |
| 7 | Southern America | 18 |
+----+---------------------------------+-----------+
You can order by multiple conditions but have to join on yourself.
Example in SQL
SELECT rp.region_common_name parent_name, r.region_common_name FROM regions r
INNER JOIN regions rp ON r.parent_id = rp.id
WHERE r.parent_id != 1 AND r.parent_id != 0
ORDER BY rp.region_common_name ACS, r.region_common_name ASC
So your result will look like this:
+---------------------------------+---------------------------------+
| parent_name | region_common_name |
+---------------------------------+---------------------------------+
| Africa | Eastern Africa |
| Africa | Middle Africa |
| Americas | North America |
| Americas | South America |
+---------------------------------+---------------------------------+
In eloquent you can use the QueryBuilder to generate your query.
It will look like this:
DB::table('regions as r')
->join('regions as rp', 'r.parent_id', '=', 'rp.id')
->where('r.parent_id', '!=', '0')
->where('r.parent_id', '!=', '1')
->orderBy('rp.region_common_name', 'asc')
->orderBy('r.region_common_name', 'asc')
->select('rp.region_common_name as parent_name', 'r.region_common_name as name');

Custom Week number from Acquistion Date of each User

Is there a way to calculate Custom Week numbers that start from a user's first Transaction Date onwards? The Users (emailId) and TransDate columns may not be in a sorted condition as shown below:
e.g.
+------+-------------+---------------------+
| WkNo | TransDate | emailId |
+------+-------------+---------------------+
| 1 | 2018-Aug-30 | moz.shea#abc.com |
| 1 | 2018-Aug-30 | moz.shea#abc.com |
| 10 | 2018-Nov-07 | moz.shea#abc.com |
| 1 | 2018-Aug-09 | zabi.prado#abc.com |
| 1 | 2018-Aug-09 | zabi.prado#abc.com |
| 6 | 2018-Sep-20 | zabi.prado#abc.com |
| 15 | 2018-Nov-23 | zabi.prado#abc.com |
| 21 | 2018-Dec-31 | zabi.prado#abc.com |
| 1 | 2018-Aug-20 | silo.whitte#abc.com |
| 5 | 2018-Sep-23 | silo.whitte#abc.com |
| 7 | 2018-10-11 | silo.whitte#abc.com |
| 7 | 2018-10-11 | silo.whitte#abc.com |
| 8 | 2018-Oct-14 | silo.whitte#abc.com |
| 9 | 2018-Oct-19 | silo.whitte#abc.com |
| 1 | 2018-Jul-01 | pablo.gucci#abc.com |
| 6 | 2018-Aug-10 | pablo.gucci#abc.com |
| 13 | 2018-Oct-03 | pablo.gucci#abc.com |
+------+-------------+---------------------+
I wrote the following formula using FILTER function that then supplies the filtered dates per user to the DATEDIF function. However, i am not getting the desired result as shown above.
=ARRAYFORMULA(if(B2:B="","",1 + round(DATEDIF(min(sort(FILTER(B2:B,C2:C=C2:C),1,true)),sort(FILTER(B2:B,C2:C=C2:C),1,true),"D")/7)))
EDIT:
Formula Result:
1
7
7
7
8
10
10
13
13
14
16
16
16
17
19
22
27
Also removed SORT from above formula:
=ARRAYFORMULA(if(B2:B="","",1 + round(DATEDIF(min(sort(FILTER(B2:B,C2:C=C2:C),1,true)),FILTER(B2:B,C2:C=C2:C),"D")/7)))
Formula Result:
10
10
19
7
7
13
22
27
8
13
16
16
16
17
1
7
14
Both seem to work, but give unexpected results as MIN is evaluating to a single date 2018-Jul-01 instead of an Array of Minimum dates per user. Where am i going wrong?
Did you try using the MINIFS() function ?
EG with the following
Dates in cells B2:B10
Emails in Cells C2:C10
The formula in cell A2 would give the earliest date for the email address in Cell C2
=MINIFS($B$2:$B$10, $C2:$C10, "="&C2)
You should be able to use this in your formula to calculate the number of weeks
For those who might face a similar challenge, here is the answer:
=ARRAYFORMULA(IF(B2:B="","",ROUND((B2:B-VLOOKUP(C2:C,SORT({C2:C,B2:B},2,1),2,0))/7)+1))
The idea is to do a Vlookup on column C, passing it a switched and sorted range of dates in ascending order. These first dates are then deducted from the B column dates, to get the desired result ie. either days or weeks.
One can remove the +1 as i used it just to display the starting week as 1 instead of 0. So the result may differ slightly. But without +1, the result is accurate, especially if you are doing a Cohort.
0
0
10
0
0
6
15
21
0
5
7
7
8
9
0
6
13
As a check, i removed division by 7 and +1, then checked the days, which are correct.
0
0
69
0
0
42
106
144
0
34
52
52
55
60
0
40
94
Hope this helps.

Get the best route between two dirrent paths in wp8

I have created a bus transit application for a city in windows phone 8. I have stored all my routes and bus stops in a separate table in the database.
My bus stop table contains:
stop_id | stop_name | latitude | longitude | status | alias_name
---------------------------------------------------------------------------------------
1736 | Atlas Company | 18.629243 | 73.833814 | Active | Centurenca Corner
1737 | Atlas company | 18.629243 | 73.833814 | Active |
681 | Atma Anand Dhyan Kendra | 18.600349 | 73.926251 | InActive |
My Routes Table contains
bus_id | bus_no | bus_source | bus_destination | days_of_week | total_distance estimated_time | source_stop_names | destination_stop_names | total_stops | source_trip_time | destination_trip_time | bus_status
source_stop_names contains
-----------------
1 | Swargate
2 | Parvati payatha
3 | Dandekar pul
4 | Pan mala Sinhgad Road
5 | Jal Shuddhikarn Kendra Sinhgad Road
6 | Ganesh mala
7 | Vitbhatti Sinhgad Road
8 | Vitthalwadi jakat naka
9 | Jaydeo nagar
10 | Rajaram pul
11 | Vitthalwadi Mandir Hingne
12 | Hingne rasta
13 | Anand nagar singhgad rd
14 | Manik Bag
15 | Indian hum company
16 | Wadgaon phata
17 | Patil colony
18 | Dhayari phata
19 | Sanas Vidyalaya
20 | Dangat wasti
21 | Gar mala
22 | Dhayarai gaon
23 | Raykar wasti
24 | Poultry farm singhgad road
25 | Dhayarigaon shala
26 | Chavan mala
27 | DSK Vishwa
I want to find the shortest path between two bus stops not connected by a bus route, and show the number of routes and buses the user has to take while travelling from one point to another, like google maps does.
I have used default map control in windows phone 8.

Resources