Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Sunday, December 22, 2013

2.7 Performing a simple linear regression analysis with R

2.7 Performing a simple linear regression analysis with R

With a regression analysis you can, just like with a correlation, find the cohesion between different variables. With a regression analysis you get a formula with which you can predict future outcomes with some degree of reliability. With a regression analysis the interpretation is important.

Variables in a regression analysis

In this section the simple linear regression analysis is explained. This means that a variable gets predicted with the results of other variables. The predicted variable is called the dependent variable. The variables with which the dependent variable is predicted is called the independent variable.

Command

The command you have to use in R to perform a regression analysis is:
lm(*name of the dependent variable*~*independent variable*, *name of variable of the dataset*). You could use every numeric variable for a regression analysis. Variables vary in prediction power when serving as independent variable.

To be able to interpret the regression analysis, you have to give a name to the regression. You can do this by using the code <-. If you want to execute a regression analysis and want to interpret this one later, you use the command *name of the regression*<-lm(*name of the dependent variable*~*independent variable*,*name of variable of the dataset*).

In the example of Figure 23 the following command is used:
Regression1<-lm(SatisfactionCustomer~DistanceCustomer,Projects).

The estimated model

In the example of Figure 23 the satisfaction of the customer is estimated by looking at the distance between the company and the customer. R shows the following formula based on the regression analysis: SatisfactionCustomer = 2.524811 + (-0.003408x), where x stands for the number of kilometers. This formula is called the estimated model. By only putting in the name of the regression, the estimated model appears.

Figure 23: Performing a regression analysis with R
Figure 23: Performing a regression analysis with R

Executing the regression analysis

With the command summary(*name of the regression*) you get an overview of the regression. In this overview you have to look at the part underneath the text Coefficients. After the rows (Intercept) and DistanceCustomer (in this case look at AfstandKlant) the same values as in the estimated model.

In the example of Figure 23 the same values are shown as in the results of the regression analysis.

Significance

After the rows 3 stars (*) are presented. The stars indicate the level of significance. This indicates the importance and strength of the independent variable in predicting the dependent variable. An indication with one star is enough to conclude significance. The more stars after a row, the more significance the variables have. If there are now stars after a row, the conclusion is that the specific independent variable is not significant in predicting the dependent variable. This indicates that the variable has not a lot of prediction power in this regression model, and could be removed from the regression analysis. 

Prediction power of the regression analysis / Adjusted R-squared

In the overview of the regression analysis you find the text Adjusted R-squared followed by a value. This is the percentage presented in decimals of the results/outcomes that are correctly be predicted by the current regression model. An Adjusted R-squared of 1 means that 100% of the outcomes can be predicted by the current regression model. An Adjusted R-squared of 0,5 means that 50% of the predictions are correctly predicted by the current regression model.

It is up to the user to judge, based on the Adjusted R-squared value, if the regression model is good enough to give predictions. Generally, a Adjusted R-squared value of 0,5 (or higher) is quite an acceptable regression model to make predictions for.

In the example given in Figure 23 the Adjusted R-squared is 0.1041. This means that in 10,41% of the cases the prediction/regression model could predict the satisfaction of the customer correctly. Because this is quite a low value, the prediction model SatisfactionCustomer = 2.524811 + (-0.003409x) could be judged as a low level prediction model. 

New regression analysis

Figure 24 shows that another regression analysis is executed. In this case there is searched for a prediction model that could predict the turnover of a project by looking at the hours spent on a specific project. In this case the following command is used: Regression2<-lm(TurnoverProject~HoursProject,Projects).

By typing in the command Regression2, the following prediction model appears. Turnoverproject = 0 + 40x. In this case x is the number of hours that is spent in the project.

With the command summary(Regression2) the following information about the regression analysis appears (see Figure 24). This shows that only the variable HoursProject (UrenProject) is significant (the intercept could be left away since this has a value of 0).
The information also shows that the value of the Adjusted R-squared is 1. This means that the prediction model is 100% reliable. This is quite logical since the company asks $ 40,- per hour. The turnover is simply the sum of hours and fee per hour.

Figure 24: Another regression analysis in R
Figure 24: Another regression analysis in R





2.4 Modifying data in R

Modifying data in R

The R console has a simple tool called Data Editor to review and modify an important data file.

The data editor

The data editor opens by clicking on Edit in the window of the R console: Edit-> Data Editor. The window appears that asks about which dataframe and matrix to open. Here you type in the name of the matrix and confirming this by clicking OK. Figure 18 shows that the variable/matrix Projects is typed in for review.

Figure 18: Selecting a variable in the Data Editor of R
Figure 18: Selecting a variable in the Data Editor of R
After choosing a variable/matrix to modify, the data appears in the form of a spreadsheet. Here you can modify different cells. If you click at the top of the spreadsheet, the window Variable Editor appears. Here you can choose between to data types: Numeric (values of the variable should be interpreted as numbers) and Character (values of the variable should be interpreted as categories). Figure 19 shows that the variable Customer satisfaction (TevredenheidKlant) is interpreted as character. You can change this by selecting numeric.

Figure 19: Variable editor of R, interpreting values of variables as numeric or character
Figure 19: Variable editor of R, interpreting values of variables as numeric or character

2.3 Importing and making a matrix of a dataset

Importing and making a matrix of the dataset


Reviewing the previous steps

In the previous steps of this tutorial you learned how to set the working directory, how to convert to a .csv-file and how to check your dataset. By executing these steps you are ready to import the dataset into the R console.

Figure 17: Making a matrix of the imported dataset
Figure 17: Making a matrix of the imported dataset

Importing the dataset

In the example of Figure 17 the dataset Projects.csv is imported. This is executed by the code read.csv('Projects.csv').

Making a variable of the dataset

Making a importing and making a variable of the dataset could also be done by one command, in case of calling the variable of the dataset Projects use the following command: Projects<-read.csv('Projects.csv'). The dataset is now attached to the variable called Projects. 

Making a matrix of the dataset

To make a matrix of the dataset, use the command attach(*name of the variable*). 
In the example of Figure 17 a matrix is created for the variable Projects with the code attach(Projects). To give an overview of the matrix of the dataset you can make R present a summary by using the command summary(*name of the variable*). In the example a summary is presented of the dataset Projects.csv. with the variable Projects by using the command summary(Projects).

To the next step: Modifying data in R

Saturday, December 21, 2013

1.3 Importing the data set in the R Console

Importing data in R

Verify if the correct Working Directory is set. Also make sure that the data files are placed in this folder. 
If these two operations are executed, you are ready to import data files into the R console.

Importing the csv file


Figure 6: Imorting and making a matrix of the data set in the r console
Figure 6: Importing and making a matrix of the data file


You can select the file you wat to import into the R console. This can be executed in the following way:
  1. Importing the data file: Type in the command read.csv('*name of your file*'). In the example of Figure 6 the file Flowersales.csv is imported. In this example the code read.csv('Flowersales.csv') is used. After pressing Enter information about the data set appears in the R console. Please note that the code that is inserted in the R console is case sensitive, "flowersales.csv" could not be found. Please also not that the name of the file has to be put between 'quotation marks'.
  2. Creating a variable for the data file:  The next step is creating variables for the data file. In the introduction of this tutorial it is explained how to create a variable with the <- code. For the data file Flowersales.csv a variable is created. This is done by using the following code: Flowersales<-read.csv('Flowersales.csv'). The file Flowersales.csv is no longer seen as an external file, but as a variable in this session. This makes analyzing the data set in the R console way easier. The purpose of making a variable is that the code for importing the data file [read.csv('Bloemenverkoop.csv')] does not have to be insterted every time. Now that a variable is created for this data file, you only have to type in Flowersales to show information about the data file.
  3. Making a matrix of the data file: By making a matrix of the just created variable Flowersales, the R console could read the data in a better way. Making a matrix in the R console is easy by using the command: attach(*Name of the variable of the data file*). Figure 6 shows that the command attach(Bloemenverkoop) is used (Bloemenverkoop is the Dutch word for Flowersales). Now catagories of the data set could easy be shown in the R consol. For example by showing information about the catagory Tulips, you easily use the command Tulips. In the next chapter you will see that in front of the command Tulips differerent functions could be placed to analyze this catagory.

Remarks

  • Like chapter 1.1 explains about the Working Directory; The Working Directory is a map in which R searches for data files. So, the data files you want to analyze have to be placed in this map to be found by R.
  • It is not neccesary to name the variable Flowersales, you could decide yourself how you name the variable. With the <- code R recognized the data file. Flowers<-read.csv('Flowersales.csv') is for example acceptable as well. In this case the variable Flowers has to be used during the whole analyzing process in the R console.