So I am trying to get average lengths of the four seasons in the 21st century using PowerShell as a self-imposed programming challenge.
My idea is to get values from a text file, create a [PSCustomObject] and assign values to its noteproperties each line, add year to the date and convert the dates to [datetime] and add to another array, then loop through the second array using index and get season length using new-timespan and add to a third array, and measure-object third array, forgive me if this may sound confusing in English but it really is very simple in code.
Now I get the dates of equinoxes and solstices from here:Solstices and Equinoxes: 2001 to 2100
Using Notepad++ to format the dates I got this:
2001 Mar 20 13:31 Jun 21 07:38 Sep 22 23:05 Dec 21 19:22
2002 Mar 20 19:16 Jun 21 13:25 Sep 23 04:56 Dec 22 01:15
2003 Mar 21 01:00 Jun 21 19:11 Sep 23 10:47 Dec 22 07:04
2004 Mar 20 06:49 Jun 21 00:57 Sep 22 16:30 Dec 21 12:42
2005 Mar 20 12:34 Jun 21 06:46 Sep 22 22:23 Dec 21 18:35
2006 Mar 20 18:25 Jun 21 12:26 Sep 23 04:04 Dec 22 00:22
2007 Mar 21 00:07 Jun 21 18:06 Sep 23 09:51 Dec 22 06:08
2008 Mar 20 05:49 Jun 21 00:00 Sep 22 15:45 Dec 21 12:04
2009 Mar 20 11:44 Jun 21 05:45 Sep 22 21:18 Dec 21 17:47
2010 Mar 20 17:32 Jun 21 11:28 Sep 23 03:09 Dec 21 23:38
2011 Mar 20 23:21 Jun 21 17:16 Sep 23 09:05 Dec 22 05:30
2012 Mar 20 05:15 Jun 20 23:08 Sep 22 14:49 Dec 21 11:12
2013 Mar 20 11:02 Jun 21 05:04 Sep 22 20:44 Dec 21 17:11
2014 Mar 20 16:57 Jun 21 10:52 Sep 23 02:30 Dec 21 23:03
2015 Mar 20 22:45 Jun 21 16:38 Sep 23 08:20 Dec 22 04:48
2016 Mar 20 04:31 Jun 20 22:35 Sep 22 14:21 Dec 21 10:45
2017 Mar 20 10:29 Jun 21 04:25 Sep 22 20:02 Dec 21 16:29
2018 Mar 20 16:15 Jun 21 10:07 Sep 23 01:54 Dec 21 22:22
2019 Mar 20 21:58 Jun 21 15:54 Sep 23 07:50 Dec 22 04:19
2020 Mar 20 03:50 Jun 20 21:43 Sep 22 13:31 Dec 21 10:03
2021 Mar 20 09:37 Jun 21 03:32 Sep 22 19:21 Dec 21 15:59
2022 Mar 20 15:33 Jun 21 09:14 Sep 23 01:04 Dec 21 21:48
2023 Mar 20 21:25 Jun 21 14:58 Sep 23 06:50 Dec 22 03:28
2024 Mar 20 03:07 Jun 20 20:51 Sep 22 12:44 Dec 21 09:20
2025 Mar 20 09:02 Jun 21 02:42 Sep 22 18:20 Dec 21 15:03
2026 Mar 20 14:46 Jun 21 08:25 Sep 23 00:06 Dec 21 20:50
2027 Mar 20 20:25 Jun 21 14:11 Sep 23 06:02 Dec 22 02:43
2028 Mar 20 02:17 Jun 20 20:02 Sep 22 11:45 Dec 21 08:20
2029 Mar 20 08:01 Jun 21 01:48 Sep 22 17:37 Dec 21 14:14
2030 Mar 20 13:51 Jun 21 07:31 Sep 22 23:27 Dec 21 20:09
2031 Mar 20 19:41 Jun 21 13:17 Sep 23 05:15 Dec 22 01:56
2032 Mar 20 01:23 Jun 20 19:09 Sep 22 11:11 Dec 21 07:57
2033 Mar 20 07:23 Jun 21 01:01 Sep 22 16:52 Dec 21 13:45
2034 Mar 20 13:18 Jun 21 06:45 Sep 22 22:41 Dec 21 19:35
2035 Mar 20 19:03 Jun 21 12:33 Sep 23 04:39 Dec 22 01:31
2036 Mar 20 01:02 Jun 20 18:31 Sep 22 10:23 Dec 21 07:12
2037 Mar 20 06:50 Jun 21 00:22 Sep 22 16:13 Dec 21 13:08
2038 Mar 20 12:40 Jun 21 06:09 Sep 22 22:02 Dec 21 19:01
2039 Mar 20 18:32 Jun 21 11:58 Sep 23 03:50 Dec 22 00:41
2040 Mar 20 00:11 Jun 20 17:46 Sep 22 09:44 Dec 21 06:33
2041 Mar 20 06:07 Jun 20 23:37 Sep 22 15:27 Dec 21 12:19
2042 Mar 20 11:53 Jun 21 05:16 Sep 22 21:11 Dec 21 18:04
2043 Mar 20 17:29 Jun 21 10:59 Sep 23 03:07 Dec 22 00:02
2044 Mar 19 23:20 Jun 20 16:50 Sep 22 08:47 Dec 21 05:43
2045 Mar 20 05:08 Jun 20 22:34 Sep 22 14:33 Dec 21 11:36
2046 Mar 20 10:58 Jun 21 04:15 Sep 22 20:22 Dec 21 17:28
2047 Mar 20 16:52 Jun 21 10:02 Sep 23 02:07 Dec 21 23:07
2048 Mar 19 22:34 Jun 20 15:54 Sep 22 08:01 Dec 21 05:02
2049 Mar 20 04:28 Jun 20 21:47 Sep 22 13:42 Dec 21 10:51
2050 Mar 20 10:20 Jun 21 03:33 Sep 22 19:29 Dec 21 16:39
2051 Mar 20 15:58 Jun 21 09:17 Sep 23 01:26 Dec 21 22:33
2052 Mar 19 21:56 Jun 20 15:16 Sep 22 07:16 Dec 21 04:18
2053 Mar 20 03:46 Jun 20 21:03 Sep 22 13:05 Dec 21 10:09
2054 Mar 20 09:35 Jun 21 02:47 Sep 22 19:00 Dec 21 16:10
2055 Mar 20 15:28 Jun 21 08:39 Sep 23 00:48 Dec 21 21:56
2056 Mar 19 21:11 Jun 20 14:29 Sep 22 06:40 Dec 21 03:52
2057 Mar 20 03:08 Jun 20 20:19 Sep 22 12:23 Dec 21 09:42
2058 Mar 20 09:04 Jun 21 02:03 Sep 22 18:07 Dec 21 15:24
2059 Mar 20 14:44 Jun 21 07:47 Sep 23 00:03 Dec 21 21:18
2060 Mar 19 20:37 Jun 20 13:44 Sep 22 05:47 Dec 21 03:00
2061 Mar 20 02:26 Jun 20 19:33 Sep 22 11:31 Dec 21 08:49
2062 Mar 20 08:07 Jun 21 01:10 Sep 22 17:19 Dec 21 14:42
2063 Mar 20 13:59 Jun 21 07:02 Sep 22 23:08 Dec 21 20:22
2064 Mar 19 19:40 Jun 20 12:47 Sep 22 04:58 Dec 21 02:10
2065 Mar 20 01:27 Jun 20 18:31 Sep 22 10:41 Dec 21 07:59
2066 Mar 20 07:19 Jun 21 00:16 Sep 22 16:27 Dec 21 13:45
2067 Mar 20 12:55 Jun 21 05:56 Sep 22 22:20 Dec 21 19:44
2068 Mar 19 18:51 Jun 20 11:55 Sep 22 04:09 Dec 21 01:34
2069 Mar 20 00:44 Jun 20 17:40 Sep 22 09:51 Dec 21 07:21
2070 Mar 20 06:35 Jun 20 23:22 Sep 22 15:45 Dec 21 13:19
2071 Mar 20 12:36 Jun 21 05:21 Sep 22 21:39 Dec 21 19:05
2072 Mar 19 18:19 Jun 20 11:12 Sep 22 03:26 Dec 21 00:54
2073 Mar 20 00:12 Jun 20 17:06 Sep 22 09:14 Dec 21 06:50
2074 Mar 20 06:09 Jun 20 22:59 Sep 22 15:04 Dec 21 12:36
2075 Mar 20 11:48 Jun 21 04:41 Sep 22 21:00 Dec 21 18:28
2076 Mar 19 17:37 Jun 20 10:35 Sep 22 02:48 Dec 21 00:12
2077 Mar 19 23:30 Jun 20 16:23 Sep 22 08:35 Dec 21 06:00
2078 Mar 20 05:11 Jun 20 21:58 Sep 22 14:25 Dec 21 11:59
2079 Mar 20 11:03 Jun 21 03:51 Sep 22 20:15 Dec 21 17:46
2080 Mar 19 16:43 Jun 20 09:33 Sep 22 01:55 Dec 20 23:31
2081 Mar 19 22:34 Jun 20 15:16 Sep 22 07:38 Dec 21 05:22
2082 Mar 20 04:32 Jun 20 21:04 Sep 22 13:24 Dec 21 11:06
2083 Mar 20 10:08 Jun 21 02:41 Sep 22 19:10 Dec 21 16:51
2084 Mar 19 15:58 Jun 20 08:39 Sep 22 00:58 Dec 20 22:40
2085 Mar 19 21:53 Jun 20 14:33 Sep 22 06:43 Dec 21 04:29
2086 Mar 20 03:36 Jun 20 20:11 Sep 22 12:33 Dec 21 10:24
2087 Mar 20 09:27 Jun 21 02:05 Sep 22 18:27 Dec 21 16:07
2088 Mar 19 15:16 Jun 20 07:57 Sep 22 00:18 Dec 20 21:56
2089 Mar 19 21:07 Jun 20 13:43 Sep 22 06:07 Dec 21 03:53
2090 Mar 20 03:03 Jun 20 19:37 Sep 22 12:01 Dec 21 09:45
2091 Mar 20 08:40 Jun 21 01:17 Sep 22 17:49 Dec 21 15:37
2092 Mar 19 14:33 Jun 20 07:14 Sep 21 23:41 Dec 20 21:31
2093 Mar 19 20:35 Jun 20 13:08 Sep 22 05:30 Dec 21 03:21
2094 Mar 20 02:20 Jun 20 18:40 Sep 22 11:15 Dec 21 09:11
2095 Mar 20 08:14 Jun 21 00:38 Sep 22 17:10 Dec 21 15:00
2096 Mar 19 14:03 Jun 20 06:31 Sep 21 22:55 Dec 20 20:46
2097 Mar 19 19:49 Jun 20 12:14 Sep 22 04:37 Dec 21 02:38
2098 Mar 20 01:38 Jun 20 18:01 Sep 22 10:22 Dec 21 08:19
2099 Mar 20 07:17 Jun 20 23:41 Sep 22 16:10 Dec 21 14:04
2100 Mar 20 13:04 Jun 21 05:32 Sep 22 22:00 Dec 21 19:51
Then I used these codes on PowerShell 7.1 x64 on Windows 10:
$timetable = Get-Content .\Desktop\Equinox-Solstice.txt | Convertfrom-String -Templatefile .\Desktop\template.txt
$count=$timetable.count
$timetable1=#()
for ($i=0;$i -lt $count;$i++) {
$year=[string]$timetable[$i].year
$mequi=[datetime]($year+" "+$timetable[$i].marequi)
$jsols=[datetime]($year+" "+$timetable[$i].junsols)
$sequi=[datetime]($year+" "+$timetable[$i].sepequi)
$dsols=[datetime]($year+" "+$timetable[$i].decsols)
$timetable1+=[pscustomobject]#{year=$year;mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols}
}
With this as template:
{[int]year*:2001} {[string]marequi:Mar 20 13:31} {[string]junsols:Jun 21 07:38} {[string]sepequi:Sep 22 23:05} {[string]decsols:Dec 21 19:22}
{[int]year*:2002} {[string]marequi:Mar 20 19:16} {[string]junsols:Jun 21 13:25} {[string]sepequi:Sep 23 04:56} {[string]decsols:Dec 22 01:15}
{[int]year*:2003} {[string]marequi:Mar 21 01:00} {[string]junsols:Jun 21 19:11} {[string]sepequi:Sep 23 10:47} {[string]decsols:Dec 22 07:04}
{[int]year*:2004} {[string]marequi:Mar 20 06:49} {[string]junsols:Jun 21 00:57} {[string]sepequi:Sep 22 16:30} {[string]decsols:Dec 21 12:42}
{[int]year*:2005} {[string]marequi:Mar 20 12:34} {[string]junsols:Jun 21 06:46} {[string]sepequi:Sep 22 22:23} {[string]decsols:Dec 21 18:35}
{[int]year*:2006} {[string]marequi:Mar 20 18:25} {[string]junsols:Jun 21 12:26} {[string]sepequi:Sep 23 04:04} {[string]decsols:Dec 22 00:22}
{[int]year*:2007} {[string]marequi:Mar 21 00:07} {[string]junsols:Jun 21 18:06} {[string]sepequi:Sep 23 09:51} {[string]decsols:Dec 22 06:08}
{[int]year*:2008} {[string]marequi:Mar 20 05:49} {[string]junsols:Jun 21 00:00} {[string]sepequi:Sep 22 15:45} {[string]decsols:Dec 21 12:04}
{[int]year*:2009} {[string]marequi:Mar 20 11:44} {[string]junsols:Jun 21 05:45} {[string]sepequi:Sep 22 21:18} {[string]decsols:Dec 21 17:47}
{[int]year*:2010} {[string]marequi:Mar 20 17:32} {[string]junsols:Jun 21 11:28} {[string]sepequi:Sep 23 03:09} {[string]decsols:Dec 21 23:38}
And when I ran the command, I got lots of error messages, all of them are like this:
InvalidArgument:
Line |
3 | $mequi=[datetime]($year+" "+$timetable[$i].marequi)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot convert value "2093 " to type "System.DateTime". Error: "String '2093 ' was not recognized as a valid DateTime."
I have checked thoroughly and found all of them are from $mequi, which stands for March Equinox, I realised the dates must be missing, then I typed $timetable and confirmed lots of March Equinox dates are missing, and only March Equinox dates are missing, I then used this command:
for($i=0;$i -lt 99;$i++){if ($timetable[$i].marequi -eq $null){$i}}
And found out 46 of them are missing, but the dates in other 3 categories are not missing, just March Equinox, the indexes of the missing dates are the following:
11, 15, 19, 23, 27, 31, 35, 39, 40, 43, 44, 47, 48, 51, 52, 55, 56, 59, 60, 63, 64, 67, 68, 69, 71, 72, 73, 75, 76, 77, 79, 80, 81, 83, 84, 85, 87, 88, 89, 91, 92, 93, 95, 96, 97, 98
Any ideas? Can someone help me, please?
Update3: So I used the complete code:
$timetable = Get-Content .\Desktop\Equinox-Solstice.txt | Convertfrom-String -Templatefile .\Desktop\template.txt
$count=$timetable.count
[array]$timetable1 = 0..99 | foreach-object {
$year=[string]$timetable[$_].year
$mequi=[datetime]($year+" "+$timetable[$_].marequi)
$jsols=[datetime]($year+" "+$timetable[$_].junsols)
$sequi=[datetime]($year+" "+$timetable[$_].sepequi)
$dsols=[datetime]($year+" "+$timetable[$_].decsols)
[pscustomobject]#{year=$year;mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols}
}
[array]$seasons=0..98 | foreach-object {
$year=$timetable1[$_].year
$spring=[double](New-Timespan -Start $timetable1[$_].mequi -End $timetable1[$_].jsols).totaldays
$summer=[double](New-Timespan -Start $timetable1[$_].jsols -End $timetable1[$_].sequi).totaldays
$autumn=[double](New-Timespan -Start $timetable1[$_].sequi -End $timetable1[$_].dsols).totaldays
$winter=[double](New-Timespan -Start $timetable1[$_].dsols -End $timetable1[$_+1].mequi).totaldays
[pscustomobject]#{year=$year;spring=$spring;summer=$summer;autumn=$autumn;winter=$winter}
}
$meanspring=($seasons.spring | Measure-Object -Average).average
$meansummer=($seasons.summer | Measure-Object -Average).average
$meanautumn=($seasons.autumn | Measure-Object -Average).average
$meanwinter=($seasons.winter | Measure-Object -Average).average
$meanyear=$meanspring+$meansummer+$meanautumn+$meanwinter
Write-Host "Mean Spring Length: $meanspring days, Mean Summer Length: $meansummer days, Mean Autumn Length: $meanautumn days, Mean Winter Length: $meanwinter days, Mean Solar Year Length: $meanyear days"
And this is what I got:
Mean Spring Length: 92.7203563411897 days, Mean Summer Length: 93.6684764309764 days, Mean Autumn Length: 89.8785914702581 days, Mean Winter Length: 88.9748106060606 days, Mean Solar Year Length: 365.242234848485 days.
Update: I am now editing the question because I want to share a better method.
With a little more effort at find and replace in Notepad++ I was able to obtain this:
"year","marequi","junsols","sepequi","decsols"
"2001","Mar 20 13:31","Jun 21 07:38","Sep 22 23:05","Dec 21 19:22"
"2002","Mar 20 19:16","Jun 21 13:25","Sep 23 04:56","Dec 22 01:15"
"2003","Mar 21 01:00","Jun 21 19:11","Sep 23 10:47","Dec 22 07:04"
"2004","Mar 20 06:49","Jun 21 00:57","Sep 22 16:30","Dec 21 12:42"
"2005","Mar 20 12:34","Jun 21 06:46","Sep 22 22:23","Dec 21 18:35"
"2006","Mar 20 18:25","Jun 21 12:26","Sep 23 04:04","Dec 22 00:22"
"2007","Mar 21 00:07","Jun 21 18:06","Sep 23 09:51","Dec 22 06:08"
"2008","Mar 20 05:49","Jun 21 00:00","Sep 22 15:45","Dec 21 12:04"
"2009","Mar 20 11:44","Jun 21 05:45","Sep 22 21:18","Dec 21 17:47"
"2010","Mar 20 17:32","Jun 21 11:28","Sep 23 03:09","Dec 21 23:38"
"2011","Mar 20 23:21","Jun 21 17:16","Sep 23 09:05","Dec 22 05:30"
"2012","Mar 20 05:15","Jun 20 23:08","Sep 22 14:49","Dec 21 11:12"
"2013","Mar 20 11:02","Jun 21 05:04","Sep 22 20:44","Dec 21 17:11"
"2014","Mar 20 16:57","Jun 21 10:52","Sep 23 02:30","Dec 21 23:03"
"2015","Mar 20 22:45","Jun 21 16:38","Sep 23 08:20","Dec 22 04:48"
"2016","Mar 20 04:31","Jun 20 22:35","Sep 22 14:21","Dec 21 10:45"
"2017","Mar 20 10:29","Jun 21 04:25","Sep 22 20:02","Dec 21 16:29"
"2018","Mar 20 16:15","Jun 21 10:07","Sep 23 01:54","Dec 21 22:22"
"2019","Mar 20 21:58","Jun 21 15:54","Sep 23 07:50","Dec 22 04:19"
"2020","Mar 20 03:50","Jun 20 21:43","Sep 22 13:31","Dec 21 10:03"
"2021","Mar 20 09:37","Jun 21 03:32","Sep 22 19:21","Dec 21 15:59"
"2022","Mar 20 15:33","Jun 21 09:14","Sep 23 01:04","Dec 21 21:48"
"2023","Mar 20 21:25","Jun 21 14:58","Sep 23 06:50","Dec 22 03:28"
"2024","Mar 20 03:07","Jun 20 20:51","Sep 22 12:44","Dec 21 09:20"
"2025","Mar 20 09:02","Jun 21 02:42","Sep 22 18:20","Dec 21 15:03"
"2026","Mar 20 14:46","Jun 21 08:25","Sep 23 00:06","Dec 21 20:50"
"2027","Mar 20 20:25","Jun 21 14:11","Sep 23 06:02","Dec 22 02:43"
"2028","Mar 20 02:17","Jun 20 20:02","Sep 22 11:45","Dec 21 08:20"
"2029","Mar 20 08:01","Jun 21 01:48","Sep 22 17:37","Dec 21 14:14"
"2030","Mar 20 13:51","Jun 21 07:31","Sep 22 23:27","Dec 21 20:09"
"2031","Mar 20 19:41","Jun 21 13:17","Sep 23 05:15","Dec 22 01:56"
"2032","Mar 20 01:23","Jun 20 19:09","Sep 22 11:11","Dec 21 07:57"
"2033","Mar 20 07:23","Jun 21 01:01","Sep 22 16:52","Dec 21 13:45"
"2034","Mar 20 13:18","Jun 21 06:45","Sep 22 22:41","Dec 21 19:35"
"2035","Mar 20 19:03","Jun 21 12:33","Sep 23 04:39","Dec 22 01:31"
"2036","Mar 20 01:02","Jun 20 18:31","Sep 22 10:23","Dec 21 07:12"
"2037","Mar 20 06:50","Jun 21 00:22","Sep 22 16:13","Dec 21 13:08"
"2038","Mar 20 12:40","Jun 21 06:09","Sep 22 22:02","Dec 21 19:01"
"2039","Mar 20 18:32","Jun 21 11:58","Sep 23 03:50","Dec 22 00:41"
"2040","Mar 20 00:11","Jun 20 17:46","Sep 22 09:44","Dec 21 06:33"
"2041","Mar 20 06:07","Jun 20 23:37","Sep 22 15:27","Dec 21 12:19"
"2042","Mar 20 11:53","Jun 21 05:16","Sep 22 21:11","Dec 21 18:04"
"2043","Mar 20 17:29","Jun 21 10:59","Sep 23 03:07","Dec 22 00:02"
"2044","Mar 19 23:20","Jun 20 16:50","Sep 22 08:47","Dec 21 05:43"
"2045","Mar 20 05:08","Jun 20 22:34","Sep 22 14:33","Dec 21 11:36"
"2046","Mar 20 10:58","Jun 21 04:15","Sep 22 20:22","Dec 21 17:28"
"2047","Mar 20 16:52","Jun 21 10:02","Sep 23 02:07","Dec 21 23:07"
"2048","Mar 19 22:34","Jun 20 15:54","Sep 22 08:01","Dec 21 05:02"
"2049","Mar 20 04:28","Jun 20 21:47","Sep 22 13:42","Dec 21 10:51"
"2050","Mar 20 10:20","Jun 21 03:33","Sep 22 19:29","Dec 21 16:39"
"2051","Mar 20 15:58","Jun 21 09:17","Sep 23 01:26","Dec 21 22:33"
"2052","Mar 19 21:56","Jun 20 15:16","Sep 22 07:16","Dec 21 04:18"
"2053","Mar 20 03:46","Jun 20 21:03","Sep 22 13:05","Dec 21 10:09"
"2054","Mar 20 09:35","Jun 21 02:47","Sep 22 19:00","Dec 21 16:10"
"2055","Mar 20 15:28","Jun 21 08:39","Sep 23 00:48","Dec 21 21:56"
"2056","Mar 19 21:11","Jun 20 14:29","Sep 22 06:40","Dec 21 03:52"
"2057","Mar 20 03:08","Jun 20 20:19","Sep 22 12:23","Dec 21 09:42"
"2058","Mar 20 09:04","Jun 21 02:03","Sep 22 18:07","Dec 21 15:24"
"2059","Mar 20 14:44","Jun 21 07:47","Sep 23 00:03","Dec 21 21:18"
"2060","Mar 19 20:37","Jun 20 13:44","Sep 22 05:47","Dec 21 03:00"
"2061","Mar 20 02:26","Jun 20 19:33","Sep 22 11:31","Dec 21 08:49"
"2062","Mar 20 08:07","Jun 21 01:10","Sep 22 17:19","Dec 21 14:42"
"2063","Mar 20 13:59","Jun 21 07:02","Sep 22 23:08","Dec 21 20:22"
"2064","Mar 19 19:40","Jun 20 12:47","Sep 22 04:58","Dec 21 02:10"
"2065","Mar 20 01:27","Jun 20 18:31","Sep 22 10:41","Dec 21 07:59"
"2066","Mar 20 07:19","Jun 21 00:16","Sep 22 16:27","Dec 21 13:45"
"2067","Mar 20 12:55","Jun 21 05:56","Sep 22 22:20","Dec 21 19:44"
"2068","Mar 19 18:51","Jun 20 11:55","Sep 22 04:09","Dec 21 01:34"
"2069","Mar 20 00:44","Jun 20 17:40","Sep 22 09:51","Dec 21 07:21"
"2070","Mar 20 06:35","Jun 20 23:22","Sep 22 15:45","Dec 21 13:19"
"2071","Mar 20 12:36","Jun 21 05:21","Sep 22 21:39","Dec 21 19:05"
"2072","Mar 19 18:19","Jun 20 11:12","Sep 22 03:26","Dec 21 00:54"
"2073","Mar 20 00:12","Jun 20 17:06","Sep 22 09:14","Dec 21 06:50"
"2074","Mar 20 06:09","Jun 20 22:59","Sep 22 15:04","Dec 21 12:36"
"2075","Mar 20 11:48","Jun 21 04:41","Sep 22 21:00","Dec 21 18:28"
"2076","Mar 19 17:37","Jun 20 10:35","Sep 22 02:48","Dec 21 00:12"
"2077","Mar 19 23:30","Jun 20 16:23","Sep 22 08:35","Dec 21 06:00"
"2078","Mar 20 05:11","Jun 20 21:58","Sep 22 14:25","Dec 21 11:59"
"2079","Mar 20 11:03","Jun 21 03:51","Sep 22 20:15","Dec 21 17:46"
"2080","Mar 19 16:43","Jun 20 09:33","Sep 22 01:55","Dec 20 23:31"
"2081","Mar 19 22:34","Jun 20 15:16","Sep 22 07:38","Dec 21 05:22"
"2082","Mar 20 04:32","Jun 20 21:04","Sep 22 13:24","Dec 21 11:06"
"2083","Mar 20 10:08","Jun 21 02:41","Sep 22 19:10","Dec 21 16:51"
"2084","Mar 19 15:58","Jun 20 08:39","Sep 22 00:58","Dec 20 22:40"
"2085","Mar 19 21:53","Jun 20 14:33","Sep 22 06:43","Dec 21 04:29"
"2086","Mar 20 03:36","Jun 20 20:11","Sep 22 12:33","Dec 21 10:24"
"2087","Mar 20 09:27","Jun 21 02:05","Sep 22 18:27","Dec 21 16:07"
"2088","Mar 19 15:16","Jun 20 07:57","Sep 22 00:18","Dec 20 21:56"
"2089","Mar 19 21:07","Jun 20 13:43","Sep 22 06:07","Dec 21 03:53"
"2090","Mar 20 03:03","Jun 20 19:37","Sep 22 12:01","Dec 21 09:45"
"2091","Mar 20 08:40","Jun 21 01:17","Sep 22 17:49","Dec 21 15:37"
"2092","Mar 19 14:33","Jun 20 07:14","Sep 21 23:41","Dec 20 21:31"
"2093","Mar 19 20:35","Jun 20 13:08","Sep 22 05:30","Dec 21 03:21"
"2094","Mar 20 02:20","Jun 20 18:40","Sep 22 11:15","Dec 21 09:11"
"2095","Mar 20 08:14","Jun 21 00:38","Sep 22 17:10","Dec 21 15:00"
"2096","Mar 19 14:03","Jun 20 06:31","Sep 21 22:55","Dec 20 20:46"
"2097","Mar 19 19:49","Jun 20 12:14","Sep 22 04:37","Dec 21 02:38"
"2098","Mar 20 01:38","Jun 20 18:01","Sep 22 10:22","Dec 21 08:19"
"2099","Mar 20 07:17","Jun 20 23:41","Sep 22 16:10","Dec 21 14:04"
"2100","Mar 20 13:04","Jun 21 05:32","Sep 22 22:00","Dec 21 19:51"
Save as seasons.csv
Now use these codes:
$Seasons=Import-Csv path\to\seasons.csv | % {
$MEqui=[DateTime]$([string]($_.Year )+$_.marequi)
$JSols=[DateTime]$([string]($_.Year )+$_.junsols)
$SEqui=[DateTime]$([string]($_.Year )+$_.sepequi)
$DSols=[DateTime]$([string]($_.Year )+$_.decsols)
[pscustomobject]#{mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols}
}
$Seasons | %{
$MEqui=$MEqui.ToString(yyyy-MM-ddTHH:mm:ssZ)
$JSols=$JSols.ToString(yyyy-MM-ddTHH:mm:ssZ)
$SEqui=$SEqui.ToString(yyyy-MM-ddTHH:mm:ssZ)
$DSols=$DSols.ToString(yyyy-MM-ddTHH:mm:ssZ)
[pscustomobject]#{MarchEquinox=$mequi;JuneSolstice=$jsols;SeptemberEquinox=$sequi;DecemberSolstice=$dsols} | Export-Csv path\to\output.csv -NoTypeInformation -Append
}
And the rest of the steps are the same (just replace $timetable1 with $seasons), this approach is better because importing from and exporting to csv is much easier and far more reliable.
For the template issue, it appears that you have only one sample value (Jun 21) for the junsols column, if you update that, you will see that it works just fine.
In general, I would recommend the following template to cover all the dates:
{[int]year*:0000} {[string]marequi:Mar 00 00:00} {[string]junsols:Jun 00 00:00} {[string]sepequi:Sep 00 00:00} {[string]decsols:Dec 00 00:00}
{[int]year*:9999} {[string]marequi:Mar 99 99:99} {[string]junsols:Jun 99 99:99} {[string]sepequi:Sep 99 99:99} {[string]decsols:Dec 99 99:99}
Also (as a side note): try to avoid using the increase assignment operator (+=) to create a collection as it is exponential expensive
I have a data structure in Ruby as below:
[["N1-Alb",
{'Sun, 05 Feb 2017'=>"",
'Mon, 06 Feb 2017'=>"",
'Tue, 07 Feb 2017'=>"",
'Wed, 08 Feb 2017'=>"0.25",
'Thu, 09 Feb 2017'=>"0.03",
'Fri, 10 Feb 2017'=>"",
'Sat, 11 Feb 2017'=>""}],
["N1-Cet",
{'Sun, 05 Feb 2017'=>"",
'Mon, 06 Feb 2017'=>"7.8",
'Tue, 07 Feb 2017'=>"",
'Wed, 08 Feb 2017'=>"0.00",
'Thu, 09 Feb 2017'=>"",
'Fri, 10 Feb 2017'=>"",
'Sat, 11 Feb 2017'=>""}],
["N3-Tju",
{'Sun, 05 Feb 2017'=>"",
'Mon, 06 Feb 2017'=>"",
'Tue, 07 Feb 2017'=>"",
'Wed, 08 Feb 2017'=>"3.15",
'Thu, 09 Feb 2017'=>"",
'Fri, 10 Feb 2017'=>"8.0",
'Sat, 11 Feb 2017'=>""}],
["N7-Mlp",
{'Sun, 05 Feb 2017'=>"",
'Mon, 06 Feb 2017'=>"",
'Tue, 07 Feb 2017'=>"5.01",
'Wed, 08 Feb 2017'=>"0.03",
'Thu, 09 Feb 2017'=>"",
'Fri, 10 Feb 2017'=>"",
'Sat, 11 Feb 2017'=>"4"}]]
How can I get sum for all Sundays, Mondays etc. up to Saturdays separately in to a Hash or an Array format?
The final hash should be:
result = { 'sun': '0',
'mon': '7.8',
'tue': '5.01',
'wed': '3.43',
'thu': '0.03',
'fri': '8.0',
'sat': '4' }
Try this:
days = [:mon, :tue, :wed, :thu, :fri, :sat, :sun]
result = your_hash.each_with_object({}) do |n, h|
n[1].each do |key, value|
h[days[key.cwday - 1]] = (h[days[key.cwday - 1]].to_f + value.to_f).to_s
end
end
I updated the code to be more concise, per your request. This code exploits the fact that nil.to_f == 0.0, which may upset some stomachs.
My nonlinear mixed-effects model regresses body mass (bm) on age. I would like consider that brood is nested within year, but as a brood can only occur in one of the seven years that are in the dataset, the random effects of year and brood should be crossed.
In Pinheiro & Bates (2000): ‘Mixed-Effects Models in S and S-plus’ (http://link.springer.com/book/10.1007%2Fb98882), there is one example with crossed random effects: A full factorial model of a cell culture bioassay is used to represent the fixed effects, and three random effects are used to account for block, row, and column effects, with the last two random effects nested within block, but crossed with each other. This is implemented followingly (p. 163 - 165):
options( contrasts = c("contr.treatment", "contr.poly") )
fm1Assay <- lme( logDens ~ sample * dilut, Assay,
random = pdBlocked(list(pdIdent(~ 1), pdIdent(~ sample - 1), pdIdent(~ dilut - 1))) )
Their example is a linear mixed model, but the syntax should work also for nonlinear mixed models. Therefore, I tried the following for my model (I do not know however, why the nested effects have to have -1 after the covariate):
fmbm1 <- nlme(bm ~ SSlogis(age, Asym, xmid, scal), data = bmgrp,
fixed = Asym + xmid + scal ~ 1,
random = pdBlocked(list(pdIdent(Asym + xmid + scal ~ 1),
pdIdent(Asym + xmid + scal ~ brood - 1))),
start = startvalues, na.action = na.omit)
But this syntax seems to be wrong, as it makes R freeze (there isn't even an error message, R just does not respond anymore so that I need to force quitting R).
I would be happy about tips how to realise the nested and crossed structure in nlme.
Here is the structure of my data and below that the previous R code:
> str(bmgrp)
Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 428 obs. of 7 variables:
$ pop : Factor w/ 2 levels "NL","RUS": 1 2 2 2 2 2 1 2 2 2 ...
$ year : Ord.factor w/ 7 levels "2013"<"2012"<..: 5 1 1 7 1 1 2 1 1 1 ...
$ sex : Factor w/ 2 levels "f","m": 2 1 1 2 1 2 1 2 1 2 ...
$ age : int 0 0 7 8 10 10 10 12 12 12 ...
$ bm : int 80 85 137 165 225 227 132 215 183 195 ...
$ relhatch: num 4.06 4.22 4.22 4.29 1.22 ...
$ brood : Factor w/ 270 levels "","NL2004N1040054",..: 61 189 189 205 190 190 27 187 187 187 ...
bmdata <- read.table(header = TRUE, stringsAsFactors = TRUE, text = "
ID pop year sex age bm relhatch brood
1 NL 2015 m 0 80 4.06293706 NL2015E278
2 RUS 2013 f 0 85 4.22000000 RUS2013N350
7 RUS 2013 f 7 137 4.21888412 RUS2013N350
9 RUS 2014 m 8 165 4.28608247 RUS2014N1372
11 RUS 2013 f 10 225 1.21888412 RUS2013N396
12 RUS 2013 m 10 227 1.21888412 RUS2013N396
13 NL 2012 f 10 132 0.86649874 NL2012E262
17 RUS 2013 m 12 215 -0.78111588 RUS2013N120
18 RUS 2013 f 12 183 -0.78111588 RUS2013N120
19 RUS 2013 m 12 195 -0.78111588 RUS2013N120
21 RUS 2014 m 13 325 -0.71391753 RUS2014N235
22 RUS 2014 m 13 280 -0.71391753 RUS2014N235
27 RUS 2013 m 16 498 -4.78111588 RUS2013N60
28 RUS 2005 m 17 870 3.20000000 RUS2005N1030701
29 NL 2012 m 18 324 -11.13350126 NL2012E45
32 RUS 2003 f 19 880 1.20000000 RUS2003N1030266
33 RUS 2005 f 19 750 6.10000000 RUS2005N1051002
35 RUS 2004 f 20 670 3.20000000 RUS2004N1030477
36 RUS 2004 m 20 790 3.20000000 RUS2004N1030491
37 RUS 2004 m 20 940 3.20000000 RUS2004N1030476
39 RUS 2004 f 20 900 3.20000000 RUS2004N1030521
40 RUS 2004 f 20 740 3.20000000 RUS2004N1030473
41 RUS 2003 m 21 980 -0.80000000 RUS2003N1030429
42 NL 2012 f 21 260 -2.13350126 NL2012Wun23
44 RUS 2014 m 21 620 -3.71391753 RUS2014N248
45 RUS 2003 m 21 1080 -0.80000000 RUS2003N1030045
46 NL 2012 m 21 237 -0.13350126 NL2012E265
47 RUS 2004 m 21 572 2.80000000 RUS2004N1040648
48 RUS 2004 m 21 940 3.20000000 RUS2004N1030482
49 RUS 2004 m 21 450 4.20000000 RUS2004N1030645
53 RUS 2014 f 22 660 -0.71391753 RUS2014N866
54 RUS 2003 f 22 890 -1.80000000 RUS2003N1030415
55 RUS 2005 f 22 684 0.80000000 RUS2005N1040060
56 RUS 2005 f 22 652 0.80000000 RUS2005N1040060
57 RUS 2003 f 22 776 -1.80000000 RUS2003N1030418
58 RUS 2004 m 22 672 1.80000000 RUS2004N1042138
59 RUS 2004 f 22 622 1.80000000 RUS2004N1042138
61 RUS 2004 m 22 472 3.30000000 RUS2004N1042092
62 RUS 2004 f 22 438 3.30000000 RUS2004N1042092
63 NL 2012 m 22 222 7.86649874 NL2012W213
64 RUS 2003 m 22 1410 -1.80000000 RUS2003N1030417
65 RUS 2003 f 22 1010 -1.80000000 RUS2003N1030089
66 RUS 2004 f 22 612 1.20000000 RUS2004N1030470
67 RUS 2004 m 22 598 1.80000000 RUS2004N1042139
68 RUS 2004 f 22 316 1.30000000 RUS2004N1042114
69 RUS 2004 m 22 420 3.20000000 RUS2004N1030474
70 RUS 2004 m 22 370 3.30000000 RUS2004N1042095
76 RUS 2004 f 23 1080 0.20000000 RUS2004N1030467
77 RUS 2003 f 23 854 -2.80000000 RUS2003N1030196
78 RUS 2003 f 23 897 -1.80000000 RUS2003N1030402
79 RUS 2003 m 23 1210 -1.80000000 RUS2003N1030416
80 RUS 2003 f 23 1180 -1.80000000 RUS2003N1030416
81 RUS 2004 m 23 990 0.20000000 RUS2004N1030446
82 RUS 2004 f 23 990 0.20000000 RUS2004N1030446
83 RUS 2004 f 23 1110 0.20000000 RUS2004N1030461
84 RUS 2014 m 23 710 -5.71391753 RUS2014N160
85 RUS 2014 f 23 695 -1.71391753 RUS2014N600
86 RUS 2003 m 23 1230 -1.80000000 RUS2003N1030404
87 RUS 2004 m 23 870 0.20000000 RUS2004N1030465
88 RUS 2004 f 23 500 0.80000000 RUS2004N1040578
89 RUS 2004 m 23 598 0.80000000 RUS2004N1040288
90 RUS 2004 f 23 480 2.80000000 RUS2004N1040685
91 RUS 2004 m 23 444 2.80000000 RUS2004N1040688
92 RUS 2005 f 23 820 -0.80000000 RUS2005N1030706
99 RUS 2004 m 24 1120 -0.80000000 RUS2004N1030467
100 RUS 2004 m 24 612 -0.20000000 RUS2004N1040474
101 RUS 2004 m 24 642 -0.20000000 RUS2004N1040474
102 RUS 2004 m 24 634 -0.20000000 RUS2004N1040474
103 RUS 2014 f 24 740 2.28608247 RUS2014N49
104 RUS 2014 f 24 840 2.28608247 RUS2014N49
105 RUS 2014 m 24 900 2.28608247 RUS2014N49
106 RUS 2005 m 24 972 0.80000000 RUS2005N1040106
107 RUS 2005 f 24 820 0.80000000 RUS2005N1040106
108 RUS 2005 f 24 910 0.80000000 RUS2005N1040106
109 RUS 2014 m 24 980 2.28608247 RUS2014N759
110 RUS 2014 f 24 775 2.28608247 RUS2014N759
111 RUS 2004 f 24 870 0.20000000 RUS2004N1030457
112 RUS 2004 f 24 516 0.80000000 RUS2004N1040528
113 RUS 2004 f 24 672 1.80000000 RUS2004N1040828
114 RUS 2004 f 24 604 1.80000000 RUS2004N1040828
115 RUS 2004 m 24 790 3.20000000 RUS2004N1030491
116 RUS 2004 m 24 940 3.20000000 RUS2004N1030487
117 RUS 2004 m 24 1040 3.20000000 RUS2004N1030487
118 NL 2015 m 24 300 4.06293706 NL2015E278
119 NL 2012 m 24 258 -8.13350126 NL2012Eun7
120 NL 2012 f 24 190 3.86649874 NL2012E239
121 RUS 2014 m 24 1085 2.28608247 RUS2014N858
122 RUS 2003 m 24 1100 1.20000000 RUS2003N1030333
123 RUS 2004 m 24 610 -0.70000000 RUS2004N1040622
124 RUS 2004 m 24 674 -0.20000000 RUS2004N1040444
125 RUS 2004 m 24 772 -0.20000000 RUS2004N1040285
126 RUS 2004 m 24 760 -0.20000000 RUS2004N1040571
127 RUS 2005 m 24 1060 0.80000000 RUS2005N1040118
139 RUS 2014 f 25 860 4.28608247 RUS2014N766
140 RUS 2014 m 25 1040 4.28608247 RUS2014N766
142 NL 2015 f 25 490 -0.93706294 NL2015W224
143 RUS 2014 m 25 1002 1.28608247 RUS2014NT1
144 RUS 2014 f 25 880 1.28608247 RUS2014N812
145 RUS 2014 m 25 955 1.28608247 RUS2014N111
146 RUS 2014 m 25 985 1.28608247 RUS2014N111
147 RUS 2003 f 25 980 -0.80000000 RUS2003N1030045
148 RUS 2004 f 25 1070 0.20000000 RUS2004N1030466
149 RUS 2004 m 25 1050 0.20000000 RUS2004N1030439
150 RUS 2004 f 25 940 0.20000000 RUS2004N1030439
151 RUS 2004 f 25 900 0.20000000 RUS2004N1030461
152 RUS 2014 m 25 985 1.28608247 RUS2014N602
153 RUS 2004 m 25 560 -1.20000000 RUS2004N1040503
154 RUS 2004 f 25 680 0.20000000 RUS2004N1030437
155 RUS 2004 f 25 910 0.20000000 RUS2004N1030451
156 RUS 2004 m 25 484 0.80000000 RUS2004N1040541
158 RUS 2014 f 26 985 0.28608247 RUS2014N61
159 RUS 2014 f 26 882 0.28608247 RUS2014N61
160 RUS 2004 m 26 626 -0.20000000 RUS2004N1040474
161 RUS 2003 f 26 900 -0.80000000 RUS2003N1030429
162 RUS 2004 m 26 810 3.20000000 RUS2004N1030476
163 RUS 2014 f 26 860 0.28608247 RUS2014N706
164 RUS 2014 f 26 945 2.28608247 RUS2014N963
165 NL 2004 f 26 480 23.00000000 NL2004N1040293
166 RUS 2004 f 26 720 -0.70000000 RUS2004N1040832
167 RUS 2004 m 26 658 1.30000000 RUS2004N1042086
168 RUS 2004 f 26 634 1.80000000 RUS2004N1042149
171 RUS 2004 f 27 1300 -1.80000000 RUS2004N1030455
172 RUS 2014 f 27 800 -0.71391753 RUS2014N619
173 RUS 2014 m 27 1020 -0.71391753 RUS2014N411
174 RUS 2014 m 27 1040 -0.71391753 RUS2014N411
175 RUS 2014 f 27 980 3.28608247 RUS2014N1119
176 RUS 2014 m 27 990 3.28608247 RUS2014N1119
177 RUS 2014 m 27 925 3.28608247 RUS2014N1119
178 RUS 2003 f 27 762 -1.80000000 RUS2003N1030400
179 RUS 2003 f 27 880 -1.80000000 RUS2003N1030415
180 RUS 2003 f 27 1020 -1.80000000 RUS2003N1030406
181 RUS 2003 f 27 950 -1.80000000 RUS2003N1030406
182 RUS 2003 m 27 1060 -1.80000000 RUS2003N1030406
183 RUS 2003 m 27 772 0.20000000 RUS2003N1030400
184 NL 2015 m 27 525 1.06293706 NL2015E222
185 RUS 2014 f 27 830 2.28608247 RUS2014N769
186 RUS 2014 f 27 940 2.28608247 RUS2014N769
187 RUS 2003 m 27 976 -1.80000000 RUS2003N1030402
188 RUS 2003 f 27 1050 -1.80000000 RUS2003N1030413
189 RUS 2003 m 27 1090 -1.80000000 RUS2003N1030414
190 RUS 2003 f 27 1140 -1.80000000 RUS2003N1030413
191 RUS 2003 f 27 1080 -1.80000000 RUS2003N1030414
192 RUS 2004 f 27 630 3.20000000 RUS2004N1030475
193 NL 2012 m 27 290 0.86649874 NL2012W103
194 RUS 2014 m 27 1100 -0.71391753 RUS2014N519
195 RUS 2014 m 27 985 -0.71391753 RUS2014N629
196 RUS 2014 m 27 925 1.28608247 RUS2014N533
197 RUS 2014 f 27 950 2.28608247 RUS2014N452
198 RUS 2003 f 27 1060 -1.80000000 RUS2003N1030411
199 RUS 2005 f 27 990 3.10000000 RUS2005N1050621
204 NL 2012 f 28 237 -0.13350126 NL2012E262
206 RUS 2014 m 28 1085 -1.71391753 RUS2014N321
207 RUS 2014 m 28 1220 -1.71391753 RUS2014N321
208 RUS 2014 m 28 1010 -1.71391753 RUS2014N321
209 RUS 2014 f 28 1005 2.28608247 RUS2014N554
210 RUS 2014 m 28 1040 2.28608247 RUS2014N554
211 RUS 2014 f 28 1005 2.28608247 RUS2014N554
212 RUS 2003 f 28 960 -0.80000000 RUS2003N1030429
213 RUS 2003 m 28 1330 -0.80000000 RUS2003N1030100
214 RUS 2003 f 28 1140 -0.80000000 RUS2003N1030100
215 RUS 2003 m 28 1340 -0.80000000 RUS2003N1030100
216 RUS 2004 m 28 586 -2.20000000 RUS2004N1040279
217 RUS 2005 m 28 894 0.80000000 RUS2005N1040060
218 RUS 2014 m 28 980 -1.71391753 RUS2014N631
219 RUS 2014 f 28 790 1.28608247 RUS2014NT1
220 RUS 2003 f 28 902 -2.80000000 RUS2003N1030196
221 RUS 2004 f 28 744 -3.20000000 RUS2004N1040600
222 RUS 2005 m 28 576 0.80000000 RUS2005N1040130
223 RUS 2005 f 28 517 0.80000000 RUS2005N1040130
224 RUS 2014 f 28 855 2.28608247 RUS2014N1171
225 RUS 2003 f 28 740 -0.80000000 RUS2003N1030090
226 RUS 2004 m 28 792 -2.20000000 RUS2004N1040381
227 RUS 2005 m 28 1034 0.80000000 RUS2005N1040265
231 NL 2015 f 29 430 -0.93706294 NL2015W228
232 NL 2015 f 29 425 -0.93706294 NL2015W228
233 RUS 2014 m 29 843 -2.71391753 RUS2014N418
234 RUS 2014 f 29 1020 -2.71391753 RUS2014N418
235 RUS 2014 m 29 1100 -4.71391753 RUS2014N123
236 RUS 2014 f 29 1100 -4.71391753 RUS2014N123
237 RUS 2014 m 29 1000 -2.71391753 RUS2014N113
238 RUS 2014 m 29 1160 -2.71391753 RUS2014N113
239 RUS 2014 f 29 1000 -2.71391753 RUS2014N113
240 RUS 2014 f 29 1055 -2.71391753 RUS2014N286
241 RUS 2014 f 29 1100 -2.71391753 RUS2014N286
242 RUS 2014 f 29 1125 -0.71391753 RUS2014NT7
243 RUS 2014 f 29 985 -0.71391753 RUS2014N284
244 RUS 2004 f 29 612 0.80000000 RUS2004N1040528
245 RUS 2005 m 29 624 -0.20000000 RUS2005N1040150
246 RUS 2005 f 29 510 -0.20000000 RUS2005N1040150
247 RUS 2005 m 29 1000 -0.20000000 RUS2005N1040151
248 RUS 2005 f 29 902 -0.20000000 RUS2005N1040151
249 RUS 2014 f 29 1115 -2.71391753 RUS2014N672
250 RUS 2014 f 29 1245 -0.71391753 RUS2014N616
251 RUS 2014 m 29 1325 -0.71391753 RUS2014N239
252 RUS 2004 f 29 860 3.20000000 RUS2004N1030484
253 RUS 2005 m 29 708 -0.70000000 RUS2005N1040163
254 RUS 2005 m 29 618 -0.20000000 RUS2005N1040082
255 RUS 2005 f 29 910 -0.20000000 RUS2005N1040137
264 RUS 2014 m 30 1170 0.28608247 RUS2014N61
265 RUS 2004 f 30 1090 0.20000000 RUS2004N1030467
266 RUS 2014 f 30 1030 -0.71391753 RUS2014N866
267 RUS 2014 m 30 1025 -0.71391753 RUS2014N619
268 RUS 2014 m 30 1120 7.28608247 RUS2014N1016
269 RUS 2014 f 30 1000 7.28608247 RUS2014N1016
270 RUS 2014 f 30 890 7.28608247 RUS2014N1016
271 RUS 2004 m 30 954 -2.20000000 RUS2004N1040606
272 RUS 2004 f 30 965 -2.20000000 RUS2004N1040606
273 RUS 2004 m 30 948 -2.20000000 RUS2004N1040606
274 RUS 2005 m 30 1238 -1.20000000 RUS2005N1040217
275 RUS 2005 m 30 1260 -1.20000000 RUS2005N1040217
276 RUS 2005 f 30 1146 -1.20000000 RUS2005N1040217
277 RUS 2014 m 30 1182 -3.71391753 RUS2014N112
279 RUS 2014 m 30 1260 -3.71391753 RUS2014N112
281 RUS 2014 f 30 1200 -3.71391753 RUS2014N364
282 RUS 2014 m 30 1110 -0.71391753 RUS2014N221
283 RUS 2014 m 30 1235 -0.71391753 RUS2014N221
284 RUS 2004 m 30 880 0.20000000 RUS2004N1030442
285 RUS 2004 f 30 720 0.20000000 RUS2004N1030442
286 RUS 2014 m 30 1022 -3.71391753 RUS2014N238
287 RUS 2014 m 30 1120 -3.71391753 RUS2014N270
288 RUS 2014 m 30 1175 -3.71391753 RUS2014N647
289 RUS 2014 f 30 1100 -3.71391753 RUS2014N288
290 RUS 2014 f 30 980 1.28608247 RUS2014N1032
291 RUS 2004 f 30 840 0.20000000 RUS2004N1030449
292 RUS 2005 m 30 1080 -1.40000000 RUS2005N1050085
294 NL 2012 f 31 430 -3.13350126 NL2012W27
295 NL 2012 m 31 405 -3.13350126 NL2012W27
296 NL 2015 m 31 810 -2.93706294 NL2015E149
297 NL 2015 m 31 740 -2.93706294 NL2015E149
298 RUS 2014 f 31 1140 -0.71391753 RUS2014N914
299 RUS 2014 m 31 1255 -0.71391753 RUS2014N914
300 RUS 2014 f 31 1225 -0.71391753 RUS2014N914
301 RUS 2014 m 31 1020 -0.71391753 RUS2014N411
302 RUS 2014 m 31 1255 2.28608247 RUS2014N698
303 RUS 2014 m 31 1235 2.28608247 RUS2014N698
304 RUS 2014 m 31 1120 2.28608247 RUS2014N698
305 RUS 2004 m 31 1204 -0.70000000 RUS2004N1040833
306 RUS 2004 f 31 1024 -0.70000000 RUS2004N1040833
307 RUS 2004 m 31 790 3.20000000 RUS2004N1030475
308 RUS 2004 m 31 824 1.30000000 RUS2004N1042120
309 RUS 2004 f 31 704 1.30000000 RUS2004N1042142
318 NL 2012 f 32 418 -4.13350126 NL2012W70
319 NL 2012 m 32 410 -4.13350126 NL2012W70
320 RUS 2004 m 32 1340 -1.80000000 RUS2004N1030455
321 RUS 2005 f 32 1140 2.10000000 RUS2005N1050173
322 RUS 2005 m 32 1330 2.10000000 RUS2005N1050173
323 RUS 2005 m 32 1300 2.10000000 RUS2005N1050173
324 RUS 2005 m 32 1210 2.10000000 RUS2005N1050173
325 RUS 2005 f 32 1070 2.10000000 RUS2005N1050173
326 NL 2015 m 32 810 -3.93706294 NL2015W40
327 NL 2012 m 32 408 -4.13350126 NL2012E25
328 NL 2012 f 32 282 -4.13350126 NL2012E25
330 RUS 2005 f 32 1050 2.10000000 RUS2005N1050505
331 RUS 2005 f 32 1050 2.10000000 RUS2005N1050505
332 RUS 2005 f 32 930 2.10000000 RUS2005N1050505
333 RUS 2014 f 32 1125 -3.71391753 RUS2014N91
334 RUS 2014 m 32 1402 -3.71391753 RUS2014N91
335 RUS 2014 f 32 1085 -1.71391753 RUS2014N631
336 RUS 2003 f 32 1140 -0.80000000 RUS2003N1030424
337 RUS 2003 m 32 1100 -0.80000000 RUS2003N1030424
338 RUS 2004 m 32 1010 0.20000000 RUS2004N1030457
339 RUS 2005 m 32 1008 -3.20000000 RUS2005N1040180
340 RUS 2005 f 32 830 -3.20000000 RUS2005N1040180
342 RUS 2014 f 32 1140 -5.71391753 RUS2014N117
343 NL 2004 f 32 530 17.50000000 NL2004N1041011
344 RUS 2004 f 32 640 0.20000000 RUS2004N1030459
348 RUS 2014 m 33 1265 -3.71391753 RUS2014N293
349 RUS 2014 m 33 1190 -3.71391753 RUS2014N293
350 RUS 2014 m 33 1225 -3.71391753 RUS2014N293
351 RUS 2014 f 33 1090 -3.71391753 RUS2014N293
352 RUS 2004 m 33 1160 -0.80000000 RUS2004N1030467
354 RUS 2003 m 33 980 -1.80000000 RUS2003N1030415
355 RUS 2004 f 33 476 -2.20000000 RUS2004N1040279
356 RUS 2004 m 33 554 -2.20000000 RUS2004N1040279
357 RUS 2014 f 33 1310 4.28608247 RUS2014N1000
358 RUS 2014 f 33 1060 4.28608247 RUS2014N1000
359 RUS 2003 m 33 842 -1.80000000 RUS2003N1030418
360 RUS 2005 m 33 1050 -2.90000000 RUS2005N1050580
361 RUS 2005 m 33 1226 -2.90000000 RUS2005N1050580
362 RUS 2014 m 33 1190 -2.71391753 RUS2014N175
363 RUS 2014 f 33 1105 4.28608247 RUS2014N755
364 RUS 2014 m 33 1260 4.28608247 RUS2014N801
365 RUS 2003 f 33 850 -1.80000000 RUS2003N1030422
367 RUS 2004 m 33 1054 -2.20000000 RUS2004N1040478
368 RUS 2004 f 33 810 0.20000000 RUS2004N1030456
369 RUS 2004 m 33 944 1.80000000 RUS2004N1041049
370 RUS 2004 m 33 1102 1.30000000 RUS2004N1042127
371 RUS 2004 f 33 1050 1.80000000 RUS2004N1040821
372 RUS 2004 f 33 935 1.80000000 RUS2004N1042131
373 RUS 2005 m 33 868 -4.20000000 RUS2005N1040266
376 NL 2012 m 34 270 -6.13350126 NL2012W15
377 NL 2012 f 34 438 -6.13350126 NL2012W15
379 NL 2012 m 34 440 -6.13350126 NL2012Wun15
380 NL 2012 f 34 440 -6.13350126 NL2012Wun15
383 RUS 2014 f 34 990 -4.71391753 RUS2014N216
384 RUS 2014 f 34 1220 -3.71391753 RUS2014N106
385 RUS 2014 f 34 1090 -3.71391753 RUS2014N106
386 RUS 2014 m 34 1240 -0.71391753 RUS2014NT7
387 RUS 2014 f 34 1085 3.28608247 RUS2014N896
388 RUS 2014 m 34 1110 3.28608247 RUS2014N896
389 RUS 2004 m 34 1074 -1.20000000 RUS2004N1040269
390 RUS 2004 f 34 980 -1.20000000 RUS2004N1040269
391 RUS 2004 f 34 930 0.20000000 RUS2004N1030466
392 RUS 2004 f 34 1042 -3.20000000 RUS2004N1040455
393 RUS 2004 m 34 815 0.80000000 RUS2004N1040516
399 RUS 2014 m 35 1275 -1.71391753 RUS2014N332
400 RUS 2014 f 35 1210 -1.71391753 RUS2014N332
401 RUS 2014 m 35 1230 -1.71391753 RUS2014N332
402 RUS 2014 m 35 1380 2.28608247 RUS2014N733
403 RUS 2014 m 35 1410 2.28608247 RUS2014N733
404 RUS 2014 f 35 1270 2.28608247 RUS2014N733
405 RUS 2014 f 35 1040 2.28608247 RUS2014N1102
406 RUS 2014 f 35 1080 2.28608247 RUS2014N1102
407 RUS 2005 f 35 1150 -0.90000000 RUS2005N1051024
408 RUS 2005 m 35 1390 -0.90000000 RUS2005N1051024
409 NL 2012 m 35 455 -7.13350126 NL2012E121
410 RUS 2014 m 35 1250 -3.71391753 RUS2014N397
411 NL 2005 m 35 990 1.30000000 NL2005N1050244
412 RUS 2004 f 35 1020 -2.20000000 RUS2004N1040437
413 RUS 2005 m 35 1200 -6.40000000 RUS2005N1050422
418 RUS 2004 m 36 1320 -1.80000000 RUS2004N1030455
419 RUS 2004 m 36 1270 -1.80000000 RUS2004N1030455
420 RUS 2004 m 36 1430 -1.80000000 RUS2004N1030455
421 NL 2012 f 36 460 -8.13350126 NL2012Wun5
422 NL 2012 m 36 505 -8.13350126 NL2012Wun5
424 RUS 2004 m 36 916 -3.20000000 RUS2004N1040600
425 RUS 2014 m 36 1300 1.28608247 RUS2014N1451
426 RUS 2005 m 36 1420 -1.40000000 RUS2005N1050054
427 RUS 2005 f 36 1170 -1.90000000 RUS2005N1050270
432 RUS 2013 f 37 1030 -4.78111588 RUS2013N407
433 RUS 2013 f 37 1050 -4.78111588 RUS2013N407
434 RUS 2013 m 37 1140 -4.78111588 RUS2013N407
439 RUS 2014 f 37 1250 0.28608247 RUS2014N422
440 RUS 2004 f 37 1120 -2.20000000 RUS2004N1040480
447 RUS 2014 m 38 1615 -4.71391753 RUS2014N351
448 RUS 2014 f 38 1430 -4.71391753 RUS2014N351
449 RUS 2014 f 38 1225 -0.71391753 RUS2014N386
450 RUS 2014 f 38 1315 -0.71391753 RUS2014N386
451 NL 2015 m 38 940 -2.93706294 NL2015E117
452 NL 2012 m 38 1090 -5.13350126 NL2012E130
453 RUS 2014 f 38 1225 -0.71391753 RUS2014N668
454 RUS 2014 m 38 1430 -0.71391753 RUS2014N493
461 NL 2012 f 39 535 -11.13350126 NL2012E31
462 NL 2012 f 39 195 -11.13350126 NL2012Wun1
463 RUS 2014 m 39 1440 -1.71391753 RUS2014N202
464 RUS 2014 m 39 1250 -1.71391753 RUS2014N202
465 NL 2015 m 39 600 2.06293706 NL2015W280
466 NL 2012 f 39 1090 16.86649874 NL2012E323
467 RUS 2014 f 39 1325 -1.71391753 RUS2014N266
468 NL 2004 m 39 1050 9.00000000 NL2004N1040299
475 RUS 2014 m 40 1480 -2.71391753 RUS2014N300
476 RUS 2014 m 40 1450 -2.71391753 RUS2014N300
477 RUS 2014 m 40 1475 -2.71391753 RUS2014N300
478 RUS 2014 f 40 1230 -2.71391753 RUS2014N656
479 RUS 2014 f 40 1300 -2.71391753 RUS2014N207
480 RUS 2014 m 40 1480 -2.71391753 RUS2014N207
481 RUS 2014 m 40 1310 -2.71391753 RUS2014N656
482 NL 2012 m 40 805 -8.13350126 NL2012W39
483 RUS 2014 m 40 1428 -2.71391753 RUS2014N273
491 RUS 2014 m 41 1110 -3.71391753 RUS2014N615
492 NL 2004 f 41 1000 7.00000000 NL2004N1040255
502 NL 2012 f 44 700 -6.13350126 NL2012W15
503 NL 2005 m 44 1160 -7.20000000 NL2005N1050197
504 NL 2005 m 44 1020 -7.20000000 NL2005N1050197
505 NL 2005 m 44 1240 -7.20000000 NL2005N1050197
506 NL 2005 f 44 1050 -7.20000000 NL2005N1050197
508 NL 2012 m 45 710 -7.13350126 NL2012W9
509 NL 2012 f 45 680 -7.13350126 NL2012W9
510 NL 2012 m 47 1100 -2.13350126 NL2012W154
511 NL 2015 m 47 630 -5.93706294 NL2015W202
512 NL 2012 m 47 1130 -2.13350126 NL2012E197
513 NL 2004 f 47 1210 1.00000000 NL2004N1040149
514 NL 2004 f 47 1250 1.00000000 NL2004N1040157
519 NL 2004 f 50 1320 -2.00000000 NL2004N1040129
520 NL 2004 m 50 1380 -2.00000000 NL2004N1040129
521 NL 2012 f 50 1060 -8.13350126 NL2012E111
522 NL 2004 m 50 1510 -2.00000000 NL2004N1040119
523 NL 2004 f 50 940 -1.50000000 NL2004N1040102
524 NL 2012 m 51 625 -3.13350126 NL2012W27
525 NL 2012 f 51 1325 -6.13350126 NL2012E28
527 NL 2012 f 52 1220 -1.13350126 NL2012E154
528 NL 2004 m 52 1160 -5.00000000 NL2004N1040134
529 NL 2012 m 53 1200 -2.13350126 NL2012W154
530 NL 2012 f 53 880 -5.13350126 NL2012Wun9
531 NL 2012 f 53 880 -5.13350126 NL2012Wun9
532 NL 2004 m 53 1290 -5.00000000 NL2004N1040106
533 NL 2004 m 53 1230 -5.00000000 NL2004N1040106
534 NL 2012 m 53 790 -5.13350126 NL2012Wun12
535 NL 2012 m 53 1130 -2.13350126 NL2012W57
536 NL 2012 f 53 1300 2.86649874 NL2012Wun24
537 NL 2004 m 53 1530 -5.00000000 NL2004N1040063
538 NL 2004 m 54 1450 -5.00000000 NL2004N1040054
539 NL 2004 m 54 1060 -5.00000000 NL2004N1040054
540 NL 2004 f 54 1110 -5.00000000 NL2004N1040054
541 NL 2012 f 54 695 -6.13350126 NL2012E131
542 NL 2012 m 54 935 -6.13350126 NL2012W115
543 NL 2012 f 54 640 -6.13350126 NL2012E131
544 NL 2012 f 54 830 -6.13350126 NL2012W115
545 NL 2004 m 54 1380 -5.00000000 NL2004N1040057
546 NL 2004 f 54 910 -5.00000000 NL2004N1041151
550 NL 2012 m 56 1325 -11.13350126 NL2012E45
551 NL 2012 m 56 900 -5.13350126 NL2012E32
552 NL 2012 f 56 1225 -5.13350126 NL2012E32
553 NL 2015 m 56 1510 0.06293706 NL2015E209
554 NL 2012 f 56 1280 -3.13350126 NL2012W203
555 NL 2012 f 57 1180 -4.13350126 NL2012E25
556 NL 2015 m 57 1180 -7.93706294 NL2015Wu
557 NL 2015 f 57 1080 -0.93706294 NL2015W348
559 NL 2015 f 58 980 -1.93706294 NL2015E93
560 NL 2015 f 58 1040 -1.93706294 NL2015Eu
562 NL 2015 f 59 1040 -2.93706294 NL2015W216
563 NL 2015 f 59 1030 -2.93706294 NL2015W216
564 NL 2015 f 59 1140 -2.93706294 NL2015W216
565 NL 2015 m 59 1440 -2.93706294 NL2015E66
566 NL 2012 f 59 1360 -8.13350126 NL2012W35
567 NL 2012 m 60 1070 -15.13350126 NL2012W49
569 NL 2012 m 62 1400 -11.13350126 NL2012E37")
bmgrp <- groupedData(bm ~ age | year, data = bmdata)
options(contrasts = c("contr.sum","contr.poly"))
contrasts(bmgrp$pop); contrasts(bmgrp$sex)
startvalues <- c(31.228925, 75.552237, 12.708210, 9.427097)
Some further search in the internet brought me to the following article: Zhou et al. (2007): Using a nonlinear crossed random effects model for describing circadian patterns of serum prolactin concentrations in heat stressed Holsteins
(http://newprairiepress.org/cgi/viewcontent.cgi?article=1113&context=agstatconference). They write the following about crossed random effects in nlme:
Fitting nonlinear mixed models with both crossed and nested random effects is still a new and challenging topic in statistics. […] The NLME package in R is very powerful for fitting multilevel nonlinear mixed-effects
models with nested random effects, but it does not fit nonlinear mixed-effects models
with crossed random effects (Pinheiro and Bates, 2000). Rasbash and Goldstein (1994)
show how to fit a linear mixed model with crossed random effects as a purely
hierarchical formulation of nested random effects. Zhou, Parkhurst, et al (2006) (http://newprairiepress.org/agstatconference/2006/proceedings/11/) developed a method to enable NLME in R to fit a nonlinear mixed-effects model with crossed random effects based on Rasbash and Goldstein’s idea for linear mixed-effects model. This method can be used to fit a nonlinear mixed effects model with arbitrary levels of crossed and nested random effects. However, only one correlation for the crossed random effects can be estimated. The NLME code for fitting the nonlinear mixed-effects model […] is documented in the Appendix.
# Feeding Event is specified as the lowest hierarchical level grouping factor
# (hierarchical level 1 grouping factor)
# Specify Pig as a standard hierarchical level 2 grouping factor
# Create a new grouping factor as the highest hierarchical level
# (hierarchical level 3 grouping factor) with one level spanning the entire data set
newGF <- factor(rep(1,length(heat$Y)))
# Specify that each level of Period has a coefficient random at newGF
# “ -1” indicates that the specific term factor(Period ) does not have an intercept
# It is not possible to estimate the correlation associated with Period.
full.nlme <-
nlme(model = Y ~ Y0 + KG * (exp(-KD * X) - exp(-KG * X))/(KG - KD),
fixed = Y0 + KG + KD ~ factor(Trt),
random = list(newGF = pdIdent(Y0 ~ factor(Period) -1),
newGF = pdIdent(KG ~ factor(Period) -1),
newGF = pdIdent(KD ~ factor(Period) -1),
Pig = pdDiag(Y0 + KG + KD ~ 1),
Event = pdDiag(Y0 + KG + KD ~ 1)),
start = c(39, 0, 0, 60, 0, 0, 20, 0, 0), data = heat)
I applied this syntax to my case - and it worked!
Here it is:
fmbm1 <- nlme(bm ~ SSlogis(age, Asym, xmid, scal), data = bmgrp,
fixed = Asym + xmid + scal ~ 1,
random = list(dummy = pdIdent(Asym ~ factor(year) - 1),
dummy = pdIdent(xmid ~ factor(year) - 1),
dummy = pdIdent(scal ~ factor(year) - 1),
brood = pdDiag(Asym + xmid + scal ~ 1)),
start = coef(fmbml), na.action = na.omit)
> summary(fmbm1)
Nonlinear mixed-effects model fit by maximum likelihood
Model: bm ~ SSlogis(age, Asym, xmid, scal)
Data: bmgrp
AIC BIC logLik
5505.905 5546.235 -2742.952
Random effects:
Formula: Asym ~ factor(year) - 1 | dummy
Structure: Multiple of an Identity
Asym.factor(year)2013 Asym.factor(year)2012 Asym.factor(year)2003 Asym.factor(year)2005 Asym.factor(year)2015 Asym.factor(year)2004
StdDev: 0.004011662 0.004011662 0.004011662 0.004011662 0.004011662 0.004011662
Asym.factor(year)2014
StdDev: 0.004011662
Formula: xmid ~ factor(year) - 1 | dummy %in% dummy
Structure: Multiple of an Identity
xmid.factor(year)2013 xmid.factor(year)2012 xmid.factor(year)2003 xmid.factor(year)2005 xmid.factor(year)2015 xmid.factor(year)2004
StdDev: 8.630687 8.630687 8.630687 8.630687 8.630687 8.630687
xmid.factor(year)2014
StdDev: 8.630687
Formula: scal ~ factor(year) - 1 | dummy %in% dummy %in% dummy
Structure: Multiple of an Identity
scal.factor(year)2013 scal.factor(year)2012 scal.factor(year)2003 scal.factor(year)2005 scal.factor(year)2015 scal.factor(year)2004
StdDev: 1.890416 1.890416 1.890416 1.890416 1.890416 1.890416
scal.factor(year)2014
StdDev: 1.890416
Formula: list(Asym ~ 1, xmid ~ 1, scal ~ 1)
Level: brood %in% dummy %in% dummy %in% dummy
Structure: Diagonal
Asym xmid scal Residual
StdDev: 2.176433e-05 5.079293 0.001926683 99.83194
Fixed effects: Asym + xmid + scal ~ 1
Value Std.Error DF t-value p-value
Asym.(Intercept) 1721.7518 123.05426 153 13.991810 0
xmid.(Intercept) 32.1919 3.83007 153 8.405047 0
scal.(Intercept) 11.5256 1.36451 153 8.446679 0
Correlation:
As.(I) xm.(I)
xmid.(Intercept) 0.495
scal.(Intercept) 0.734 0.369
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.20621393 -0.43418898 0.01076569 0.40318446 2.34353430
Number of Observations: 417
Number of Groups:
dummy dummy.1 %in% dummy dummy.2 %in% dummy.1 %in% dummy
1 1 1
brood %in% dummy.2 %in% dummy.1 %in% dummy
262
>