Вы находитесь на странице: 1из 56

Clear screen : ctrl + L R can be used as a calc

Before R, there wa s S. S-Plus implements the S Language. ( this is expensive) The free version of S-Plus is R. We deal with sets of data. So we need to make a vector of values. R is case-sensitive.

c= column We could also do

Length: Useful to find out how much data I have We can do arithmetic with vectors. x+y

Perform x 3

List all variable

Remove a variable from a workspace

HELp functions ?ls()

Type help() for general help/

Mean|

Rnorm(100)

[1] indicates the first element, [6] indicates the 6th element and so on. Mean(y) If you have multiple commands on one line, separate by semi-colns

Std dev and variance

Pearson correlation

Enter commands in the script file and see the outpyut in the R window.

Make sure the cursor in the ls line. Do ctrl+R.

The R windows displays

You can select multiple lines and do Ctrl+R

Output

Periodically keep saving the script file.

T-tests Comments start with a #

Conclusion: Failed to reject Null Hypothesis. You can believe that the sample came from a population of mu=60.

If you see the + sign in R console, either give )(missing bracket) or press ESC.

Up arrow will repeat your commands. With mu=30.

Conclusion:Reject Null Hypothesis.

Paired Sample t.test (x,y,paired=T)

Independent Sample T-test t.test(x,y)

Mean is close to 0 Std deviation is 1. You may not always want this.
Data=rnorm(n, mean = 50, sd = 10)

Verify that the mean is close to 50 from histogram.

Scatter Plots

Rm(list=ls()) Remove all variables using this command

d1 is a data frame.

Corelation using frame (without attach)

only age column, d1$age

rm(age,sal) to remove age and sal variables.

Using attach

dont use it too often. There could be two data frames with the same column name. So in that case, it will pick the column in the last attached frame. Similarly detach

correlation for data in frame d1.

Well put data in excel and save it as csv files. first change directory:

change it to the directory where you created the csv file. use getwd() to confirm your working directory

you can also set wd from the command line

To list all the files in this folder.

read.csv("Rdata_import",header=T)

Save it in a variable

you can use the tab feature to autocomplete. like you type my..and press tab, it automatically fills up mydata. and so on..

import Record2.csv

The file is too big. So I just want to see the column names. Do names(rec). I want to see the first few rows. head(rec): default shows 6 rows. head(rec,10): show me the first 10 rows tail(rec)

test the hypothesis that the mean sales = 180

Conclusion: Reject the null hypothesis. Class2 1 patient-satis.csv a) Plot histogram of age,severity,anxiety b) Find the mean,sd of these 3 variables Use par(mfrow=c(3,1)) to define a window of 3 rows and 1 column

How to edit data in R xyz=data.frame() xyz=edit(xyz)

Data types of R Two types: Numeric and Character

Specify that by clicking on the column headers

After entering data, click close. It automatically saves it. Then type the variable name to view it

Linear Regression Model using R first make a model

Data above. Model is mod = lm( y ~ x1+x2) here x1,x2 are predictors.

summary(mod) would give us the output:

everything in R is treated as an object. names(mod)

Now if want to access say residuals, I use mod$residuals

Plot a histogram of the residuals

Exercise: Build a linear regression model for the Record2.csv data. Use the . notation to keep things simple.

music.mod = lm(sales~adverts+airplay+attract, data=music) Basically we are saying that this is a model object for our music dataset. data=music is used to save typing. use the .mod notation to keep it easy for bookkeeping. Plot the residuals

Now plot the entire information in the model par(mfrow=c(2,2)) plot(music.mod)

Work on house-rents.csv Predict the rental rate using these predictors

Now we find that the vac_rate isnt a good predictor. So we run the model without that.

Make predictions now use predict(name of the model, dataframe which contains the new data) M

Make multiple predictions using frames

Work on forest.csv Predict popdens 400 500 600 700 cropch 30 35 35 5 pasturech 10 12 15 5

Install Packages install foreign package which allows you to import excel files.

Вам также может понравиться