Title: Stata regression with conditions on dummies and variable values
Tags: regression;stata;dummy-variable;multiple-conditions;two-way
Question: I'm trying to create a regression that would include a polynomial (let's say 2nd order) of ```year``` on a certain interval of ```year``` (say 1 to 70) and a number of dummies for certain values of ```year``` (say for every ```year``` between 45 and 60).

If I didn't have the restriction for dummies, I believe the commands would be:

```gen year2=year^2
regress y year year2 i.year if inrange(year,1,70)
```

I can't make the dummies manually, there will be more than 15 of them in the end). Could anybody help me, please?

If I then want to plot the estimated function without the dummies, why do these two bring different things?

```twoway function _b[_cons] +_b[year]*x + _b[year2]*x^2, range(1 70)

twoway function _b[_cons] +_b[year]*year + _b[year2]*year^2, range(1 70)
```

The way I understood it, ```_b[_cons]```, ```_b[year]``` and ```_b[year2]``` call previously calculated coefficients for the corresponding independent variables and then multiplies it with them. Why does it bring different results then if ```x``` should be the same thing as ```year``` in this case?

Comment: I assumed both my questions arised from my unfamiliarity with Stata and may be easy to answer by people who have some experience with it. As such, could you please tell me which important features is it missing? Claiming it is a duplicate to my previous question suggests you haven't even read it.

Comment: You need to act on the advice given in your previous question please.

Comment: Possible duplicate of [how to make a flexible polynomial regression in Stata?](https://stackoverflow.com/questions/50213871/how-to-make-a-flexible-polynomial-regression-in-stata)

Comment: `twoway function` is unusual. It always plots in terms of a generic x-axis variable which it calls `x` and which is used regardless of whether any variable in the data is called (or abbreviates to) `x`. Referring to `x` in the syntax is needed for the command to make sense in most cases. It's not illegal not to mention `x` as for example `twoway function 2` has to be legal to show a horizontal line value 2 over the range 0 to 1, but specifying in terms of some other variable usually produces nonsense, or not what you want.

Comment: I've edited it out personal comments and padding, which don't help your question. For example, there is no queue you can jump by being under time pressure, as if no one else was! Claims of urgency are usually counter-productive.

Comment: Please don't snap at the people who are willing to help you. @PearlySpencer clearly did read your previous question and made fair comment on it. It's not a good question for the reasons given.


Here is another answer: I am not sure why Pearly is giving you such a hard time, I think this may be what you're looking for, but let me know if it is something different:

One thing to note, I am using a dataset that comes preloaded with Stata and this is usually a nice way to make a MVCE like Nick was saying in your other post.

```clear
sysuse gnp96
/* variables: gnp, date (quarterly) */
gen year = year(dofq(date)) // get yearly variable
gen year2=year^2 // get the square of the yearly variable
tab year if inrange(year,1970,1975), gen(yr) // generate dummy variables
// the dummy varibales generated have null values for years not 
// in the specified range, so we're going to fill those in
foreach v of varlist yr* {
    replace `v' = 0 if `v' == .
}

// here's your regression
regress gnp year year2 yr* if inrange(year,1967,1990)
```

Now, the yr* are your dummy variables and the * is a wildcard calling all variables named like yr[something]

This gives you the range for the dummy variables and the range for the year variables.

As to your question on using x vs year, I am only hypothesizing, but I think that when you use x it is continuous since Stata isn't looking at your variables, but instead just at the x axis whereas your year variable is discrete (a bunch of integers) so it looks more like a step function. More information can be found using the command ```help twoway function```

Comment for this answer: Sure it is okay to be a beginner. I was one too. And all volunteers on here, i am certain they want to help. But they cannot do so effectively and on  a regular basis if they have to guess what each question is asking, re-write the OPs' questions for clarity, provide a MVCE and so on.
Comment for this answer: On the first and last paragraphs, please see my comments on the main question.
Comment for this answer: I read that blog post too, and much of the commentary on it as well. FWIW, I don't think that has anything to do with a difficult and time-consuming question being too difficult and time-consuming to be worth my detailed effort. SO is not a help-line in which every question is entitled to an answer. We can choose what to ignore! Also, I put effort here not just into answering questions but also in trying to be clear on why it is difficult or even impossible to answer a question. Any implication that I should be infinitely obliging is not one that I can achieve.
Comment for this answer: @NickCox https://stackoverflow.blog/2018/04/26/stack-overflow-isnt-very-welcoming-its-time-for-that-to-change/
it's okay to be a beginner here, I couldn't answer the linked question, but this one was completely different and understandable even if it was not formatted wonderfully

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:05/08/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<regression><stata><dummy-variable><multiple-conditions><two-way>', 'Title': 'Stata regression with conditions on dummies and variable values', 'ViewCount': 783, 'id': 50238395, 'user_AboutMe': None, 'user_AccountId': 13515595.0, 'user_DisplayName': 'xgolt01', 'user_Reputation': 1.0, 'user_Views': 21.0, 'user_id': 9750270.0}}