Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
R-Studio-Binder
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Simon Ress
R-Studio-Binder
Commits
89695f07
Commit
89695f07
authored
4 years ago
by
Simon Ress
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
d7a6633c
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
SS19-Erklärung_unterschiedlicher_Gesundheitspolitiken_in_Europa/_Example__Linear_regression_models.r
+83
-0
83 additions, 0 deletions
...spolitiken_in_Europa/_Example__Linear_regression_models.r
with
83 additions
and
0 deletions
SS19-Erklärung_unterschiedlicher_Gesundheitspolitiken_in_Europa/_Example__Linear_regression_models.r
0 → 100644
+
83
−
0
View file @
89695f07
#---------------#
#### 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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment