Midterm part 2 question

Midterm part 2 question

by Benjamin Weia -
Number of replies: 12

Hi, I'm stuck on a question for part 2 e of the midterm:

// e. Recode the hivstatus variable so that 0=HIV Negative, 1=HIV Positive, and .=Excluded Record (missing). (2 pts)

When I try the following code

encode hivstatus, generate(hivstatus_categ)

label define hivstatus_def 0 "HIV Negative" 1 "HIV Positive" . "Excluded Record (missing)" // define labels

I get an error that ". cannot be labeled." From googling, it seems the system missing value of . cannot be labeled. If that is the case, how would we achieve the labeling requested by the question?



In reply to Benjamin Weia

Re: Midterm part 2 question

by Cozie Gwaikolo -
Hi Benjamin,
Great question.
For this one, you may need to take it one step at the time:

First, try to see what the current coding is for hivstatus, this will confirm what kind of variable hivstatus is (codebook could be useful here!).

Then encode just as you did in your first code. You may also need to confirm what the coding of your new variable is (hivstatus_categ) after encoding. Again using codebook.

Next, you will need to recode your new variable (hivstatus_categ) before labeling them as you you were trying to do in your second step. After recoding, be sure to try the two-step labelling method demonstrated in the lecture.

When you're done, please check to make sure the variable is coded and labelled are indeed coded and labelled to values you wanted. This is just to know if your coding and labeling worked.

I hope this is helpful.
Please let me know if the above needs further clarifications, or if you have additional questions.

Cozie
In reply to Cozie Gwaikolo

Re: Midterm part 2 question

by Mattheus Ramsis -
It seems that I am having trouble uploading the data,

This is the code I'm inputting the upload the data:

import delimited "M:\Biostat_212_2021_Wk3\Data\HIVvax_followup (2).xlsx"

I then receive thousands of lines that look like this:

"Note Unmatched quote while processing row 4421; this can be due to a
formatting problem in the file or because a quoted data element spans
multiple lines. You should carefully inspect your data after importing.
Consider using option bindquote(strict) if quoted data spans multiple
lines or option bindquote(nobind) if quotes are not used for binding data."

and

"Note: 2,462 binary zeros were ignored in the source file. The first instance
occurred on line 1. Binary zeros are not valid in text data. Inspect
your data carefully."


and when I codebook hivstatus, I receive the following "variable hivstatus not found"

Would appreciate your assistance in this.

Best,
Mattheus
In reply to Mattheus Ramsis

Re: Midterm part 2 question

by Crystal Langlais -
Hi Mattheus,

Thanks for posting. It looks like there are a few things going on.

Your code is attempting to import a .csv file whereas that data file is a .xlsx file. If you can't remember/find the code for importing .xlsx files, you can use the drop down menus and then copy the stata-generated code into your do-file.
The errors you are receiving are preventing you from properly importing the data so you are unable to run commands on variables. Once you successfully import your data, you will be able to use the "codebook hivstatus" command.

Another thing I noticed is the full file path on your import command. Be sure to set your working directory to the parent folder (for you, it looks to be "Biostatat_212_2021_Wk3"). Then you will need to use the relative file path (relative to your parent folder/working directory). This will allow us to run the full do-file on our machines by simply changing the working directory, as per the project description. To make this fully compatible with other machines, you will also want to be careful not to change the file names from those downloaded (it looks like you may have downloaded the datafile twice and thus your machine added the " (2)" to the file name)

Hope this helps.
Crystal
In reply to Crystal Langlais

Re: Midterm part 2 question

by Eleni Jaswa -
Related question about "relative file path" - can you give me an example of what that would look like / where I can read more to understand what that is (for Mac)?

For example, how do I change the import line below to be a relative file path?

cd "/Users/ejaswa/Dropbox/REI/ATCR Masters/Biostat 212/hivvax"

log using "/Users/ejaswa/Dropbox/REI/ATCR Masters/Biostat 212/hivvax/Output/Jaswa_baseline_cleaning.smcl", replace

import delimited "/Users/ejaswa/Dropbox/REI/ATCR Masters/Biostat 212/hivvax/Data/HIVvax_baseline.csv", clear
In reply to Eleni Jaswa

Re: Midterm part 2 question

by Crystal Langlais -
Hi Eleni,
Thanks for posting. Using relative paths is first covered in Lecture 2 (starts at slide 15).

Once you set your working directory, you use the relative file path to move within the working directory. In large group for lecture 2 I showed an example like this:
 
cd "/Users/crystal/Box/_Biostat212_2021/Week2/Assignment"
log using "Output/testlog.smcl", replace

First, I set the working directory. Within my working directory on my computer, I already created a file called "Output" (and "Data" and "DoFiles"; note, these names might not be the same as for your midterm). With the second line of code, I give Stata the relative path. Essentially, I tell Stata: go to the working directory, open the folder called "Output" and save the log there with the name "testlog.smcl".

Alternatively, I could have replaced the second line of code with

log using "/Users/crystal/Box/_Biostat212_2021/Week2/Assignment/Output/testlog.smcl"

but this would NOT be a relative file path.

Hope this helps!
crystal
In reply to Crystal Langlais

Re: Midterm part 2 question

by Eleni Jaswa -
awesome, this clarifies everything, thank you!
In reply to Cozie Gwaikolo

Re: Midterm part 2 question

by Amy Alagh -
I was able to get through to recode the hivstatus variable to another variable (hivstatusreal) with redefining the numeric values. the problem is now when try to do this after recoding the variable:
label define hivstatusreal . "Excluded record" 0 "HIV negative" 1 "HIV positive"

label values hivstatusreal hivstatus

I get an error message that says "label hivstatusreal already defined"

Not sure if anyone else is having that issue too :(
In reply to Amy Alagh

Re: Midterm part 2 question

by Crystal Langlais -
Hi Amy,
Thanks for posting. I see a couple of issues:

From your "label define" command, it looks like the value label you created is called "hivstatusreal" but when you assign the value label to the new variable (also called "hivstatusreal" per your description) in your "label values" command, you tell Stata to apply the label "hivstatus". Unless you've also created a value label called "hivstatus", this line of code will not do anything. 

You can not assign value labels to missing values that are stored as "." Lecture 4 covers how to label missing values, if that is necessary to address the question (some questions just require you to correctly code missing/unknown values to "." as opposed to labeling them). As such, the "label define" command you have may not work as expected.

You must have already created a label called "hivstatusreal", thus Stata tells you it already exits. To get around this, you can use either the modify or replace option, or you can drop the label and recreate it: "label drop hivstatusreal". Note: these approaches will also change the value labeling for any variable that you've already assigned the existing "hivstatusreal" value label to.

Hope this helps!
In reply to Cozie Gwaikolo

Re: Midterm part 2 question

by Melanie Molina -
I'm also having trouble with this step. I try:
encode hivstatus, generate(hivstatus_n)

then I try to recode:
Recode hivstatus_n (3=0) (2=1) (1=.) (miss=.)

and I get an error: "program recode already defined"

When the instructions say ".=Excluded Record (missing)", that means that they want us to combine the actual missing with the excluded records to make a total of 3 categories, correct?

Thanks!
In reply to Melanie Molina

Re: Midterm part 2 question

by Nizar Hakam -
Hi Melanie, if the labels are already defined, you would need to add ",modify" at the end of your recode command and this would hopefully resolve the issue.
In reply to Melanie Molina

Re: Midterm part 2 question

by Katherine Kentoffio -
I also have this same second question about part 2 as Melanie - for the hivstatus variable, there are values labeled as "Excluded Record' and also values that are just missing. Do you want us to combine both together into a single category labeled "."? Or do you only want the "Excluded Record" field relabeled "."? I did it one way and now I am not sure. Thank you!
In reply to Katherine Kentoffio

Re: Midterm part 2 question

by Crystal Langlais -
Hi Katherine,
All missing data should be coded the same way for this specific question.
As you noted, the variable already had missing data (coded as "."). So when you clean "Excluded Record" to also be missing, the total missing will be greater than just those that were originally labeled "Excluded Record".

hope this helps.