Skip to content

Instantly share code, notes, and snippets.

View MartinMacharia's full-sized avatar

MartinMacharia

View GitHub Profile
@MartinMacharia
MartinMacharia / IRIS dataset
Created April 28, 2015 10:38
Plotting using R base packages
### Title: Back to basics: High quality plots using base R graphics
### An interactive tutorial for the Davis R Users Group meeting on April 24, 2015
###
### Date created: 20150418
### Last updated: 20150423
###
### Author: Michael Koontz
### Email: [email protected]
### Twitter: @michaeljkoontz
###
@MartinMacharia
MartinMacharia / Extracting climate covariates
Last active October 26, 2020 13:10
Matching AfSIS spatial layers to OFRA legacy data
#Rgdal is what allows R to understand the structure of shapefiles by providing functions to read and
#convert spatial data into easy to work with R dataframes.
#Sp enables transformations and projections of the data and provides
#functions for working with the loaded spatial polygon objects.
#R to use .shp files.
# Some of these layers are in geographic coordinates;
#+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
#others in laea; +proj=laea +lat_0=5 +lon_0=20 +x_0=0 +y_0=0 +datum=WGS84
#+units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
#The two coordinate referencing systems have to be harmonised i.e all set as laea for example
@MartinMacharia
MartinMacharia / Agricolae package for randomization
Last active October 26, 2020 13:10
Generation of field book and field layouts
require(agricolae)
treatments=c("0N0P0K","30N0P0K","60N0POK","90N0P0K","120N0P0K","0N15P0K","30N15P0K","60N15P0K","90N15P0K","120N15P0K","90N7.5P0K","90N22.5P0K","90N15P10K","90N15P20K","90N15P30K","Diagnostic")=c("0N0P0K","30N0P0K","60N0POK","90N0P0K","120N0P0K","0N15P0K","30N15P0K","60N15P0K","90N15P0K","120N15P0K","90N7.5P0K","90N22.5P0K","90N15P10K","90N15P20K","90N15P30K","Diagnostic")
treatments
Fieldbook=design.rcbd(treatments,3,serie=2, 34589,"Super-Duper", first=TRUE)
Fieldbook
outdesign=design.rcbd(treatments,3,serie=2, 34589,"Super-Duper", first=TRUE)
outdesign
book=outdesign$book
@MartinMacharia
MartinMacharia / ANOVA, RegTrees
Last active October 1, 2015 10:07
ANOVA, Addditive& Interactive effects
x2=c(4,25,13,7,56,90,2,82)
x2=array(x2,c(2,4))
x2
tab=LTT.Kenya_Exe
tab
tab2=tab[1:464, 1:7]
tab2
names(tab2)
tab3=subset(tab,Site=="Bon" & "Nin"=="N0", select=c(Site,Var,Fert,Nin,Yield))
write.csv(tab, "LLT_BonN0")
@MartinMacharia
MartinMacharia / Kenya legacy exploration
Last active December 23, 2015 10:50
Kenya Legacy analysis
Kenya <- read.csv("C:/Users/machariam/Desktop/Spatial_layers/Kenya.csv", na.strings=c("NA"))
names(Kenya)
#create a new dataset with less variable of interst
str(Kenya)
Kenya_1=Kenya[ , c("Country","District","Center","Site","Longitudes","Latitudes","Elevation","Year","Crop_1","Variety_1",
"Crop_2","Crop_2_Variety","Crop_3" ,"Crop_3_Variety","PlantMon","PrevCrop","AppliMethod",
"Manure","Inorganic", "Crop_Fallow","pH_water","PH_Other","Soil_type","Nrate","Prate","Krate",
"Srate","Znrate","Curate","MgRate","Brate","Other_Nutrients","FYM","Compost","Grain_Yield",
"Stover_yield", "Additional_comments")]
@MartinMacharia
MartinMacharia / OFRA
Last active October 31, 2017 05:02
OFRA Legacy Data, Kenya
#1988
#Import data
OlNgarua=read.csv("C:/Users/machariam/Desktop/Legacy_data_Analysis/OlNgarua.csv")
OlNgarua
names(OlNgarua)
str(OlNgarua)
OlNgarua_1=OlNgarua[ ,c("District","Center","Longitudes","Latitudes","Year","Crop",
"Variety","PlantMon","AppliMethod","Nrate","Prate","Grain_Yield")]
summary(OlNgarua_1)
#Here is a document.
A single # represents a tittle, 2 represents a subtittle)
`Back quotes (``)are use to represent the inside text in a type writter format`
*The star signs between a text italicize the text*
**Double stars make the text bold**
* **Bulleting items, star space then item. This will not only bullet but also make the bulleted text bold**
@MartinMacharia
MartinMacharia / Mapping data in R
Created July 20, 2015 14:54
Mapping data in R using on-line data
# load the required libraries
library(sp)
library(rgdal)
# First load the data from the Washington department of ecology website
# Data source
#
# Data: ftp://www.ecy.wa.gov/gis_a/inlandWaters/wria.zip
# Markus Gesmann
library(arm) # for 'display' function only
icecream <- data.frame(
# http://www.statcrunch.com/5.0/viewreport.php?reportid=34965&groupid=1848
temp=c(11.9, 14.2, 15.2, 16.4, 17.2, 18.1,
18.5, 19.4, 22.1, 22.6, 23.4, 25.1),
units=c(185L, 215L, 332L, 325L, 408L, 421L,
406L, 412L, 522L, 445L, 544L, 614L)
)
basicPlot <- function(...){
@MartinMacharia
MartinMacharia / Evaluating Logistic Regression Models in R
Created September 1, 2015 12:50
Evaluating Logistic Regression Models in R
This post provides an overview of performing diagnostic and performance evaluation on logistic regression models in R. After training a statistical model, it’s important to understand how well that model did in regards to it’s accuracy and predictive power. The following content will provide the background and theory to ensure that the right technique are being utilized for evaluating logistic regression models in R.
Logistic Regression Example
We will use the GermanCredit dataset in the caret package for this example. It contains 62 characteristics and 1000 observations, with a target variable (Class) that is allready defined. The response variable is coded 0 for bad consumer and 1 for good. It’s always recommended that one looks at the coding of the response variable to ensure that it’s a factor variable that’s coded accurately with a 0/1 scheme or two factor levels in the right order. The first step is to partition the data into training and testing sets.
library(caret)
data(GermanCredit)
Train <- create