Instantly share code, notes, and snippets.

# AgEconomist/R basic statistics.r Created Feb 22, 2017

What would you like to do?
Basic Statistics in R
 # ------------------------------------------------------------------ # |PROGRAM NAME: R basic statistics # |DATE: 2/20/17 # |CREATED BY: MATT BOGARD # |PROJECT FILE: # |---------------------------------------------------------------- # | PURPOSE: BASIC STATISTICS IN R # |---------------------------------------------------------------- # create some toy data GARST <- c(150,140,145,137,141,145,149,153,157,161) PIO <- c(160,150,146,138,142,146,150,154,158,162) MYC <- c(137,148,151,139,143,120,115,136,130,129) DEK <- c(150,149,145,140,144,148,152,156,160,164) PLOT <- c(1,2,3,4,5,6,7,8,9,10) BT <- c('Y','Y','N','N','N','N','Y','N','Y','Y') RR <- c('Y','N','Y','N','N','N','N','Y','Y','N') yield_data <- data.frame(GARST,PIO,MYC,DEK,PLOT,BT,RR) # create GMO trait field yield_data\$GMO <- ifelse(yield_data\$BT == 'Y' & yield_data\$RR == 'Y','Stacked Trait', ifelse(yield_data\$RR == "Y" , 'Single Trait ', ifelse(yield_data\$BT =="Y",'Single Trait ', 'Non-GMO '))) #-------------------------------- # summary statistics #-------------------------------- summary(yield_data) #by processing by(yield_data, yield_data\$GMO, summary) #frequencies table(yield_data\$GMO) #---------------------------- # graphics #---------------------------- #histogram hist(yield_data\$GARST) #scatterplot plot(yield_data\$GARST,yield_data\$PIO) #----------------------------------- # t-tests #----------------------------------- # independent samples t-test t.test(yield_data\$GARST,yield_data\$PIO,var.equal=TRUE ) # difference in GARST vs PIO yields from yield_data file #Welch's 2 sample ttest t.test(yield_data\$GARST,yield_data\$PIO,var.equal=FALSE) # this is default # run as if paired ttest t.test(yield_data\$GARST,yield_data\$PIO,paired=TRUE) # not actually appropriate but notice the difference in the results from the previous t-test on # GARST and PIO #------------------------------- # correlations #------------------------------- cor(yield_data\$GARST,yield_data\$PIO, method="pearson") #------------------------------- # regression #------------------------------- summary(lm(GARST~PIO,data=yield_data))