A short introduction to for Epidemiology Bendix Carstensen

A Short Introduction To For Epidemiology Bendix Carstensen-Free PDF

  • Date:13 Sep 2020
  • Views:1
  • Downloads:0
  • Pages:54
  • Size:543.70 KB

Share Pdf : A Short Introduction To For Epidemiology Bendix Carstensen

Download and Preview : A Short Introduction To For Epidemiology Bendix Carstensen


Report CopyRight/DMCA Form For : A Short Introduction To For Epidemiology Bendix Carstensen


Transcription:

1 Getting R running on your computer 1,1 1 What is R 1. 1 2 Getting R 1,1 2 1 Starting R 1,1 2 2 Quitting R 2. 1 3 Working with the script editor 2,1 3 1 Rstudio 2. 1 3 2 Try 3,1 4 Changing the looks 3,1 4 1 of standard R 3. 1 4 2 of Rstudio 3,1 5 Further reading 4,2 Some basic commands in R 5.
2 1 Preliminaries 5,2 2 Using R as a calculator 5,2 3 Objects and functions 6. 2 4 Sequences 7,2 5 The births data 7,2 6 Referencing parts of the data frame 8. 2 7 Summaries 9,2 8 Turning a variable into a factor 9. 2 9 Frequency tables 10,2 10 Grouping the values of a metric variable 10. 2 11 Tables of means and other things 11,2 11 1 Other tabulation functions 12.
2 12 Generating new variables 12,2 13 Logical variables 12. 3 Working with R 14,3 1 Saving the work space 14,3 2 Saving output in a file 14. 3 3 Saving R objects in a file 15,3 4 Using a text editor with R 15. 3 5 The search path 16,3 6 Attaching a data frame 16. 4 Graphs in R 18,4 1 Simple plot on the screen 18,4 2 Colours 19.
4 3 Adding to a plot 19,4 3 1 Using indexing for plot elements 20. 4 3 2 Generating colours 21,4 4 Interacting with a plot 21. 4 5 Saving your graphs for use in other documents 22. 4 6 The par command 22,5 The effx function for effects estimation 23. 5 1 The function effx 23,5 2 Factors on more than two levels 24. 5 3 Stratified effects 25,5 4 Controlling the effect of hyp for sex 25.
5 5 Numeric exposures 25,5 6 Checking on linearity 26. 5 7 Frequency data 26,6 Dates in R 27,7 Follow up data in the Epi package 29. 7 1 Timescales 29, 7 2 Splitting the follow up time along a timescale 30. 7 3 Cutting time at a specific date 34,7 4 Competing risks multiple types of events 36. 7 5 Multiple events of the same type recurrent events 37. References 40,8 R command sheet 41,Getting help 41.
Input and output 41,Data creation 42,Slicing and extracting data 42. Variable conversion 43,Variable information 43,Data selection and manipulation 43. Matrices 44,Advanced data processing 44,Strings 44. Dates and Times 45,Plotting 45,Low level plotting commands 46. Graphical parameters 47,Lattice Trellis graphics 48.
Optimization and model fitting 48,Statistics 48,Distributions 49. Programming 49,The Epi package 49,Getting R running on your computer. 1 1 What is R, R is free program for data analysis and graphics It contains all state of the art statistical. methods and has become the preferred analysis tool for most professional statisticians in. the world It can be used as simple calculator and as a very specialized statistical analysis. and reporting machinery, The special thing about R is that you enter commands from the keyboard into a console. window where you also see the results This is an advantage because you end up with a. script that you can use to reproduce your analyses a requirement in any scientific. The disadvantage is that you somehow have to find out what to type The practicals will. contain some hints and you will mostly be using R as a calculator as you just saw type. an expression hit the return key and you get the result. 1 2 Getting R, You can obtain R which is free from CRAN the Comprehensive R Archive Network at.
http cran r project org Under Download R for Windows click on install R for. the first time and then on Download R 3 0 2 for Windows which is a self extracting. installer This means that if you save it to your computer somewhere and click on it it will. install R for you, Apart from what you have downloaded there are several thousand add on packages to R. dealing with all sorts of problems from ecology to fiance and incidentally epidemiology. You must download these manually In this course we shall only need the Epi package. 1 2 1 Starting R, You start R by clicking on the icon that the installer has put on your desktop You should. edit the properties of this so that R starts in the folder that you have created on your. computer for this course, Once you have installed R start it and in the menu bar click on Packages Install. package s chose a mirror this is just a server where you can get the stuff and then the. Epi package, 2 1 3 Working with the script editor R for epidemiology. Once R hopefully has told you that it has been installed you can type. library Epi, to get access to the Epi package You can get an overview of the functions and datasets in.
the package by typing,library help Epi, It should be apparent that you have version 1 1 49 of the Epi package For documentauon. purposes it is often useful to have the following at the beginning of your program. sessionInfo,R version 3 1 0 2014 04 10,Platform i386 w64 mingw32 i386 32 bit. 1 LC COLLATE Danish Denmark 1252 LC CTYPE Danish Denmark 1252. 3 LC MONETARY Danish Denmark 1252 LC NUMERIC C,5 LC TIME Danish Denmark 1252. attached base packages, 1 utils datasets graphics grDevices stats methods base. other attached packages,1 Epi 1 1 65 foreign 0 8 61.
loaded via a namespace and not attached,1 tools 3 1 0. 1 2 2 Quitting R, Type q in the console and answer No when asked whether you want to save workspace. 1 3 Working with the script editor, If you click on File New script R will open a window for you which is a text editor very. much like Notepad, If you write a command in it you can transfer it to the R console and have it executed by. pressing CTRL r If nothing is highlighted the line where the cursor is will be transmitted. to the console and the cursor will move to the next line If a part of the screen is. highlighted the highlighted part will be transmitted to the console Highlighting can also. be used to transmit only a part of a line of code,1 3 1 Rstudio.
This is an interface that allows you to have a slithly more flexible script editor than the. built in R studio har syntax coloriung which can be very nice You can obtain it from. http rstudio com, Getting R running on your computer 1 4 Changing the looks 3. Now either open a script by File New script and type omit the in the beginning of. the line or fire up R studio and type in the editor window. N c 27 33 81, Run the lines one at a time by pressing CTRL r in R studio it is CTRL ENTER and see. what happens, You can also type the commands in the console directly But then you will not have a. record of what you have done Well you can press File Save History and save all you. typed in the console including the 73 6 commands with errors. 1 4 Changing the looks,1 4 1 of standard R, If you want R to start up with a different font different colors etc the go to the folder. where R is installed most likely Program Files R R 2 13 1 then to the folder etc. and open the file Rconsole with Notepad In the file are specifications on how R will look. when you start it pretty self explanatory except perhaps for MDI. MDI means Multiple Display Interface which means you get a single R window and. within that sub windows with the console the script editor graphs etc If this is set to. no you get SDI which means Single Display Interface which means that R will open. the console script editor etc in separate windows of their own. A withe background can be trying to look at so on my BxC computer I use a bold font. and the following colors,background gray5,normaltext yellow2.
usertext green,pagerbg gray5,pagertext yellow2,highlight red. dataeditbg gray5,dataedittext red,dataedituser yellow2. editorbg gray5,editortext lightblue, If you want to know which colors are available in R just give the command colors. 1 4 2 of Rstudio, Click on Tools Global options Apperance and choose Consolas font 16 pt Editor theme. 4 1 5 Further reading R for epidemiology,1 5 Further reading.
On the CRAN web site the last menu entry on the left is Contributed and will take you. to a very long list of various introductions to R including manuals in esoteric languages. such as Danish Finnish and Hungarian,Some basic commands in R. 2 1 Preliminaries, The purpose of these notes is to describe a small subset of the Rlanguage sufficient to. allow someone new to R to get started The exercises are important because they reinforce. basic aspects of R For further details about R we refer the reader to An Introduction to. R by W N Venables D M Smith and the R development team This can be downloaded. from the R website at http www r project org, To start R click on the R icon To change your working directory click on. File Change dir and select the directory you want to work in Alternatively you can. setwd c where alll my files are, To get out of R click on the File menu and select Exit or simpler just type q You will. be offered the chance to save the work space but at this stage just exit without saving. then start R again and change the working directory as before. R is case sensitive so that A is different from a Commands in R are generally separated. by a newline although a semi colon can also be used When using R it makes sense to. avoid as much typing as possible by recalling previous commands using the vertical arrow. key and editing them,2 2 Using R as a calculator, Typing 2 2 will return the answer 4 typing 2 3 will return the answer 8 2 to the power of.
3 typing log 10 will return the natural logarithm of 10 which is 2 3026 and typing. sqrt 25 will return the square root of 25, Instead of printing the result you can store it in an object say. which can be used in further calculations The expression pronounced gets is called. the assignment operator and is obtained by typing and then The assignment operator. can also be used in the opposite direction as in,6 2 3 Objects and functions R for epidemiology. The contents of a can be printed by typing a, Standard probability functions are readily available For example the probability below. 1 96 in a standard normal i e Gaussian distribution is obtained with. pnorm 1 96,pchisq 3 84 1, will return the probability below 3 84 in a 2 distribution on 1 degree of freedom and. pchisq 3 84 1 lower tail FALSE,will return the probability above 3 84.
Exercise 2 1,1 Calculate 32 42, 2 Find the probability above 4 3 in a chi squared distribution on 1 degree of. 2 3 Objects and functions, All commands in R are functions which act on objects One important kind of object is a. vector which is an ordered collections of numbers or an ordered collection of character. strings Examples of vectors are 4 6 1 2 2 which is a numeric vector with 4 components. and Charles Darwin Alfred Wallace which is a vector of character strings with 2. components The components of a vector must be of the same type numeric or character. The combine function c together with the assignment operator is used to create. vectors Thus,v c 4 6 1 2 2, creates a vector v with components 4 6 1 2 2 by first combining the 4 numbers 4 6 1 2 2. in order and then assigning the result to the vector v Collections of components of. different types are called lists and are created with the list function Thus. m list 4 6 name of company, creates a list with 3 components The main differences between the numbers 4 6 1 2 2. and the vector v is that along with v is stored information about what sort of object it is. and hence how it is printed and how it is combined with other objects Try. and you will see that R understands what to do in each case This may seem trivial but. remember that unlike most statistical packages there are many different kinds of object in. You can get a description of the structure of any object using the function str For. example str v shows that v is numeric with 4 components. Some basic commands in R 2 4 Sequences 7,2 4 Sequences.
It is not always necessary to type out all the components of a vector For example the. vector 15 20 25 85 can be created with,seq 15 85 by 5. and the vector 5 20 25 85 can be created with,c 5 seq 20 85 by 5. You can learn more about functions by typing followed by the function name For. example seq gives information about the syntax and usage of the function seq. Exercise 2 2,1 Create a vector w with components 1 1 2 2. 2 Print this vector to the screen,3 Obtain a description of w using str. 4 Create the vector w 1 and print it, 5 Create the vector 0 1 5 10 15 75 using c and seq.
2 5 The births data,Table 2 1 Variables in the births dataset. Variable Units or Coding Type Name,Subject number categorical id. Birth weight grams metric bweight,Birth weight 2500 g 1 yes 0 no categorical lowbw. Gestational age weeks metric gestwks, Gestational age 37 weeks 1 yes 0 no categorical preterm. Maternal age years metric matage, Maternal hypertension 1 hypertensive 0 normal categorical hyp.
Sex of baby 1 male 2 female categorical sex, The most important example of a vector in epidemiology is the data on a variable. recorded for a group of subjects To introduce R we use the births data which concern 500. mothers who had singleton births in a large London hospital These data are available as. an R object called births in the Epi package You can get them into your workspace by. library Epi,data births, 8 2 6 Referencing parts of the data frame R for epidemiology. to make sure that you have an object called births in your working directory A more. detailed overview of the objects in your workspace is obtained by. A short introduction to for Epidemiology June 2014 Version 4 Compiled Friday 27th June 2014 09 48 from C Bendix undervis SPE Intro R intro tex Michael Hills Retired

Related Books