페이지

레이블이 Computer Programming인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Computer Programming인 게시물을 표시합니다. 모든 게시물 표시

5/12/2016

ANOVA Test in python code and interpretation - 2

ANOVA Test & Post hoc Test - Tukey HSD
# =============================================================
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.stats.multicomp as multi
data = pd.read_csv(‘nesarc_pds.csv’, low_memory=False)
#setting variables you will be working with to numeric
data['S1Q10A’] = pd.to_numeric(data['S1Q10A’], errors='coerce’)
data['S3AQ3B1’] = pd.to_numeric(data['S3AQ3B1’], errors='coerce’)
data['S3AQ3C1’] = pd.to_numeric(data['S3AQ3C1’], errors='coerce’)
data['CHECK321’] = pd.to_numeric(data['CHECK321’], errors='coerce’)
#subset data to those people who have income and have smoked in the past 12 months
sub=data[ (data['S1Q10B’]!=0) & (data['CHECK321’]==1)]
sub.loc[:,'S3AQ3B1’]=sub['S3AQ3B1’].replace(9, np.nan)
sub.loc[:,'S3AQ3C1’]=sub['S3AQ3C1’].replace(99, np.nan)
#recoding number of days smoked in the past month
recode1 = {1: 30, 2: 22, 3: 14, 4: 5, 5: 2.5, 6: 1}
sub['USFREQMO’]= sub.loc[:,'S3AQ3B1’].map(recode1)
# mean value of personal income in last 12 month
avgIncome = sub.loc[:,'S1Q10A’].mean(0)
LvIncome = [0, sub.loc[:,'S1Q10A’].quantile(0.3), sub.loc[:,'S1Q10A’].quantile(0.7), sub.loc[:,'S1Q10A’].max()]
# Split into 3 groups: 'low’, 'medium’, 'high’
splitIntoCat = pd.cut(sub.S1Q10A, LvIncome, labels=['low’, 'medium’, 'high’])
sub['INCOMECAT’] = splitIntoCat
# num of ciga per month
sub['NUMCIGMO_EST’]=sub['USFREQMO’] * sub['S3AQ3C1’]
sub1 = sub[['NUMCIGMO_EST’, 'INCOMECAT’]].dropna()
# using ols function for calculating the F-statistic and associated p value
model = smf.ols(formula='NUMCIGMO_EST ~ C(INCOMECAT)’, data=sub1)
results = model.fit()
print (results.summary())
print ('means for numcigmo_est by personal income’)
print(sub1.groupby('INCOMECAT’).mean())
print ('standard deviations for numcigmo_est by personal income’)
print(sub1.groupby('INCOMECAT’).std())
# ============ Post hoc test, Tukey HSD ===========================
mc1 = multi.MultiComparison(sub1['NUMCIGMO_EST’], sub1['INCOMECAT’])
res1 = mc1.tukeyhsd()
print(res1.summary())

ANOVA Test in python code and interpretation - 1

 I’d like to know the association btw personal income level(explanatory) and smoking amount(quantitative response). I thought there’s a certain relationship. From my experience, I’ve quit smoking since new year 2016 for raising cigarette prices surprisingly. However, I wouldn’t quit the smokes if I’ve got more paid. 
 From NESARC(’S1Q10A’), I split into 3 levels as ‘low’, 'medium’, 'high’ according to the level of personal income to make it categorical. The 'low’ level was set up to 30%, 'medium’ level was up to 70% and more than 70%(30% top of personal income) are categorized as 'high’.
 ANOVA results revealed that there’re no significant relationship btw personal income and number of cigarettes smoked, F(2, 9345)=1.130 and P-value=0.323. And the belows are means and std for no of cigarettes smoked by personal income level.
means for numcigmo_est by personal income
INCOMECAT        NUMCIGMO_EST
low                       424.175214
medium               413.702265
high                     423.927979
standard deviations for numcigmo_est by personal income
INCOMECAT       NUMCIGMO_EST           
low                      342.501714
medium              311.897224
high                    335.876155
 There’re no significant difference each others and here are the results of post hoc test. Especially, ‘meandiff’ value btw 'high’ and 'low’ is very small. Of course, there’re all 'False’ results in reject columns when the significant level(alpha) is set to 0.05.
Multiple Comparison of Means - Tukey HSD,FWER=0.05
==============================================
group1 group2 meandiff  lower    upper  reject
———————————————-
high   low    0.2472  -20.5833 21.0778 False
high  medium -10.2257 -29.6021  9.1507 False
low   medium -10.4729 -29.5268  8.5809 False
———————————————-
 If someone ask me whether this result are credible or not, I would say 'No’. There might be lots of uncontrolled fators, for one simple example, the different ciga. cost by State.

12/12/2015

Led dimmer by using Potentiometer



Here's my codes,

const int LedPin = 3;
const int PotPin = 0;
static int val=0;

void setup() {
  pinMode(LedPin, OUTPUT);
}

void loop() {
  val = analogRead(PotPin);
  LedTune();

  analogWrite(LedPin, val);
}

void LedTune(){
  val = map(val, 0, 1023, 0, 255);
  val = constrain(val, 0, 255);
}

11/29/2015

Photoresistor, Arduino circuit.



Changing the light intensity of an LED by using a photo resistor.

And here's my codes,


const int PhotoPinNo = 0;
const int LedPinNo = 3;
int val;

void setup() {
  pinMode(LedPinNo, OUTPUT);
}

void loop()
{
  val = analogRead(PhotoPinNo);

  BrightTune();

  analogWrite(LedPinNo, val);
}

void BrightTune()
{
  val = map(val, 450, 850, 255, 0);
  val = constrain(val, 0, 255);

11/11/2015

[C#] namespace, #include 선언의 의미

학부때 C언어 배운 지식으로 무턱대고 C#을 살펴보려니 대체 한줄 한줄이 해석이 안된다.
아무래도 Syntax를 모르기 때문인듯..

특히, C#시작시에 매번 당연하게 선언해주는

#include <iostream>
using namespace std

에 대해서 MSDN이랑 다수 여러 네이버 블로그 님들 지식을 짜집기 해서 간단히 정리 하고자 한다.
(매번 느끼는 거지만 이렇게 직접 글로 써보고 나면 머리에 남는다.)

#include, #define 등은 'Preprocessor'라고 하며 글자 그대로 본 프로그램 시작전에(pre) 실행되는걸 의미하는데, 중요한건 어떻게 부르는지보다 왜 쓰는지 아는게 중요한것 같다.

#include A 의 의미는 A헤더 파일의 내용을 본 프로그램 상단에 글자 그대로 포함 시키라는 의미이다. 더 쉽게 말해 A라는 이름의 헤더 파일에 적혀있는 모든 글자를 Ctrl + A, C 해서 Ctrl + V 하라는 의미이다.

헤더 파일안에는, namespace, class, struct, 변수 등등이 포함되어 있는데,
대략적으로 간단하게 이해하기 위해서는 

namespace > class ~= struct > function(=method), variables(=field) 

등의 먹이사슬(?) 관계를 가진다. 사실 독립적인거 같지만, 즉, 네임스페이스 바로 아래에 충분히 여러 메소드들이나 필드들이 정의 될 수 있는거 같다. 

그런데 그렇게 생각하기 시작하면 헷갈리는듯...
그냥 필드랑 메소드는 동급레벨이며 따라서 메소드 안에서 필드들이 사용될 수 있고,
비슷한 속성(Attributes)의 필드와 메소드들이 모여서 구조체나 클래스가 되고, 
비슷한 속성의 구조체나 클래스들이 모여서 네임스페이스로 정의된다 로 알고 있을 생각이다.;;
나중에 익숙해지면, 한단계 나아가겠지;;

이렇게 구분짓는 이유는 메소드, 변수들의 이름 '충돌(중복사용)등을 막기 위함' 이라고 함.

암튼 헤더파일안에 정의 되어 있을 'std' 라는 이름의 네임스페이스의 내부 값(메소드, 구조체, 클래스 etc..)에
접근하기 위해서는 아래와 같이

[C++] std:: class.method.variable..
[C#]   std.class.method.variable.. 

사용된다. 그러나, 매번 접근을 위한 operator를 사용하기 귀찮으므로,

using namespace std라고 서두에 정의를 해두면 매번 귀찮은 namespace의 이름을 쳐주지 않아도 된다. 즉,

class.method.variable만으로도 ㅇㅋ

쉽게 생각해서 컴퓨터가 나 대신에 std::을 복사  붙여넣기 해준다는 의미이다.




11/06/2015

classic game Pong - source, concept

It's a basic concept of the classic game 'Pong' that I've studied.


Initialize the const Screen Width and Height, BallRadius, Pad Width and Height
function - change the ball direction, velocity("direction")
 {
    if "direction" == right: // when going right
        ball_velocity_vector[0] = - ball_velocity_vector[0]  // change the direction 
        ball_velocity_vector[0] += 1 // increase the velocity

        if ball_velocity_vector[1] >= 0: // when touching the screen - y direction
            ball_velocity_vector[1] += 0.3 // also, increase the velocity
        else if ball_velocity_vector[1] < 0
            ball_velocity_vector[1] -= 0.3
    else if ... right...
 }

function - make a new game
{
    pad_velocity = 0 // reset the pad velocity and position

    pad_position = [coord_x, coord_y]
    if player1 win:
        score1 += 1
    else if player2 win:
        score2 += 1}
}

//Loop, printing the pad and ball position through the screen
Loop body
{
    pad1_position += pad1_velocity
    pad2_position += pad2_velocity

    //when the ball touch the pad, collision
    if ball_position[x] touch the screen line 
        if ball_position[y] touch the height of pad

            call the change the ball function("direction") 


    if ball_position is beyond the screen
        initialize the ball_position
        randomly generate the new ball_velocity
        call the function of making a new game
 }


11/05/2015

Bit, Byte, Memory address, 32비트 운영체제... 등

인터넷상에 관련글을 몇개 읽다가..

소설책 읽듯이 그런가보다 하고 넘어갔는데, 생각하면 할 수록 헷갈리기 시작하더니.. 약 20분넘게 머리싸메가면서 고민함.(바보인듯ㅠ;;)

이제 겨우 정리가 된듯하다. 혹시 또 잊어먹을까해서 남겨둠.
  • 1 byte = 8 bit 임.
  • 32비트 운영체제에서는 주소값의 크기가 32비트 임. (이부분이 완전 헷갈리게 함, 비트란 말때문에...)
    • 즉, 주소 번지수를 0, 1, 2, 3... 이렇게 메길껀데 메길 수 있는 주소의 한계(limit)가 있음. 
    • 컴퓨터는 2진 체계이므로 32개의 슬롯에서 저마다 0/1을 표기할 수 있으므로, 232가지의 서로 다른 값을 나타낼 수 있음.
    • 약 4x109 개의 표현 할 수 있는 주소 번호를 가진다는 뜻. 
  • 메모리에 주소 할당할때 1 byte 당 주소 번호 하나씩 부여 함.
    • 이부분에서도 1 byte안에 8개 bit 인데 1 bit 마다 주소를 부여한다고 생각하여 엄청 고민함..ㅠ 
    • 따라서, 4x109 개의 표현 할 수 있는 주소 번호에 주소번호당 1바이트 부여하면 왜 32비트 운영체계에서 메모리의 한계가 4GB인지 이해가 됨.
써놓고 나니까...별거 아닌거 아닌데, 32비트 운영체제란 말에서 비트란 말때문에 4바이트당 주소번호를 1개씩 할당한다고 맘대로 생각하고 완전...우주로...깨닫는데 왜이리 오래걸린건지..

역시 자기 실수는 깨닫기 어려운듯.

블로그 보관함

Facebook

Advertising

Disqus Shortname

Popular Posts