简体   繁体   English

按年份在同一散点图上绘制两条回归线:X 轴日期 MM/DD

[英]Plot Two Regression Lines on Same Scatter Plot By Year: X-Axis Date MM/DD

I have a scatter plot of calls / time.我有一个电话/时间的散点图。 My x variable is the date (Day/Month) and my Y variable is a number of calls on each date.我的 x 变量是日期(日/月),我的 Y 变量是每个日期的调用次数。 I would like to plot two regression lines using PROC SGPLOT REG, one for 2019 and one for 2020. However, when I try to do this, all I get is a regular scatter plot with no regression lines.我想使用 PROC SGPLOT REG 绘制两条回归线,一条针对 2019 年,一条针对 2020 年。但是,当我尝试这样做时,我得到的只是一个没有回归线的常规散点图。 Here is my code:这是我的代码:

 proc sgplot data=intern.bothphase1;
 reg x=date y=count / group=Year;
 label count="Calls Per Day" year="Year";
 Title "Comparison of EMS Calls per Day 1/1 - 3/31 in 2019 vs. 
 2020";
 run;

The scatter plot comes up without issue (2019 and 2020 values in different colors) but I want to see how the trends differed between the two time periods, so I really want to get the regression lines on there.散点图没有问题(不同颜色的 2019 年和 2020 年值),但我想看看两个时间段之间的趋势有何不同,所以我真的想在那里得到回归线。 Can anyone help?任何人都可以帮忙吗?

I imagine this has to do with the fact that I concatenated my day and month with a / so it is a character variable and so SAS cannot calculate the regression.我想这与我将日期和月份与 / 连接起来的事实有关,因此它是一个字符变量,因此 SAS 无法计算回归。 I did this so I could use year as a class variable.我这样做是为了我可以使用 year 作为类变量。 I still have the original date variable in my table, is there a way I could get SAS to give me the month/day from that as a numeric variable?我的表中仍然有原始日期变量,有没有办法让 SAS 将月/日作为数字变量提供给我?

Thanks!谢谢!

EDIT: I used a date value in SAS and changed the format to mm/dd, but this doesn't help because the regression lines are just on either end of the graph rather than overlapping (picture attached).编辑:我在 SAS 中使用了日期值并将格式更改为 mm/dd,但这无济于事,因为回归线仅位于图形的两端而不是重叠(附图片)。 what I want is to have the regression lines overlap for the same time period 2019 vs. 2020 This is because SAS dates correspond to numbers from 1/1/1960.我想要的是让 2019 年与 2020 年同一时期的回归线重叠这是因为 SAS 日期对应于 1/1/1960 的数字。 What I want is the mm/dd to correspond to numbers 1-365 so I get two overlapping regression lines to show how the trends changed from one year to the next.我想要的是 mm/dd 对应于数字 1-365,所以我得到两条重叠的回归线来显示趋势从一年到下一年的变化。 Anyone know how I can do this?有谁知道我怎么能做到这一点?

So two steps here: first, you need to generate a "day" value that's 1-365... so let's just subtract out 01JAN from the day value.所以这里有两个步骤:首先,您需要生成一个 1-365 的“天”值……所以让我们从天值中减去 01JAN。

data have;
  do date = '01JAN2019'd to '31DEC2020'd;
    count = 25+2*rand('uniform');
    year = year(date);
    if month(date) le 3 then output;
  end;  
  format date date9.;
run;

data adjusted;
  set have;
  date_fixed = date - intnx('year',date,0,'b') + 1;  *current date minus jan 1 plus 1 (otherwise off by 1);
  format date_fixed date5.;                          *this does not actually affect the graph axis, oddly;
run;


 proc sgplot data=adjusted;
 reg x=date_fixed y=count / group=Year;
 xaxis valuesformat=date5.;                   *this seems to be needed for some reason;
 label count="Calls Per Day" year="Year";
 Title "Comparison of EMS Calls per Day 1/1 - 3/31 in 2019 vs. 
 2020";
 run;

Then we add the xaxis line because for some reason it won't obey the DATE5.然后我们添加xaxis线,因为出于某种原因它不会遵守DATE5. format (could also use MMDDYY5. as Reeza noted in comments, but we can force it to here.格式(也可以使用MMDDYY5.正如MMDDYY5.在评论中指出的那样,但我们可以将其强制到这里。

Here is what I get.这是我得到的。 You can use other axis options to further limit things, so for example 01APR doesn't show up.您可以使用其他轴选项来进一步限制事物,例如 01APR 不会显示。

回归图像显示覆盖散点图上的蓝线和红线,单轴显示 01JAN-01APR ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM