Skip to content
Snippets Groups Projects
Commit 89695f07 authored by Simon Ress's avatar Simon Ress
Browse files

Upload New File

parent d7a6633c
Branches main
No related tags found
No related merge requests found
#---------------#
#### Options ####
#---------------#
#Clear workingspace
rm(list = ls())
#load packages
if (!require("csv")) install.packages("csv") # usage of read.csv2()
library(csv)
if (!require("rstudioapi")) install.packages("rstudioapi") #usage of getActiveDocumentContext()
library(rstudioapi)
#Set Working Directory
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # setting working directory to source file location
#------------------------#
#### Loading datasets ####
#------------------------#
data <- read.csv2("OECD.ComparativeWelfare.data/OECD.ComparativeWelfare.data.csv")
#-----------------------------------------#
#### Overview / Descriptive Statistics ####
#-----------------------------------------#
#Overview (first six observations of the dataset)
head(data)
#Descriptive Statistics for all variables
if(!require("Hmisc")) install.packages("Hmisc")
library(Hmisc)
describe(data)
#Descriptive Statistics for all variables
if(!require("psych")) install.packages("psych")
library(psych)
describe(data)
describeBy(data, data$Country)
#Mean of variable
mean(data$leftmaj) # mean is "NA" because there are missing values on this variable
mean(data$leftmaj, na.rm = TRUE) # "na.rm = TRUE" removes the missing values in the calculation of the mean value
#Summary of the results of various model fitting functions
summary(data$leftmaj, na.rm = TRUE)
#Summary of partial data set (Only statistics for "Germany")
summary(data$leftmaj[data$Country=="Germany"], na.rm = TRUE)
#Graphical representation of the relationship between two variables
plot(data$leftmaj , data$Drug.use.disorders)
#-------------------------------#
#### Linear Regression Model ####
#-------------------------------#
model1 <- lm(data$Share.GDP.gov.expenditures.health ~ data$Years.lost) # error because the data type of the variable is not "numeric"
summary(model1) # doesn't work because of error above (-> Data type of one or more variables is not numeric)
#Check in type of data (TRUE: numeric / FALSE: not numeric)
for(i in 3:length(names(data))-2) {
print(names(data[i]))
print(is.numeric( eval(parse(text = paste("data$",names(data[i]), sep="" )))))
print("------------")
}
#saving the needed variables as numeric
data$Years.lost <- as.numeric(data$Years.lost)
data$Share.GDP.gov.expenditures.health <- as.numeric(data$Share.GDP.gov.expenditures.health)
#Now is the regression working
model1 <- lm(data$Share.GDP.gov.expenditures.health ~ data$Years.lost)
summary(model1)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment