Posts

Script 5

#Exercise 5.1 #1 define improve improve <- (misdata$mis_arith1 - misdata$mis_arith0) misdata$improve <- improve #2. scatterplot plot(improve ~ misdata$mis_IQ) # relationship is linear, higher IQ score predicts larger improvement #3. ??lm #4. model1 <- lm(improve ~ misdata$mis_IQ); model1 #5.ask summary summary(model1) # IQ is a significant predictor of improvement on arithmetic test score #6. ??abline abline(model1) #7. model2 <- lm(improve~misdata$mis_IQ, na.action=na.exclude) ??na.exclude summary(model2) #there is no difference in the two models, hence na.exclude is the default #8. fittie <- fitted(model1); fittie fittie2<- fitted(model2); fittie2 resid <- resid(model1); resid resid2<-resid(model2); resid2 #model 1 does not include NAs anymore, they were deleted. model 2 does include NAs #so the function na.exclude just excludes the NAs from analysis but leaves them in #9. qqnorm(resid); qqline(resid) # residuals seem light taile...

Script 4

#4.1.1 Load datafile load("misdata.Rdata") #4.1.2 Frame null and alternative hypotheses # H0: mean difference sores (Arith1-Arith0) do not significantly differ for method A and B # Ha: mean difference score (Arith1-Arith0) do significantly differ for method A and B boxplot(misdata$mis_arith0 ~ misdata$mydata.methodf) arith0_A <-misdata$mis_arith0[misdata$mydata.method==0] arith0_B <-misdata$mis_arith0[misdata$mydata.method==1] arith0_A; arith0_B par(mfrow=c(2,1)) hist(arith0_A);hist(arith0_B) par(mfrow=c(1,1)) hist(arith0_A);hist(arith0_B) #4.2 Compare method A and B visually on Arith0 and IQ #4.2.1 Arith 0 par(mfrow=c(2,1)) hist(misdata$mis_arith0[misdata$mydata.method==0]);hist(misdata$mis_arith0[misdata$mydata.method==1]) #method A seems negatively skewed; method B seems more equally distributed but has less low values than A boxplot(misdata$mis_arith0 ~ misdata$mydata.methodf) # method A has more variance/outliers than method B #4.2.2 IQ par(...

Script 3

#Exercise load("misdata.Rdata") names(misdata) misdata$educ <- NULL; misdata$educ misdata2 <- misdata[,-c(2:4,7,8)] names(misdata2) save(misdata2, file="misdata2.Rdata") table(misdata$mydata.sex) #Exercise 3.1 #1. table(misdata$mis_educf) # missing values are not counted #2. Create table of gender x educf table(misdata$mydata.sexf, misdata$mis_educf) table(misdata$mydata.sexf, misdata$mydata.methodf) #3. proportions instead of frequencies prop.table(table(misdata$mydata.sexf, misdata$mis_educf),1) prop.table(table(misdata$mydata.sexf, misdata$mis_educf),2) prop.table(table(misdata$mydata.sexf, misdata$mis_educf)) #if a number is given behind a comma, the values will be a proportion of that number #if no number is given, the values will be a proportion of the sum of all values in the table #4. bonus prop table prop.table(table(misdata$mydata.sexf, misdata$mis_educf, exclude=NULL)) #the NAs are treated as a different category alltogeth...

Script 2

# Exercise #.1 Give each 10th element of mis_arith1 NA mis_arith1 <- arith1 mis_arith1[seq(1,150,10)] <- NA #.2 Add NA to every 17th variable of mis_educ and mis_educf mis_educ <- educ mis_educ[seq(1,150,17)] <- NA mis_educf <- educf mis_educf[seq(1,150,17)] <- NA #.3 Add NA to mis_IQ < 85 mis_IQ <- IQ mis_IQ <- ifelse(mis_IQ < 85, NA, IQ) #4. Add NA to mis_arith0 if arith0 > 140 mis_arith0 <- ifelse(mis_arith0>140, NA, mis_arith0) ??ifelse #5. Make indicators for missing elements ind_educ <- as.numeric(is.na(mis_educ)) ind_IQ <- as.numeric(is.na(mis_IQ)) ind_arith0 <- as.numeric(is.na(mis_arith0)) ind_arith1 <- as.numeric(is.na(mis_arith1)) #6. Make data frame misdata <- data.frame(ID, method, methodf, sex, sexf, mis_IQ, mis_arith0, mis_arith1, mis_educ, mis_educf, ind_educ, ind_IQ, ind_arith0, ind_arith1) #7. Give overviews and compare summary(misdata) summary(mydata) #8. Make complete dataframe wit...

Script 1

#1. Set random seed to a fixed number set.seed(631) #2. Create IDs ID <- c(1:150) #3. Random assignment of instruction method method <- rbinom(150,1,0.5) methodf <- factor(method,labels=c("A", "B")) #4. Sex of the child sex <-rbinom(150,1,0.4) sexf <-factor(sex,labels=c("girl", "boy")) #5. Take random sample library(MASS) mu <- c(100,130) sigma <- matrix(c(64,40,40,100),2,2) X <- mvrnorm(150,mu,sigma) X <- round(X,digits=0) colnames(X) <- c("IQ","arith0") IQ <- cbind(X[,1]) arith0 <- cbind(X[,2]) arith0 <- round(arith0) #6. Generate normally distributed variables e1 <- rnorm(150,0,3) e2 <- rnorm(150,0,3) e3 <- rnorm(150,0,3) #7. Compute the arithmetic score after one year arith1 = round(0.8*method + 0.3*(arith0 + e1) + 1.5*(IQ + e2) + 0.6*sex + e3, 0) #8. Parental education Xeduc <- rmultinom(150, 1, c(0.3, 0.5, 0.2)) Xeduct <-t(Xeduc) v...
#Contents: #General Commands [ST1] #Make Vector [ST2] #Matrix Algebra [ST3] #Creating Random Variables [ST4] #Converting variable types [ST5] #Getting information from variables [ST6] #Calculating with variables [ST7] #Missing Values [ST8] #Dataframe Commands [ST9] #Graphical Options [ST10] #Descriptive & Exploratory Analyses [ST11] #Statistical Analyses [ST12] #Assumptions [ST13] #MANOVA / Repeated Measures (Probably not needed) [ST14] ############################## #General Commands [ST1] getwd() #Shows current working directory ?keyword #Get help for keyword, a specific topic ??keyword #More general search, looking for anything keyword is found in setwd("X:/R") #Set working directory data() #Check available datasets library() #Check library library(XXX) #Load package XXX install.packages("xxx") #Installs package xxx ls() #List of all variables currently loaded in memory rm(list=ls(all=TRUE)) #Removes all variables from memory s...